Reputation: 23
I am basically a novice at using swing to begin with. So bear with me here. I can do simple GUI stuff with premade components in the swing library. However, now it's gotten to the time that I am trying to figure out how to draw basic shapes onto a JPanel. In this case, it is a collection of Square objects that I have recursively gathered, and are supposed to be displayed concentrically around each other.
A few weeks ago we did a smaller project involving drawing shapes, except these shapes were drawn directly onto the JFrame. Now that I am trying to do this in a JPanel or a class that extends JComponent, I am running into way too many stumbling blocks. At this point, nothing is displaying on the JPanel.
Here are the classes I have so far.
Square Class. This just creates a simple Square
public class Square
{
private int x, y, width, height;
private Color theColor;
public Square(int xS, int yS, int widthS, int heightS, Color squareColor)
{
x = xS;
y = yS;
width = widthS;
height = heightS;
theColor = squareColor;
}
public void draw(Graphics2D g2)
{
g2.setColor(theColor);
Rectangle rectDraw = new Rectangle(x,y,width,height);
g2.draw(rectDraw);
}
}
The GUI Class
public class SquareGUI extends JFrame
{
private JComboBox colorChoices, shapeChoices;
private JTextArea numberOfTimes;
private SquarePanel thisPanel;
public SquareGUI()
{
thisPanel = new SquarePanel();
JPanel northPanel = new JPanel(new FlowLayout());
setSize(640, 480);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener listener = new CommandListener();
colorChoices = new JComboBox();
shapeChoices = new JComboBox();
numberOfTimes = new JTextArea(1,3);
colorChoices.addItem("Black");
colorChoices.addItem("Blue");
colorChoices.addItem("Red");
colorChoices.addItem("Green");
shapeChoices.addItem("Square");
shapeChoices.addItem("Circle");
colorChoices.addActionListener(listener);
shapeChoices.addActionListener(listener);
northPanel.add(colorChoices);
northPanel.add(shapeChoices);
northPanel.add(new JLabel("Number of Shapes:"));
northPanel.add(numberOfTimes);
add(northPanel, BorderLayout.NORTH);
add(thisPanel, BorderLayout.CENTER);
setVisible(true);
}
public void addShapesRecursively(int x, int y, int width, int height, int times)
{
if (times == 0) { return; }
Color colorChoice = null;
switch (colorChoices.getSelectedIndex())
{
case 0: colorChoice = Color.BLACK; break;
case 1: colorChoice = Color.BLUE; break;
case 2: colorChoice = Color.RED; break;
case 3: colorChoice = Color.GREEN; break;
}
if (shapeChoices.getSelectedIndex() == 0)
thisPanel.add(new Square(x, y, width, height, colorChoice));
else
System.out.println("todo");
addShapesRecursively(x-15, y-15, width + 15, height + 15, times - 1);
}
class CommandListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
addShapesRecursively(getWidth()/2,getHeight()/2,20,20,Integer.parseInt(numberOfTimes.getText()));
}
}
public static void main(String[]args)
{
new SquareGUI();
}
}
And my JPanel class that is supposed to display the squares.
public class SquarePanel extends JPanel
{
private ArrayList<Square> squareList;
public SquarePanel()
{
setBackground(Color.WHITE);
squareList = new ArrayList<Square>();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < squareList.size(); i++)
{
Square tempSquare = squareList.get(i);
tempSquare.draw(g2);
}
}
public void add(Square addSquare)
{
squareList.add(addSquare);
}
}
I do apologize that there are no comments or such in this yet. Just been pulling my hair out trying to get this to work. I know the recursive bit works, because there are x amount of Square objects in that ArrayList after it runs. It's just the issue of painting these squares on the JPanel.
I tried this first with a separate class that extended JComponent, but could never get the overridden paintComponent to ever fire in it. So I looked around and found out you could override paintComponent in JPanel as well. So it is firing as expected, but nothing is appearing on the JPanel itself.
My overall question is, how do I get the squares to display properly?
Upvotes: 2
Views: 913
Reputation: 347244
You need to encourage the panel to update it self.
Add a call to repaint
in the add
method of the panel...
public void add(Square addSquare) {
squareList.add(addSquare);
repaint();
}
Upvotes: 2