JPanel / JPanel上的随机矩形

时间:2011-04-05 18:12:40

标签: java swing graphics

我需要创建一个在JFrame或JPanel上显示多个矩形的程序。这是我到目前为止提出的代码:

import javax.swing.*;
import java.util.Random;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class RandomRectangles extends JFrame {
private static final int FRAME_HEIGHT = 300;
private static final int FRAME_WIDTH = 250;

private JButton fewer;
private JButton more;
private Random generator = new Random();
private int count;
private JPanel display;
private JPanel panel;

public RandomRectangles()
{
    count =  generator.nextInt(100);

    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    this.setTitle("Random Rectangles");

    display = new JPanel(new BorderLayout());
    setLayout(new BorderLayout());
    //display = new JPanel(new GridLayout(2,2));
    JPanel buttons = new JPanel();

    fewer = new JButton("Fewer");
    more = new JButton("More");

    buttons.add(fewer);
    buttons.add(more);

    class  fewerNum implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            count = count / 2;
        }
    }

    ActionListener listener = new fewerNum();
    fewer.addActionListener(listener);

    class  moreNum implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            count = count * 2;
        }
    }

    ActionListener listener2 = new moreNum();
    more.addActionListener(listener2);

    for (int i = 0; i <= count; i++)
    {
     // display.add(new RectangleComponent());
       add(new RectangleComponent());
    }


    //add(display, BorderLayout.CENTER);
    add(buttons, BorderLayout.SOUTH);
}

}

上面的代码编译但有几个缺陷。单击按钮时不执行任何操作,而不是将RectangleComponent对象添加到框架或面板。 我还创建了一个单独的RectangleComponent类来绘制一个矩形对象

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class RectangleComponent extends JComponent{

private Random generator = new Random();
private int xLeft;
private int yTop;

public RectangleComponent()
{
    xLeft = generator.nextInt(100);
    yTop = generator.nextInt(100);
}

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    Rectangle rec = new Rectangle(xLeft, yTop, 20, 40);
    int amount = generator.nextInt(100);
    g2.draw(rec);
}

}

我要做的是:编写一个程序,在随机位置显示多个矩形。提供产生更少或更多随机矩形的按钮“更少”和“更多”。每次用户点击“更少”时,计数应减半。每次用户点击“更多”时,计数应加倍。

2 个答案:

答案 0 :(得分:3)

您的[更多]和[更少]按钮仅设置矩形的数量,而不是添加或删除它们。你必须以某种方式重新创建它们。

如果你只想显示矩形,你不需要创建RectangleComponent - 你可以覆盖JFrame的paint调用super.paint而不是放置和绘制矩形。在这种情况下,您必须在所有ActionListeners的jframe上添加repaint()。

编辑:更好的想法是只在这个面板上添加一个jpanel并绘制矩形,否则矩形将在按钮上方并在按钮点击后“消失”,请查看此simple demo

答案 1 :(得分:1)

我会给我的RectangleComponent类一个Rectangle的ArrayList,比如叫做rectangleList。我给了类两个公共void方法,addRectangle()和removeRectangle()。在addRectangle()方法中,我创建了一个随机Rectangle(大小适合RectangleComponent的Dimensions),将它添加到ArrayList并调用repaint。在removeRectangle中,如果数组列表的大小为&gt;,我将删除一个Rectangle。 0再次调用重绘。在paintComponent中,我将遍历ArrayList,绘制矩形。我也让paintComponent在方法的开头调用super方法。

然后在按钮的actionPerformed方法中,您将调用上面的相应方法。