我跟着peeskillet并根据他的最后想法改变了我的代码。以下是更改的代码
CriclePanel类
public class CirclePanel extends JPanel {
int centerX, centerY, radius;
Color circle_color;
public void setCircle(int centerX, int centerY, int radius, Color c) {
this.centerX = centerX;
this.centerY = centerY;
this.radius = radius;
circle_color = c;
revalidate();
repaint();
}
protected void paintComponent(Graphics g) {
System.out.println("Trying to draw circle");
super.paintComponent(g);
g.fillOval(centerX, centerY, radius*2, radius*2);
}
}
网格面板(从调色板添加并自定义构建)
myGridPanel = new JPanel(new GridLayout(8,8));
panels = new CirclePanel[8][8];
for (int i = 0; i < panels.length; i++) {
for (int j = 0; j < panels[i].length; j++) {
CirclePanel panel = new CirclePanel();
panels[i][j] = panel;
myGridPanel.add(panel);
}
}
尝试添加绘制圆圈:
if(dType==DiceType.blackDice){
System.err.println("black @"+i+","+j);
panels[i][j].setCircle(x, y, radius, Color.BLACK);
}else{
System.err.println("white @"+i+","+j);
panels[i][j].setCircle(x, y, radius, Color.WHITE);
}
但是圈子没有在网格面板上绘制,并且母板上也没有网格&#34; myGridPanel&#34;。我发现没有调用CirclePanel的paintComponant()。
答案 0 :(得分:2)
正如我在评论中提到的那样,你可以Refine your Design不要让每个Circle
成为一个小组。这是an example
适合当前设计的另一个选项是在每个圆形面板中都有一个setter,您可以为其传递圆形模型。
public class CirclePanel extends JPanel {
private Circle circle;
public void setCircle(Circle cirlce) {
this.circle = circle;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (circle != null) {
// get the state from circle and paint
g.fillOval(circle.getX(), circle.getY(), getWidth(), getHeight());
}
}
}
注意:在这种情况下,Circle
不是面板
然后,gridPanel
为GridLayout
添加CirclePanel
并将JPanel gridPanel = new JPanel(new GridLayout(5, 5));
CirclePanel[][] panels = new CirclePanel[5][5];
for (int i = 0; i < panels.length; i++) {
for (int j = 0; j < panels[i].length; j++) {
CirclePanel panel = new CirclePanel();
panels[i][j] = panel;
gridPanels.add(panel);
}
}
添加到其中。
CirclePanel
执行此操作后,gridPanel
中将显示空Circle circle = new Circle(...);
panels[1][1].setCircle(circle);
。然后,当您想在其中一个面板中绘制圆形时,可以执行类似
Circle
<强>更新强>
或者现在我想起来,你甚至不需要一个0, 0, getWidth(), getHeight()
课程,因为你可以画一个public class CirclePanel extends JPanel {
private boolean draw;
private Color color;
// setter for color
// setter for draw
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (draw) {
// get the state from circle and paint
g.fillOval(0, 0, getWidth(), getHeight());
}
}
}
的圆圈。你可以简单地设置一个标志来绘制。但是如果你想在圆圈中添加更多状态,那么也许你可以保持一个圆圈类。但如果没有,它可能看起来像
setDraw
请参阅方法Circle
。那是我设置标志的平局或不平局。我在import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CircleDemo {
private static final int GRID_SIZE = 5;
private Circle[][] circlePanels
= new Circle[GRID_SIZE][GRID_SIZE];
private JPanel gridPanel
= new JPanel(new GridLayout(GRID_SIZE, GRID_SIZE));
public CircleDemo() {
initCircles();
JFrame f = new JFrame();
f.add(gridPanel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private void initCircles() {
for (int i = 0; i < circlePanels.length; i++) {
for (int j = 0; j < circlePanels[i].length; j++) {
Circle circle = new Circle();
circlePanels[i][j] = circle;
gridPanel.add(circle);
}
}
}
class Circle extends JPanel {
private boolean draw = false;
private Color color = Color.BLUE;
public Circle() {
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
if (isDraw()) {
setDraw(false);
} else {
setDraw(true);
}
}
});
}
public boolean isDraw() {
return draw;
}
public void setDraw(boolean draw) {
this.draw = draw;
repaint();
}
public void setColor(Color color) {
this.color = color;
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(75, 75);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (draw) {
g.setColor(color);
g.fillOval(0, 0, getWidth(), getHeight());
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new CircleDemo();
}
});
}
}
类的鼠标监听器中调用它,但你可以从任何地方调用它。
{{1}}