Graphics g = panel.getGraphics();
is NOT how custom painting works, see Painting in AWT and Swing and
Performing Custom Painting for more details about how painting should be done.
Start by defining a class which is capable of painting a single illusion, with parameters to control the number of circles and size.
public class EhrensteinIllusion {
public EhrensteinIllusion() {
}
public void paint(Graphics2D graphics, int dimeter, int circleCount) {
Graphics2D g2d = (Graphics2D) graphics.create();
g2d.setColor(Color.RED);
g2d.fillOval(0, 0, dimeter, dimeter);
int divisions = dimeter / circleCount;
g2d.setColor(Color.BLACK);
for (int circle = 0; circle < circleCount; circle++) {
int size = divisions * (circle + 1);
int x = (dimeter - size) / 2;
int y = x;
g2d.drawOval(x, y, size, size);
}
g2d.drawLine(dimeter / 2, 0, dimeter, dimeter / 2);
g2d.drawLine(dimeter, dimeter / 2, dimeter / 2, dimeter);
g2d.drawLine(dimeter / 2, dimeter, 0, dimeter / 2);
g2d.drawLine(0, dimeter / 2, dimeter / 2, 0);
g2d.dispose();
}
}
Now, note, I've not specified the x/y position, this is VERY important, as I'm going to use a feature of the Graphics
API to do that instead.
Now, you need some way to be able paint these illusions...
public class IllusionPane extends JPanel {
private EhrensteinIllusion illusion = new EhrensteinIllusion();
public IllusionPane() {
setBackground(Color.DARK_GRAY);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.translate(250, 50);
illusion.paint(g2d, 80, 5);
g2d.dispose();
}
}
This satisfies the third series.
Now, all we need is a simple way to paint a grid. All this does is repeatedly paints a EhrensteinIllusion
at different positions, using the same technique above, by translation the origin point of the Graphics
context
public class EhrensteinIllusionGrid {
public void paint(Graphics2D graphics, int size, int circleCount, int rowsCols) {
Graphics2D g2d = (Graphics2D) graphics.create();
EhrensteinIllusion illusion = new EhrensteinIllusion();
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRect(0, 0, size * rowsCols, size * rowsCols);
for (int row = 0; row < rowsCols; row++) {
for (int col = 0; col < rowsCols; col++) {
Graphics2D gCopy = (Graphics2D) g2d.create();
// New origin point
gCopy.translate(col * size, row * size);
illusion.paint(gCopy, size, circleCount);
gCopy.dispose();
}
}
g2d.dispose();
}
}
Which you can use in the same way as you did the EhrensteinIllusion
class, for example...
public class IllusionPane extends JPanel {
private EhrensteinIllusionGrid grid = new EhrensteinIllusionGrid();
private EhrensteinIllusion illusion = new EhrensteinIllusion();
public IllusionPane() {
setBackground(Color.DARK_GRAY);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.translate(250, 50);
illusion.paint(g2d, 80, 5);
g2d.dispose();
g2d = (Graphics2D) g.create();
g2d.translate(230, 160);
grid.paint(g2d, 50, 5, 4);
g2d.dispose();
}
}
This satisfies the sixth series.
Make sure you have a look at 2D Graphics for more details