我遵循了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);
        }


但是,圆没有在网格面板上绘制,并且在母面板“ myGridPanel”上也没有网格。我看到没有调用CirclePanel的paintComponant()。

输出:http://s25.postimg.org/v8u398dun/no_Gridnd_Circle.png

最佳答案

正如我在评论中提到的,您可以通过不将每个Circle都设为一个面板来Refine your Design。这是an example

另一个适合您当前设计的选项是在每个圆形面板中都有一个设置器,您可以为其传递圆形模型。

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();
            }
        });
    }
}

关于java - 现有JPanel内的绘图圆,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26827914/

10-10 11:06