I am trying to draw a series of squares in C#. This is my first foray into WPF. My SpaceProbeView
class has a SetModel
function that allows the user to pass the data on the squares that will be rendered.
Once the model
has been set, SpaceProbeView
iterates through the 2D array that is the model. Each cell has data on what type of space it is, and the SpaceProbeView
sets the color of the square to draw based on the space type.
With the code I have right now it just keeps drawing the squares at the same position, and I've been looking through documentation and can't figure out how to adjust the position of each Rectangle
.
Here is my SpaceProbeView
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace SpaceProbe
{
public class SpaceProbeView
{
Window window = new Window();
Canvas canvas = new Canvas();
private int cellSize = 25;
SpaceProbeModel model;
int rowAndColumnSize;
SolidColorBrush emptySpaceBrush = new SolidColorBrush();
SolidColorBrush asteroidBrush = new SolidColorBrush();
SolidColorBrush gravityWellBrush = new SolidColorBrush();
SolidColorBrush gravityInfluencedBrush = new SolidColorBrush();
Brush outlineBrush = System.Windows.Media.Brushes.Black;
public SpaceProbeView(int size)
{
emptySpaceBrush.Color = Colors.Blue;
asteroidBrush.Color = Colors.Brown;
gravityWellBrush.Color = Colors.LightGray;
gravityInfluencedBrush.Color = Colors.Gray;
rowAndColumnSize = size;
window.Title = "Space Probe";
canvas.Width = rowAndColumnSize * cellSize;
canvas.Height = rowAndColumnSize * cellSize;
window.Width = rowAndColumnSize * cellSize;
window.Height = rowAndColumnSize * cellSize;
window.Content = canvas;
window.Show();
}
public void SetModel(SpaceProbeModel aModel)
{
model = aModel;
for (int row = 0; row < rowAndColumnSize; row++)
{
for (int column = 0; column < rowAndColumnSize; column++)
{
Rectangle rect = new Rectangle();
rect.Width = cellSize;
rect.Height = cellSize;
rect.Stroke = outlineBrush;
rect.RenderTransform = new Point(row * cellSize, column * cellSize);
switch (model.cellType[row, column])
{
case SpaceProbeModel.type.asteroid:
rect.Fill = asteroidBrush;
break;
case SpaceProbeModel.type.gravityInfluenced:
rect.Fill = gravityInfluencedBrush;
break;
case SpaceProbeModel.type.gravityWell:
rect.Fill = gravityWellBrush;
break;
case SpaceProbeModel.type.empty:
rect.Fill = emptySpaceBrush;
break;
}
canvas.Children.Add(rect);
}
}
}
}
}