I am trying to draw a filled rectangle whose color corresponds to the checkbox. The rectangle is to be randomly sized and placed on the JPanel
. So far, I have gotten the rectangle to draw to the JPanel
with a random size and random location, but I am having trouble with drawing a rectangle when a checkbos is selected. The code in my ActionListener
serves no purpose to the running of my project and I have tried other things, but am having trouble.
package colorViewerCheck;
public class ColorViewerCheckFrame extends JFrame
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
private JPanel colorPanel;
private JCheckBox redCheckBox;
private JCheckBox greenCheckBox;
private JCheckBox blueCheckBox;
public ColorViewerCheckFrame()
{
createColorPanel();
createControlPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createControlPanel()
{
class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (redCheckBox.isSelected()) {createColorPanel();}
if (greenCheckBox.isSelected()) {createColorPanel();}
if (blueCheckBox.isSelected()) {createColorPanel();}
}
}
ActionListener listener = new ColorListener();
redCheckBox = new JCheckBox("Red");
greenCheckBox = new JCheckBox("Green");
blueCheckBox = new JCheckBox("Blue");
redCheckBox.setSelected(true);
JPanel controlPanel = new JPanel();
controlPanel.add(redCheckBox);
controlPanel.add(greenCheckBox);
controlPanel.add(blueCheckBox);
redCheckBox.addActionListener(listener);
greenCheckBox.addActionListener(listener);
blueCheckBox.addActionListener(listener);
add(controlPanel, BorderLayout.SOUTH);
}
public void createColorPanel()
{
class RectangleComponent extends JComponent
{
private int x;
private int y;
private int width;
private int height;
private Random rand;
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
rand = new Random();
x = rand.nextInt(250);
y = rand.nextInt(350);
width = rand.nextInt(50);
height = rand.nextInt(50);
Rectangle rect = new Rectangle(x, y, width, height);
g2.setColor(getColor());
g2.fill(rect);
g2.draw(rect);
}
}
colorPanel = new JPanel(new GridLayout());
RectangleComponent rect = new RectangleComponent();
colorPanel.add(rect);
add(colorPanel, BorderLayout.CENTER);
colorPanel.setVisible(true);
}
public Color getColor()
{
Color color = null;
if (redCheckBox.isSelected()) {color = Color.RED;}
else if (greenCheckBox.isSelected()) {color = Color.GREEN;}
else if (blueCheckBox.isSelected()) {color = Color.blue;}
return color;
}
}
Instead of trying to create a new
JComponent
each time, simply create a simple drawing surface which is capable of adding new "rectangles" and painting them onto itselfBorderLayout
can only display a single component within in each of it's five available positions...Updated...
Before you go to much further, you're going to need to have a look at...
Basically, you want to create a single, custom component, extending from something like
JPanel
, which is responsible for painting the rectangles.Next, you'll need some kind of concept of a "rectangle", which knows it's position, size and color.
Then, when the user selects a color item, you want to create a "rectangle" and "add" it to the drawing container, for example...