Reputation: 7
What the code should do is draw X randomized shapes with the click of a button. What I have so far is a subclass that creates the random shape to be placed inside the JPanels, but the issue is that the same shape is used in all panels, I need each shape to be randomized.
The subclass looks like this:
public class Shapes extends JPanel
{
Random rand = new Random();
private int x = 5;
private int y = 5;
private int diameter = 200;
private Color outline;
private Color internal;
private Color internal2;
private Color internal3;
public Shapes() {
this(new Random());
}
//randomizes colors
public Shapes(Random rand) {
outline = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
internal = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
internal2 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
internal3 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
}
{
super.paintComponent(g);
g.setColor(outline);
g.drawOval(x, y, diameter, diameter);
g.setColor(internal);
g.fillOval(x+2, y+2, diameter-4, diameter-4);
g.setColor(internal2);
g.fillOval(x+25, y+66, diameter/3, diameter/3);
g.fillOval(x+125, y+66, diameter/3, diameter/3);
g.setColor(internal3);
g.fillArc(x+55, y+105, diameter/3, diameter/3, 180, 180);
}
}
While the main class looks like so [currently set up to make 6 images]:
public class ShapeGrid extends JFrame implements ActionListener
{
private JButton button;
int i = 2;
int j = 3;
JPanel[][] panelHolder = new JPanel[i][j];
private Shapes shapes;
public static void main(String[] args)
{
ShapeGrid myGrid = new ShapeGrid();
myGrid.setSize(800, 800);
myGrid.createGUI();
myGrid.setVisible(true);
}
public ShapeGrid()
{
setLayout(new GridLayout(i,j, 5, 5));
for(int m = 0; m < i; m++) {
for(int n = 0; n < j; n++) {
panelHolder[m][n] = new JPanel();
add(panelHolder[m][n]);
}
}
}
private void createGUI()
{
shape = new Shapes();
setDefaultCloseOperation(EXIT_ON_CLOSE);
button = new JButton("Press me");
add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == button) {
for(int m = 0; m < i; m++) {
for(int n = 0; n < j; n++) {
shape.paintComponent(panelHolder[m][n].getGraphics());
}
}
}
}
}
Upvotes: 0
Views: 289
Reputation: 324108
shape.paintComponent(panelHolder[m][n].getGraphics());
You should NEVER invoke paintCopmonent() directly and you should never use getGraphics(). Swing will determine when a component needs to be painted and Swing will pass the Graphics object to the paintComponent() method.
Once you follow the advice below there is no need for the "Press Me" button (or the above) because the Shapes will be created when the when the ShapeGrid class is created. And the Shapes will automatically be painted when the frame is made visible.
is that the same shape is used in all panels,
You only ever create a single Shape object. You need to create a Shape object for each grid. So in the constructor of the ShapeGrid you need to create one Shape object for each grid.
I suggest you should pass in the rows/columns you want for your grid (instead of hardcoding i/j in your class). So your ShapeGrid constructor code might be something like:
public ShapeGrid(int rows, columns)
{
setLayout(new GridLayout(rows, columns, 5, 5));
int drawShapes = rows * columns;
for(int i = 0; i < drawShapes; i++) {
add( new Shape() );
}
}
That's it. There is no need for the panel holder. You will now have uniques Smileys added to the frame.
Then in your main() method you do something like:
ShapeGrid myGrid = new ShapeGrid(2, 3);
Upvotes: 1