棋盘上的 java2D 棋子拖放
对于一个学校项目,我正在用java制作一个棋盘。
有一定的限制,我们不能对任何棋子使用图像。我们需要用多种形状制作一个棋子。
例如,我有一个由圆形和圆角正方形制成的棋子。这是一些代码片段。这是定义为一组字符的棋盘,每个字符代表棋盘上的一个检查
private char[][] board = new char[][] { { 'T', 'H', 'B', 'Q', 'K', 'B', 'H', 'T' },
{ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' },
{ 'T', 'H', 'B', 'Q', 'K', 'B', 'H', 'T' } };
,这里是创建普通棋子的方法的内容,
for(int i=0; i<8; i++) {
for(int j=0; j<8;j++) {
if(board[j][i] == 'P') {
Ellipse2D.Double ellipse = new Ellipse2D.Double(i * getWidth() / 8 + 20,
j * getHeight() / 8 + 20,
getWidth()/8 - 40,getHeight()/8 - 40);
g2d.setPaint(new GradientPaint(i * getWidth() / 8 , j * getHeight() / 8 + 20, Color.orange, i * getWidth() / 8, j * getHeight() / 8 + 60,
Color.pink, false) );
g2d.fill(ellipse);
RoundRectangle2D.Double roundRect = new RoundRectangle2D.Double(i*getWidth() / 8 + 20,
j*getHeight() / 8 + 10,
getWidth()/8 - 40, getHeight()/8-70,5,5);
g2d.setPaint(new GradientPaint(i * getWidth() / 8 , j * getHeight() / 8 + 20, new Color(20,20,150), i * getWidth() / 8, j * getHeight() / 8 + 60,
new Color(20, 20, 100), false) );
g2d.fill(roundRect);
}
}
}
可能不是最干净的代码,如果有任何建议可以做得更好,请提出建议!
现在,真正的问题,我的问题是,我们必须能够将这些多个形状一次拖放到板上的另一个位置,老实说,我根本不知道如何解决这个问题。
非常感谢你们能给我的任何帮助!
提前致谢!
for a school project, I am making a chessboard in java.
there were certain limitations, being that we can't use images for any of the pawns. We needed to make a pawn out of multiple shapes.
For example I have a pawn made out of a circle and a rounded square. here are some pieces of the code. This is the board defined as a set of chars, each represent a check on the board
private char[][] board = new char[][] { { 'T', 'H', 'B', 'Q', 'K', 'B', 'H', 'T' },
{ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' },
{ 'T', 'H', 'B', 'Q', 'K', 'B', 'H', 'T' } };
and here is the content of the method that creates a normal pawn
for(int i=0; i<8; i++) {
for(int j=0; j<8;j++) {
if(board[j][i] == 'P') {
Ellipse2D.Double ellipse = new Ellipse2D.Double(i * getWidth() / 8 + 20,
j * getHeight() / 8 + 20,
getWidth()/8 - 40,getHeight()/8 - 40);
g2d.setPaint(new GradientPaint(i * getWidth() / 8 , j * getHeight() / 8 + 20, Color.orange, i * getWidth() / 8, j * getHeight() / 8 + 60,
Color.pink, false) );
g2d.fill(ellipse);
RoundRectangle2D.Double roundRect = new RoundRectangle2D.Double(i*getWidth() / 8 + 20,
j*getHeight() / 8 + 10,
getWidth()/8 - 40, getHeight()/8-70,5,5);
g2d.setPaint(new GradientPaint(i * getWidth() / 8 , j * getHeight() / 8 + 20, new Color(20,20,150), i * getWidth() / 8, j * getHeight() / 8 + 60,
new Color(20, 20, 100), false) );
g2d.fill(roundRect);
}
}
}
probably not the cleanest code to do it, if there are any suggestions to do that better, please do suggest!
Now, the real problem, and my question is though, we must be able to drag and drop these multiple shapes at once onto another place on the board, and I honestly have no idea at all how to go over that.
Any help you guys can give me will be very much appreciated!
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议不要使用图像,而是在程序开始时创建一些 BufferedImages 来为棋子创建图像。然后将它们添加到 ImageIcons(可以多次使用),并将它们添加到 JLabels(不能)。例如,白色一侧将有 8 个 JLabel 用于 8 个 pawn,但每个 JLabels 将使用相同的白色 pawn ImageIcon。然后只需添加代表棋盘上每个单元格的 JLabels JPanels 即可。我会给单元格 JPanels 一个 GridBagLayout,以便 JLabels 可以毫不费力地添加到它们的中心。
有关示例,请查看:向 jpanel 添加 jlabel 是否隐藏 jpanel
I'd suggest not using an image but instead creating a few BufferedImages at the beginning of your program that creates images for your chess pieces. Then add them to ImageIcons (which can be used more than once), and add these to JLabels (which can't). So for instance the white side will have 8 JLabels for the 8 pawns, but each of these JLabels will use the same white pawn ImageIcon. Then simply add the JLabels JPanels that represents each cell on the chessboard. I'd give the cell JPanels a GridBagLayout so that the JLabels will be added to their center without any fuss.
For an example of this, please check out: does-adding-a-jlabel-to-a-jpanel-hide-the-jpanel