我已经定义了一个名为 Stone 的类来向 JPanel 添加图形石头:
public class Stone {
private int x, y;
private Color color;
private static final int radius = 18;
Stone(Color color) {
this.color = color;
}
public Stone(int x, int y, Color color) {
this(color);
this.x = x;
this.y = y;
}
void draw(Graphics g) {
g.setColor(color);
g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
void setX(int x) {
this.x = x;
}
void setY(int y) {
this.y = y;
}
}
我想在 JPanel 上绘制它们。我必须在 JPanel 的 paint 方法中执行此操作还是可以使用 JPanel 的 add 方法?
最佳答案
一个简单的回答是,您应该扩展一个JComponent
(因为您想将它添加到JPanel
)并覆盖paintComponent
方法(因为你想要你的对象的一些自定义绘画)。
关于java - 如何向 jpanel 添加视觉效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5781715/