I am attempting to draw multiple shapes in the contentPane of a JInternalFrame. When a button is clicked, the program should determine which checkboxes are selected, and add the appropriate shapes. If multiple shapes are selected, it should add all of them.
Currently the program can draw any of the shapes. The problem is that on click, only one shape is being drawn when I have multiple check boxes selected, and I can't figure out why. (Example: Yellow and blue JCheckboxes are selected, it only draws the yellow shape, when it should draw both) Please help me get the program to draw more than one shape on click!
Here is some code:
ActionListener that is to handle the adding of shapes:
class addListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (redBox.isSelected())
{
x = createRNG(RNG_MAX,RNG_MIN);//so the shapes starting location is random
y = createRNG(RNG_MAX,RNG_MIN);
rShape = new redShape(x,y);
contentPane.add(rShape);//,BorderLayout.CENTER);
}
if (blueBox.isSelected())
{
x = createRNG(RNG_MAX,RNG_MIN);
y = createRNG(RNG_MAX,RNG_MIN);
bShape = new bShape(x,y);
//internalFrame.getContentPane().add(bShape);
contentPane.add(bShape);//BorderLayout.CENTER);
}
if (yellowBox.isSelected())
{
x = createRNG(RNG_MAX,RNG_MIN);
y = createRNG(RNG_MAX,RNG_MIN);
yShape = new yShape(x,y);
//internalFrame.getContentPane().add(yShape);
contentPane.add(yShape);//,BorderLayout.CENTER);
}
internalFrame.repaint();
internalFrame.revalidate();
}
}
The code of one of the shape classes. The code of these classes are all basically the same, just different shapes being drawn.
import javax.swing.*;
import java.awt.*;
public class redShape extends coloredShapes
{
private static final int RECTANGLE_WIDTH = 80;
private static final int RECTANGLE_HEIGHT = 20;
private int xLeft;
private int yTop;
public redShape(int x, int y)
{
xLeft = x;
yTop = y;
}
public void paintComponent(Graphics g)
{
//draws 1 large rectangle
g.setColor(Color.red);
g.fillRect(xLeft,yTop,RECTANGLE_WIDTH,RECTANGLE_HEIGHT);
//Divides the rectangle into parts
g.setColor(Color.black);
g.drawLine(xLeft, yTop,xLeft+80,yTop);
g.drawLine(xLeft,yTop,xLeft,yTop+20);
g.drawLine(xLeft,yTop+20,xLeft+80,yTop+20);
g.drawLine(xLeft+80,yTop+20,xLeft+80,yTop);
g.drawLine(xLeft+20,yTop,xLeft+20,yTop+20);
g.drawLine(xLeft+40,yTop,xLeft+40,yTop+20);
g.drawLine(xLeft+60,yTop,xLeft+60,yTop+20);
}
public void moveComponentBy(int dx, int dy)
{
if (yTop < 450)
{
xLeft += dx;
yTop += dy;
if (xLeft < 0)
{
xLeft = 0;
}
if (xLeft > 500)
{
xLeft = 500;
}
repaint();
}
}
public int getYTop()
{
return yTop;
}
public int getXLeft()
{
return xLeft;
}
}
This is the code I thought would be relevant, if any further code is needed to determine the issue, please ask and I will provide it!
EDIT: coloredShapes abstract class, as requested.
import java.util.*;
import javax.swing.*;
abstract public class coloredShapes extends JComponent
{
private static final int RECTANGLE_WIDTH = 10;
private static final int RECTANGLE_HEIGHT = 10;
private int xLeft;
private int yTop;
public coloredShapes()
{
xLeft = 0;
yTop = 0;
}
public abstract void moveComponentBy(int dx, int dy);
public abstract int getYTop();
public abstract int getXLeft();
}