c# - 如何使用 C# 在棋盘上绘制圆形棋子?
问题描述
我有一个在线跳棋游戏项目,在面板数组元素上绘制跳棋时遇到问题。棋盘图案是在表单的 onload 事件中绘制的。我有一种方法来绘制一个圆圈并填充同一个圆圈以在每个棋盘格上创建一个圆形,但我的表单只是显示没有棋盘格的正方形。
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Checkers
{
public partial class Form1 : Form
{
private Panel[,] _chessBoardPanels;
public Form1()
{
InitializeComponent();
}
/*This method is supposed to draw a circle which later should be
filled by the method that follows*/
public static void drawCircle(Graphics g,Pen pen,float centerX,float centerY, float radius)
{
g.DrawEllipse(pen, centerX - radius, centerY - radius,
radius + radius, radius + radius);
}
/*This method fills a circle drawn above on the panel with the desired color
*/
public static void fillCircle(Graphics g, Brush b, float centerX, float centerY, float radius)
{
g.FillEllipse(b, centerX - radius, centerY - radius,
radius + radius, radius + radius);
}
private void Form1_Load(object sender, EventArgs e)
{
//controls the size of a single square on the checkerboard
const int tileSize = 100;
//controls the number of squares on either side of the board
const int gridSize = 8;
//this is fr applying different colors to the board so the pattern is realized
var clr1 = Color.DarkGray;
var clr2 = Color.White;
// initialize the "chess board"
_chessBoardPanels = new Panel[gridSize, gridSize];
// double for loop to handle all rows and columns
for (var n = 0; n < gridSize; n++)
{
for (var m = 0; m < gridSize; m++)
{
// create new Panel control which will be one
// chess board tile
var newPanel = new Panel
{
Size = new Size(tileSize, tileSize),
Location = new Point(tileSize * n, tileSize * m)
};
//get the graphics context from each panel and store in g
Graphics g = newPanel.CreateGraphics();
//instantiate a pen
Pen penner = new Pen(Brushes.Black, 3);
//defining the center of the circle
float cx = newPanel.Width / 2;
float cy = newPanel.Height / 2;
//defining the radius of the circle
float radius = cx / 2;
// add to Form's Controls so that they show up
Controls.Add(newPanel);
// add to our 2d array of panels for future use
_chessBoardPanels[n, m] = newPanel;
// color the backgrounds
if (n % 2 == 0)
newPanel.BackColor = m % 2 != 0 ? clr1 : clr2;
else
newPanel.BackColor = m % 2 != 0 ? clr2 : clr1;
//draw a new checker piece if this is true
if (n % 2 == 0)
drawCircle(g, penner, cx, cy, radius);
else
;
}
}
}
}
}
欢迎任何帮助我画一个圆圈并填充它,以便实现棋盘上棋子的图像。面向对象的解决方案也受到欢迎class Checkers
,这是因为编写方法将棋子轻松移动到板上的某个面板会更容易。
解决方案
在对原始答案的有用评论之后,我意识到您需要使用一个Control.Paint
事件在正方形上绘制圆圈。
代码
public class Checkerpiece
{
//colors of the rounded pieces
Color color;
//specify where the checker is drawn
Panel target_square;
//specify the center of the circle
float center_x;
float center_y;
//specify the radius of the checker piece
float radii;
//fill the details inside the constructor
public Checkerpiece(Panel mypanel,Color color)
{
this.color = color;
this.target_square = mypanel;
this.center_x = mypanel.Width / 2;
this.center_y = mypanel.Height / 2;
this.radii = mypanel.Width / 2;
}
//this method draws the checkerpiece on the target panel
public void draw()
{
this.target_square.Paint += Target_square_Paint;
}
//this event will redraw the circles as needed
private void Target_square_Paint(object sender, PaintEventArgs e)
{
Pen mypen = new Pen(color, 3);
drawCircle(e.Graphics, mypen, this.center_x, this.center_y, this.radii);
}
public static void drawCircle(Graphics g, Pen pen, float centerX, float centerY, float radius)
{
g.DrawEllipse(pen, centerX - radius, centerY - radius,
radius + radius, radius + radius);
}
}
输出
推荐阅读
- javascript - MathJax.js 从方程中删除空格
- reactjs - React - 未处理的拒绝(TypeError):无法读取未定义的属性“front_default”
- c - 为什么 For 函数没有按预期工作?
- javascript - Javascript拆分数组并一遍又一遍地调用它
- postgresql - 获取每日重复的交易金额
- c - Accelerate 框架在 Mac M1 上仅使用一个内核
- php - 如何根据逐步 div 设置 div 的高度
- amazon-web-services - 有没有办法为 AWS 放大项目生成配置文件 (yaml) 或某种方式?
- vue.js - 在没有点击事件的情况下使用 $emit
- c++ - 将输入读取到 C++ 中的数组时出错