I made a square of 18*18 now. I'd like to fill the first boundary of the square with black color like in the picture,
what should I do?
public partial class Form1 : Form
{
private int XTILES = 25;
private int YTILES = 25;
private int TILELOCATION = 20;
Graphics g;
Pen pen;
public Form1()
{
InitializeComponent();
pen = new Pen(Color.Black);
}
private void DrawBoard()
{
g = panel.CreateGraphics();
for(int i = 0; i < 19; i )
{
g.DrawLine(pen, new Point(TILELOCATION i * XTILES, TILELOCATION),
new Point(TILELOCATION i * XTILES, TILELOCATION 18 * XTILES));
g.DrawLine(pen, new Point(TILELOCATION, TILELOCATION i * YTILES),
new Point(TILELOCATION 18 * YTILES, TILELOCATION i * YTILES));
}
}
private void panel_Paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
DrawBoard();
}
}
CodePudding user response:
There are many ways. Here is one of them: Draw top, left, right and bottom edges separately using FillRect.
Brush brush = new SolidBrush(Color.Black);
private void panel_Paint(object? sender, PaintEventArgs e)
{
DrawBoundary(e.Graphics);
}
void DrawBoundary(Graphics g)
{
g.FillRectangle(brush, ToGraphics(new Rectangle(0, 0, XTILES, 1)));
g.FillRectangle(brush, ToGraphics(new Rectangle(XTILES - 1, 0, 1, YTILES)));
g.FillRectangle(brush, ToGraphics(new Rectangle(0, YTILES - 1, XTILES, 1)));
g.FillRectangle(brush, ToGraphics(new Rectangle(0, 0, 1, YTILES - 1)));
}
public Rectangle ToGraphics(Rectangle r)
{
return new Rectangle(TILELOCATION r.X * TILESIZE, TILELOCATION r.Y * TILESIZE, r.Width * TILESIZE, r.Height * TILESIZE);
}