gpt4 book ai didi

java - 添加到 JComponent 的绘制 JComponent 的问题

转载 作者:行者123 更新时间:2023-12-01 18:15:39 24 4
gpt4 key购买 nike

我在绘制 jcomponent 时遇到问题

//应在其中绘制矩形的类

public class Board extends JComponent
{
private Case[][] cases= new Case[10][10];

public Plateau() {
super();
this.setLayout(new GridLayout(10,10));
this.setSize(getPreferredSize());

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if ((i + j) % 2 == 0) {

cases[i][j] = new WhiteCase(j * Case.LONGUEUR, i * Case.LONGUEUR, Case.LONGUEUR, Case.LONGUEUR);
} else {
cases[i][j] = new BlackCase(j * Case.LONGUEUR, i * Case.LONGUEUR, Case.LONGUEUR, Case.LONGUEUR);

}
add(cases[i][j]);
}
}
repaint();

}
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
}

//矩形基类

public abstract class   Case extends JComponent  {


protected static final int LONGUEUR=60;

protected int x,y,width,height;
protected abstract void paintComponent(Graphics g);
public Dimension getPreferredSize() { return new Dimension(LONGUEUR, LONGUEUR);
}
}

///黑壳

public class BlackCase extends Case
{

private Piece piece;
private static final long serialVersionUID = 1L;
public CaseNoire(int x, int y,int width,int height)
{

this.x=x;
this.y=y;
this.width = width;
this.height= height;
}
public Dimension getPreferredSize() {
return new Dimension(LONGUEUR, LONGUEUR);
}
@Override
protected void paintComponent(Graphics g)
{

g.setColor(Color.darkGray);
g.fillRect(x, y,width,height);
}
}


public class CaseWhite extends Case {

/**
*
*/
private static final long serialVersionUID = 1L;
public CaseBlanche(int x, int y,int width,int height)
{

this.x=x;
this.y=y;
this.width = width;
this.height= height;


}

@Override
public void paintComponent(Graphics g)
{

g.setColor(Color.white);
g.fillRect(x, y,width,height);
g.setColor(Color.BLACK);
g.drawString("X= "+x , 10, 10);
}
public Dimension getPreferredSize() {
return new Dimension(LONGUEUR, LONGUEUR);
}

}

//Main class
public class CheckersGame extends JFrame {

private static final long serialVersionUID = 1L;

public static void main(String[] args )
{
CheckersGame checkers= new CheckersGame();
}
public CheckersGame()
{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Jeu de Dames");
JPanel panelPrincipalDame = new JPanel(new GridBagLayout());

Board board = new Board();
GridBagConstraints c = new GridBagConstraints();
c.fill= GridBagConstraints.NONE;
c.gridx =0;
c.gridy = 0 ;
c.gridheight= 2;
c.gridwidth= 2;

panelPrincipalDame.add(plateau,c);
setSize(800, 700);
setContentPane(panelPrincipalDame);

![//setVisible(true);][1]
setResizable(false);
}
}

这段代码的结果是(注意X+0等..仅用于调试目的) enter image description here

但我想要的是这个 enter image description here

请问为什么我只得到一个矩形?

最佳答案

非常感谢我在上一个问题中给出的不创建“CaseNoire”和“CaseBlanch”类的建议:paintComponent does not paint correctly从两周前开始。这些类(class)是不需要的。如果您想让用户灵活地选择方 block 的颜色,会发生什么?你的游戏逻辑永远不应该基于类名或类似的东西。因此,摆脱这些类并使用内置的 Swing 功能来为组件的背景着色。

我认为问题是因为您在 Case 类中创建了变量“x,y,宽度,高度”。我相信这些变量已经在 Component 类中定义,用于表示组件的大小/位置。

摆脱变量,您不需要管理每个组件的大小/位置,因为 GridLayout 会为您做这件事。

再次查看我给您的示例代码,它展示了如何创建“ChessBoard”。

关于java - 添加到 JComponent 的绘制 JComponent 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com