public Example01() {
add(paintPanel);
timer.start();
}
class BeanMachinePanel extends JPanel {
final static int HGAP = 20;
final static int VGAP = 20;
final static int RADIUS = 5;
final static int LENGTH_OF_SLOTS = 40;
final static int LENGTH_OF_OPENNING = 15;
final static int Y_FOR_FIRST_NAIL = 50;
final static int RED_BALL_START_Y = Y_FOR_FIRST_NAIL - RADIUS;
private int yRed = RED_BALL_START_Y;
private boolean hideRedBall = false;
public void hideRedBall() {
hideRedBall = true;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int y = Y_FOR_FIRST_NAIL;
int xCenter = getWidth() / 2;
if (!hideRedBall) {
g.setColor(Color.RED);
int xRed = xCenter + shift;
g.fillOval(xRed - RADIUS, yRed - RADIUS, 2 * RADIUS, 2 * RADIUS);
}
g.setColor(Color.GREEN);
for (int i = 0; i < NUMBER_OF_ROWS; i++) {
y += VGAP;
for (int k = 0; k <= i; k++) {
g.fillOval(xCenter - i * HGAP / 2 + k * HGAP - RADIUS, y - RADIUS, 2 * RADIUS, 2 *
RADIUS);
}
}
g.setColor(Color.BLACK);
y = y + RADIUS;
for (int i = 0; i < NUMBER_OF_SLOTS; i++) {
int x = xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 + (i - 1) * HGAP;
g.drawLine(x, y, x, y + LENGTH_OF_SLOTS);
}