开发者

random rectangles on a JPanel/JPanel

I need to create a program that displays multiple rectangles on a JFrame or JPanel. This is the code I have come up with so far:

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 di开发者_开发技巧splay;
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);
}

}

The above code compiles but there are several flaws. The buttons, when clicked do nothing instead of adding a RectangleComponent object to the frame or panel. I also create a separate RectangleComponent class to draw a rectangle object

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

}

what I have to do is this: Write a program that displays a number of rectangles at random positions. Supply buttons "Fewer" and "More" that generate fewer or more random rectangles. Each time the user clicks on "Fewer", the count should be halved. Each time the user clicks on "More", the count should be doubled.


Your [more] and [fewer] buttons are only setting the number of rectangles, not adding or removing them. You have to recreate them somehow.

If you only want to display rectangles, you don't need to create RectangleComponent - you can override the JFrame's paint calling super.paint and than place and draw rectangles. In such a case, you will have to add repaint() on the jframe in all of your ActionListeners.

EDIT: better idea is to add a jpanel and draw rectangles only on this panel, otherwise the rectangles will be over the buttons and will "disappear" after button clicks, check out this simple demo


I would give my RectangleComponent class an ArrayList of Rectangle, say called rectangleList. I'd give the class two public void methods, addRectangle() and removeRectangle(). In the addRectangle() method, I'd create a random Rectangle (sized so that it fits in the Dimensions of the RectangleComponent) add it to the ArrayList and call repaint. In the removeRectangle, I'd remove a Rectangle if the array list's size is > 0 and again call repaint. In paintComponent I'd iterate through the ArrayList, drawing Rectangles. I'd also have paintComponent call the super method at the start of the method.

Then in your button's actionPerformed methods, you'd call the appropriate method above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜