Hai ragione , su un timer comunque questo è il main board
[code]package com.gara;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Car car;
private List<Coin> coin;
private List <Block> block;
private boolean ingame;
private final int ICRAFT_X = 200;
private final int ICRAFT_Y = 580;
private final int B_WIDTH = 400;
private final int B_HEIGHT = 600;
private final int DELAY = 50;
public Board() {
initBoard();
}
private void initBoard() {
setFocusable(true);
setBackground(Color.GRAY);
ingame = true;
coin= new ArrayList<>();
block= new ArrayList<>();
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
car = new Car(ICRAFT_X, ICRAFT_Y);
initCoins();
initBlocks();
timer = new Timer(DELAY, this);
timer.start();
}
public void initCoins() {
//crea una moneta e la metta sulla prima riga in posizione casuale
Random randomno = new Random();
int value = randomno.nextInt(385);
coin.add(new Coin(value, 0));
}
public void initBlocks() {
Random randomno = new Random();
int value = randomno.nextInt(385);
block.add(new Block(value, 0));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (ingame) {
drawObjects(g);
} else {
drawGameOver(g);
}
Toolkit.getDefaultToolkit().sync();
}
private void drawObjects(Graphics g) {
if (car.isVisible()) {
g.drawImage(car.getImage(), car.getX(), car.getY(),
this);
}
for (Coin coin : coin) {
if (coin.isVisible()) {
g.drawImage(coin.getImage(), coin.getX(), coin.getY(), this);
}
}
for (Block block : block) {
if (block.isVisible()) {
g.drawImage(block.getImage(), block.getX(), block.getY(), this);}
}}
private void drawGameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics fm = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - fm.stringWidth(msg)) / 2,
B_HEIGHT / 2);
}
@Override
public void actionPerformed(ActionEvent e) {
inGame();
updateCar();
updateBlocks();
updateCoins();
checkCollisions();
repaint();
initCoins();
initBlocks();
}
private void inGame() {
if (!ingame) {
timer.stop();
}
}
private void updateCar() {
if (car.isVisible()) {
car.up();
}
}
private void updateCoins() {
for (int i = 0; i < coin.size(); i++) {
Coin a = coin.get(i);
if (a.isVisible()) {
a.down(); // tutte le monete vengono spostate verso il basso
Random randomno = new Random();
boolean value = randomno.nextBoolean(); //spostate a destra o sinistra in maniera casuale
if (value==true && a.x>10) {
a.left();
}
else if(a.y<385)
{
a.right();
}
} else {
coin.remove(i);
}
}
}
private void updateBlocks() {
for (int i = 0; i < block.size(); i++) {
Block b = block.get(i);
if (b.isVisible()) {
b.down();
Random randomno = new Random();
boolean value = randomno.nextBoolean();
if (value==true) {
b.left();
}
else {
b.right();
}
} else {
block.remove(i);
}
}
}
public void checkCollisions() {
Rectangle r1 = car.getBounds();
for (Coin coin : coin) {
Rectangle r2 = coin.getBounds();
if (r1.intersects(r2)) {
coin.setVisible(false);
}
}
for (Block block : block) {
Rectangle r3 = block.getBounds();
if (r1.intersects(r3)) {
block.setVisible(false);
}
}
}
}
[/code]