I've got function:
private string[] Letters = new string[11] {"", "A", "B", "C", "D", "E","F", "G","H", "I", "J"};
private void GenerateGameMap()
{
int LocA = 5;
int LocB = 5;
for(int i = 1; i < 12; i++)
{
for(int i2 = 1; i2 < 12; i2++)
{
Canvas canvas = new Canvas();
canvas.Width = 26;
canvas.Height = 26;
canvas.Margin = new Thickness(LocA + 1, LocB + 1, 0, 0);
Border border = new Border();
border.BorderThickness = new Thickness(2);
if (i == 1 || i2 == 1)
{
Label label = new Label();
label.FontFamily = new FontFamily("Arial");
label.FontSize = 20;
label.Foreground = Brushes.White;
label.Margin = new Thickness(-1, -2, 0, 0);
label.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
label.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
if(i == 1)
{
if(i2 > 1)
{
label.Content = Litery[i2 - 1];
}
}else
{
if(i2==1)
{
label.Content = (i - 1).ToString();
}
}
border.BorderBrush = Brushes.Gold;
canvas.Background = Brushes.Black;
canvas.Children.Add(label);
}else
{
border.BorderBrush = Brushes.CadetBlue;
canvas.Background = Brushes.BurlyWood;
}
if(i > 1 && i2 > 1 )
{
canvas.Name = Letters[i2 - 1] + (i - 1).ToString();
canvas.MouseLeftButtonUp +=canvas_MouseLeftButtonUp;
}
border.Width = 28;
border.Height = 28;
border.Margin = new Thickness(LocA, LocB, 0, 0);
LocA+=30;
MainGameCanvas.Children.Add(canvas);
MainGameCanvas.Children.Add(border);
if(i2 == 11)
{
LocA = 5;
}
}
LocB += 30;
}
}
Everything is fine with that function. But when I'am trying to find canvas:
private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
string canvasName = Text1.Text + Text2.Text;
var GameObject = MainGameCanvas.FindName(canvasName);
Canvas SelectedGameobject = GameObject as Canvas;
SelectedGameobject.Background = Brushes.YellowGreen;
}
I'am receiving error 'Object reference not set on instance of an object' in line "SelectedGameobject.Background = Brushes.YellowGreen;". Is there other way to find this control ?. As i said, I'am sure that function GenerateGameMap() is working corectly.
Related
I set the points and when the points of the same color form a square, I draw a polygon. But when a new square is formed, the old one disappears.
can you tell me how to make sure that when drawing a new polygon, the old one does not disappear?
in the checkpoint() function, I check whether there is a square of points of the same color and return e coordinates for drawing.
public partial class Form1 : Form
{
private Class1 Class1 = new Class1();
private CellState currentPlayer = CellState.Red;
public const int SIZE = 11;
public const int Icon_Size = 30;
public Form1()
{
InitializeComponent();
}
//ставит точки
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
var p = new Point((int)Math.Round(1f * e.X / Icon_Size), (int)Math.Round(1f * e.Y / Icon_Size));
if (Class1[p] == CellState.Empty)
{
Class1.SetPoint(p, currentPlayer);
currentPlayer = Class1.Inverse(currentPlayer);
Invalidate();
}
}
//рисуем
private void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.ScaleTransform(Icon_Size, Icon_Size);
//рисуем сеточку
using (var pen = new Pen(Color.Gainsboro, 0.1f))
{
for (int x = 1; x < SIZE; x++)
e.Graphics.DrawLine(pen, x, 1, x, SIZE - 1);
for (int y = 1; y < SIZE; y++)
e.Graphics.DrawLine(pen, 1, y, SIZE - 1, y);
}
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//рисуем точки
using (var brush = new SolidBrush(Color.White))
for (int x = 1; x < Form1.SIZE; x++)
for (int y = 1; y < Form1.SIZE; y++)
{
var p = new Point(x, y);
var cell = Class1[p];
if (cell != CellState.Empty)
{
brush.Color = StateToColor(cell);
e.Graphics.FillEllipse(brush, x - 0.2f, y - 0.2f, 0.4f, 0.4f);
}
}
using (var PenP = new Pen(Color.Black, 0.1f))
using (var brush = new SolidBrush(Color.White))
{
Class1.CheckPoint();
int i = Class1.CheckPoint()[0];
int j = Class1.CheckPoint()[1];
int cp = Class1.CheckPoint()[2];
if (cp == 1)
{
PenP.Color = Color.Red;
brush.Color = Color.IndianRed;
Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
e.Graphics.FillPolygon(brush, a);
e.Graphics.DrawPolygon(PenP, a);
}
if (cp == 2)
{
PenP.Color = Color.Blue;
brush.Color = Color.RoyalBlue;
Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
e.Graphics.FillPolygon(brush, a);
e.Graphics.DrawPolygon(PenP, a);
}
}
}
//условие смены цвета под ход игрока
Color StateToColor(CellState state, byte alpha = 255)
{
var res = state == CellState.Blue ? Color.Blue : Color.Red;
return Color.FromArgb(alpha, res);
}
}
enter image description here
I have captured points data from bamboo slate,and converted them to Windows.UI.Input.Inking.InkStroke data.Then I put them in a InkPresenter.StrokeContainer rendered like this image above.Strokes sticked to each other,how can I seperate them?
This is my code below.
private void DataDisplay()
{
List<InkPoint> list = new List<InkPoint>();
List<InkStroke> strokes = new List<InkStroke>();
InkDrawingAttributes drawingAttributes1 = new InkDrawingAttributes();
drawingAttributes1.Color = Colors.Black;
drawingAttributes1.Size = new Size(1, 1);
InkStrokeBuilder builder = new InkStrokeBuilder();
builder.SetDefaultDrawingAttributes(drawingAttributes1);
inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes1);
inkCanvas.InkPresenter.IsInputEnabled = true;
foreach (var item in data.Stroke)
{
string[] strArray = item.Split(',');
for (int i = 9; i <= strArray.Length - 5; i += 5)
{
float x = float.Parse(strArray[i]) / 30;
float y = float.Parse(strArray[i + 1]) / 30;
float pressure = float.Parse(strArray[i + 2]) / 1000;
Point point = new Point(x, y);
InkPoint ip = new InkPoint(point, pressure);
list.Add(ip);
}
Matrix3x2 matrix3X2 = new Matrix3x2(1, 0, 0, 1, 0, 0);
InkStroke newStroke = builder.CreateStrokeFromInkPoints(list, matrix3X2);
strokes.Add(newStroke);
}
inkCanvas.InkPresenter.StrokeContainer.AddStroke(strokes);
}
I am showing multiple Y axis for chart by using below code. When any axis label value is having more than 3 digits then that axis Label/Title is getting overlap with other axis Label (as shown in image).
int leftIndex = 0, rightIndex = 0;
int relativePosition = 0;
foreach (Steema.TeeChart.Axis axis in this.tChart.Axes.Custom)
{
axis.Visible = true;
axis.PositionUnits = Steema.TeeChart.PositionUnits.Pixels;
axis.RelativePosition = 0 - (axis.OtherSide ? rightIndex++ : leftIndex++) * 60;
relativePosition = relativePosition + (axis.AxisRect().Width + 60);
}
You should be able to get your chart to render correctly by slightly modifying the constant in your calculation, e.g.
TChart _tChart;
public Form1()
{
InitializeComponent();
_tChart = new TChart();
_tChart.Dock = DockStyle.Fill;
_tChart.Series.Add(typeof(Line)).FillSampleValues();
_tChart.Series.Add(typeof(Line)).FillSampleValues();
_tChart.Series.Add(typeof(Line)).FillSampleValues();
_tChart.Series[0].YValues.Value = _tChart.Series[2].YValues.Value.Select(x => x * 100).ToArray();
_tChart.Header.Text = Utils.Version;
_tChart[0].CustomVertAxis = _tChart.Axes.Custom.Add();
_tChart[0].CustomVertAxis.Title.Text = "Axis One Title";
_tChart[0].CustomVertAxis.Title.Angle = 90;
_tChart[1].CustomVertAxis = _tChart.Axes.Custom.Add();
_tChart[1].CustomVertAxis.Title.Text = "Axis Two Title";
_tChart[1].CustomVertAxis.Title.Angle = 90;
_tChart[2].CustomVertAxis = _tChart.Axes.Custom.Add();
_tChart[2].CustomVertAxis.Title.Text = "Axis Three Title";
_tChart[2].CustomVertAxis.Title.Angle = 90;
int leftIndex = 0, rightIndex = 0;
for (int i = 0; i < this._tChart.Axes.Custom.Count; i++)
{
var axis = this._tChart.Axes.Custom[i];
axis.Visible = true;
axis.PositionUnits = Steema.TeeChart.PositionUnits.Pixels;
axis.RelativePosition = 0 - (axis.OtherSide ? rightIndex++ : leftIndex++) * (i == 1 ? 80: 70);
}
_tChart.Panel.MarginLeft = 30;
this.Controls.Add(_tChart);
}
Which here gives me:
I got a form with 7 users on it among each other. But I want to set 7 button next to it in a loop. Right now I've got this.
int x = 12;
int y = 30;
foreach (details dets1 in detailsList)
{
var label = new Label();
label.AutoSize = true;
label.Location = new Point(x, y);
label.Name = dets1.fname;
label.Text = dets1.fname;
this.Controls.Add(label);
y += label.Height + 5;
}
for (int i = 0; i < 7 ; i++)
{
Button button = new Button();
button.Location = new Point(200, 30);
button.Text = "Off";
button.Tag = i;
button.BackColor = Color.Red;
this.Controls.Add(button);
button.Click += button_Click;
}
Your problem is in the Location: you put all the buttons on the same place
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200, 30);
...
}
Let's organaize all the buttons vertically:
const int shift = 50;
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200, 30 + shift * i);
...
}
Or horizontally:
const int shift = 90;
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200 + shift * i, 30);
...
}
i am developing an app in which i ant to do some work in background and in the the ui the the timer should run like starts in 3 2 1 and then by that time my manipulation will be completed . but the issue is that is that even my methods are async my UI is getting blocked.
my code is
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
Task<WriteableBitmap> transparentbordertemp = Render("tilenew.png");
Task<WriteableBitmap> transparentbitmaptemp = Render("greenpattern.png");
if (GameSettingData.mainimageselected == null)
{
bitmapimagemain = await Render("puzzleimage.png");
bitmapimagemain = bitmapimagemain.Resize(550, 550, WriteableBitmapExtensions.Interpolation.Bilinear);
puzzleimage.Source = bitmapimagemain;
GameSettingData.mainimageselected = bitmapimagemain;
}
else
{
bitmapimagemain = GameSettingData.mainimageselected;
bitmapimagemain = bitmapimagemain.Resize(550, 550, WriteableBitmapExtensions.Interpolation.Bilinear);
puzzleimage.Source = bitmapimagemain;
}
height = (puzzleimage.Parent as Grid).ActualHeight + 15;
width = (puzzleimage.Parent as Grid).ActualWidth - 25;
double startx = 0;
double starty = 0;
heightpiece = 546 / numberofrows;
widhpiece = 550 / numberofrows;
animationgrid.Height = heightpiece + 100;
animationgrid.Width = widhpiece + 100;
Rect rectangle;
int counter = 0;
WriteableBitmap transparentborder = new WriteableBitmap(330, 330);
finalbitmapimage = bitmapimagemain;
finalbitmapimage = finalbitmapimage.Resize(330 * numberofrows, 330 * numberofrows, WriteableBitmapExtensions.Interpolation.Bilinear);
int blithelpx = 0;
int blithelpy = 0;
for (int i = 0; i < numberofrows; i++)
{
for (int j = 0; j < numberofrows; j++)
{
imagepeices = new Image();
imagepeices.Height = bitmapimagemain.PixelHeight / (2 * numberofrows);
imagepeices.Width = bitmapimagemain.PixelWidth / (2 * numberofrows);
imagepeices.Stretch = Stretch.Uniform;
counter++;
imagepeices.Tag = counter;
startx = j * bitmapimagemain.PixelWidth / numberofrows;
starty = i * bitmapimagemain.PixelHeight / numberofrows;
rectangle = new Rect(startx, starty, bitmapimagemain.PixelWidth / numberofrows, bitmapimagemain.PixelHeight / numberofrows);
WriteableBitmap bitmapimagemain1 = bitmapimagemain.Crop(rectangle).Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
// transparentborder = await Render("tilenew.png");
transparentborder = await transparentbordertemp;
transparentborder = transparentborder.Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
final = new WriteableBitmap(transparentborder.PixelWidth, transparentborder.PixelHeight);
final.Blit(new Rect(0, 0, (int)bitmapimagemain1.PixelWidth, (int)bitmapimagemain1.PixelHeight), bitmapimagemain1, new Rect(0, 0, (int)bitmapimagemain1.PixelWidth, (int)bitmapimagemain1.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
final.Blit(new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), transparentborder, new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
final = final.Resize((int)widhpiece, (int)widhpiece, WriteableBitmapExtensions.Interpolation.Bilinear);
imagepeices.Source = final;
imagepeices.PointerPressed += ImagePointerPressed;
imagepeices.PointerReleased += ImagePointerReleased;
imagepeices.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
imagepeices.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
listimages.Add(imagepeices);
bitmaplist.Add(bitmapimagemain.Crop(rectangle));
margin += 20;
gridpieces.Children.Add(imagepeices);
finalbitmapimage.Blit(new Rect(blithelpx, blithelpy, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), transparentborder, new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
blithelpx += 330;
}
blithelpx = 0;
blithelpy += 330;
}
foreach (Image item in listimages)
{
var a = (item.Parent as Grid).ActualHeight;
var b = (item.Parent as Grid).ActualWidth;
var x = rand.Next(50, 250);
var y = rand.Next(100, 400);
transformpoints.Add(new Point(x, y));
TranslateTransform posTransform = new TranslateTransform();
posTransform.X = x;
posTransform.Y = y;
item.RenderTransform = posTransform;
}
newgrid = new Grid();
RowDefinition[] rows = new RowDefinition[numberofrows];
ColumnDefinition[] columns = new ColumnDefinition[numberofrows];
for (int q = 0; q < numberofrows; q++)
{
columns[q] = new ColumnDefinition();
columns[q].Width = new GridLength(1, GridUnitType.Star);
newgrid.ColumnDefinitions.Add(columns[q]);
}
for (int t = 0; t < numberofrows; t++)
{
rows[t] = new RowDefinition();
rows[t].Height = new GridLength(1, GridUnitType.Star);
newgrid.RowDefinitions.Add(rows[t]);
}
int counter1 = 0;
startx = 0;
starty = 0;
counter = 0;
for (int p = 0; p < numberofrows; p++)
{
for (int u = 0; u < numberofrows; u++)
{
counter1++;
Grid qwe = new Grid() { Name = "asd" + counter1.ToString(), Tag = counter1, Margin = new Thickness(0, 0, 0, 0) };
Image transparentimage = new Image();
transparentimage.Stretch = Stretch.Uniform;
WriteableBitmap transparentbitmap = await transparentbitmaptemp;
transparentbitmap = transparentbitmap.Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
Image imageblur = new Image();
imageblur.Stretch = Stretch.Uniform;
imageblur.Source = transparentbitmap;
imageblur.Height = transparentbitmap.PixelHeight;
imageblur.Width = transparentbitmap.PixelWidth;
if (u == 0)
{
imageblur.Margin = new Thickness(10, 0, -20, 1);
}
else if (u == numberofrows - 1)
{
imageblur.Margin = new Thickness(0, 0, 0, 1);
}
else
{
if (u == 1)
{
imageblur.Margin = new Thickness(0, 0, -13, 1);
}
else
{
imageblur.Margin = new Thickness(-13, 0, -13, 1);
}
}
transparentimage.Source = final;
qwe.Height = heightpiece;
qwe.Width = widhpiece;
qwe.Children.Add(imageblur);
qwe.Opacity = .6;
Grid.SetColumn(qwe, u);
Grid.SetRow(qwe, p);
newgrid.Children.Add(qwe);
}
}
newgrid.Margin = new Thickness(10, 0, 10, 0);
imagegrid.Children.Add(newgrid);
puzzleimage.Source = finalbitmapimage;
disabletopbar = 0;
initialcountdowngrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
imagegrid.Opacity = 1;
gridpieces.Opacity = 1;
dispatchertimer.Start();
}
and my render method is
private async Task<WriteableBitmap> Render(string filename)
{
var Assets = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("JumblerGame");
var folder = await Assets.GetFolderAsync("Images");
StorageFile file1 = await folder.GetFileAsync(filename);
BitmapImage i1 = new BitmapImage();
using (IRandomAccessStream strm = await file1.OpenReadAsync())
{
i1.SetSource(strm);
}
WriteableBitmap img1 = new WriteableBitmap(i1.PixelWidth, i1.PixelHeight);
using (IRandomAccessStream strm = await file1.OpenReadAsync())
{
img1.SetSource(strm);
}
return img1;
}
and in my on navigation i have started a timer on the ui but the timer does not start untill this async method is executed.
i dont understand why ...
All the code with all for loops and image processing in PageLoaded is executed on the UI thread, so that probably what is preventing the dispatcher time to execute.
You could add await Task.Delay(10); inside the for loops to free a little of the UI thread. You could also use await Task.Run(()=>{ ....}); to execute the image processing on WriteableBitmap which don't need to be executed on the UI thread