如何在JPanel的gridLayout中绘制随机形状

时间:2018-04-14 18:39:36

标签: java swing

代码应该做的是通过单击按钮绘制X随机形状。到目前为止我所拥有的是一个子类,它创建了放置在JPanels中的随机形状,但问题是所有面板都使用相同的形状,我需要将每个形状随机化。

子类看起来像这样:

public class Shapes extends JPanel
{
    Random rand = new Random();
    private int x = 5;
    private int y = 5;
    private int diameter = 200;
    private Color outline;
    private Color internal;
    private Color internal2;
    private Color internal3;


    public Shapes() {
     this(new Random());
    }
    //randomizes colors
    public Shapes(Random rand) {
        outline = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
        internal = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
        internal2 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
        internal3 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
    }

    {
        super.paintComponent(g);
        g.setColor(outline);
        g.drawOval(x, y, diameter, diameter);

        g.setColor(internal);
        g.fillOval(x+2, y+2, diameter-4, diameter-4);

        g.setColor(internal2);
        g.fillOval(x+25, y+66, diameter/3, diameter/3);
        g.fillOval(x+125, y+66, diameter/3, diameter/3);

        g.setColor(internal3);
        g.fillArc(x+55, y+105, diameter/3, diameter/3, 180, 180);
    }
}

虽然主要类看起来像[目前设置为制作6张图像]:

public class ShapeGrid extends JFrame implements ActionListener
{
    private JButton button;
    int i = 2;
    int j = 3;
    JPanel[][] panelHolder = new JPanel[i][j];
    private Shapes shapes;
    public static void main(String[] args)
    {
        ShapeGrid myGrid = new ShapeGrid();
        myGrid.setSize(800, 800);
        myGrid.createGUI();
        myGrid.setVisible(true);
    }

    public ShapeGrid()
    {
        setLayout(new GridLayout(i,j, 5, 5));   
            for(int m = 0; m < i; m++) {
               for(int n = 0; n < j; n++) {
                  panelHolder[m][n] = new JPanel();
                  add(panelHolder[m][n]);
               }
        }
    }

    private void createGUI()
    {
        shape = new Shapes();

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        button = new JButton("Press me");
        add(button);
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if (ae.getSource() == button) {
            for(int m = 0; m < i; m++) {
               for(int n = 0; n < j; n++) {
                shape.paintComponent(panelHolder[m][n].getGraphics());     
              }
          }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

shape.paintComponent(panelHolder[m][n].getGraphics());    

你不应该直接调用paintCopmonent(),你永远不应该使用getGraphics()。 Swing将确定何时需要绘制组件,Swing会将Graphics对象传递给paintComponent()方法。

一旦您按照以下建议进行操作,就不需要&#34;按我&#34;按钮(或上面的)因为将在创建ShapeGrid类时创建形状。当框架可见时,形状将自动绘制。

  

是在所有面板中使用相同的形状,

您只创建一个Shape对象。您需要为每个网格创建一个Shape对象。因此,在ShapeGrid的构造函数中,您需要为每个网格创建一个Shape对象。

我建议你应该传入你想要的网格行/列(而不是在你的类中硬编码i / j)。所以你的ShapeGrid构造函数代码可能是这样的:

public ShapeGrid(int rows, columns)
{
    setLayout(new GridLayout(rows, columns, 5, 5));   
    int drawShapes = rows * columns;

    for(int i = 0; i < drawShapes; i++) {
        add( new Shape() );
    }
}

那就是它。不需要面板支架。现在,您将在框架中添加唯一的笑脸。

然后在main()方法中执行以下操作:

ShapeGrid myGrid = new ShapeGrid(2, 3);