Qual seria melhor solução pro meu caso??Este é o meu projeto
package NovoObj;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class NovoObj{
private final JLabel garrafa = new JLabel(new ImageIcon(getClass().getResource("garrafa.png")));
private final JLabel jornal = new JLabel(new ImageIcon(getClass().getResource("jornal.png")));
private final ArrayList<JLabel> lista=new ArrayList<JLabel>();
int posX=300;
int posY=0;
public NovoObj(){
garrafa.setBounds(posX, posY, 33, 113);
jornal.setBounds(posX, posY, 106, 77);
lista.add(garrafa);
lista.add(jornal);
}
public JLabel sorteiaObj(){
Random r=new Random();
int x=r.nextInt(2);
return lista.get(x);
}
public boolean Colisao(Component a, Component b) {
int aX = a.getX();
int aY = a.getY();
int ladoDireitoA = aX + a.getWidth();
int ladoEsquerdoA = aX;
int ladoBaixoA = aY + a.getHeight();
int ladoCimaA = aY;
int bX = b.getX();
int bY = b.getY();
int ladoDireitoB = bX + b.getWidth();
int ladoEsquerdoB = bX;
int ladoBaixoB = bY + b.getHeight();
int ladoCimaB = bY;
boolean colidiu = false;
boolean cDireita = false;
boolean cCima = false;
boolean cBaixo = false;
boolean cEsquerda = false;
if (ladoDireitoA >= ladoEsquerdoB) {
cDireita = true;
}
if (ladoCimaA <= ladoBaixoB) {
cCima = true;
}
if (ladoBaixoA >= ladoCimaB) {
cBaixo = true;
}
if (ladoEsquerdoA <= ladoDireitoB) {
cEsquerda = true;
}
if (cDireita && cEsquerda && cBaixo && cCima) {
colidiu = true;
}
return colidiu;
}
}
package Apresentacao;
import NovoObj.NovoObj;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static java.lang.Thread.sleep;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//---------------------------------------------------Segunda classe
public class DesignTela extends JFrame {
private final ImageIcon fundo = new ImageIcon(getClass().getResource("fundoparque.png"));
private final JLabel cestoVid = new JLabel(new ImageIcon(getClass().getResource("cestovid.png")));
private final JLabel cestoPla = new JLabel(new ImageIcon(getClass().getResource("cestoplas.png")));
private final JLabel cestoMet = new JLabel(new ImageIcon(getClass().getResource("cestomet.png")));
private final JLabel cestoPap = new JLabel(new ImageIcon(getClass().getResource("cestopap.png")));
private final Panel painel = new Panel();
private final NovoObj objAtual = new NovoObj();
private final Cair desce = new Cair();
private JLabel objEscolhido = new JLabel();
private static int posX = 300;
private static int posY = 0;
//pontuação
private static int pontuacao = 0;
private final JLabel pontos = new JLabel("Pontos:" + pontuacao);
public DesignTela() {
super("Jogo Ambiental");
pontos.setForeground(Color.red);
pontos.setFont(new Font("Arial", Font.BOLD, 20));
//posicionamento dos elementos
pontos.setBounds(0, -50, 150, 150);
cestoVid.setBounds(490, 320, 115, 140);
cestoPla.setBounds(360, 320, 113, 141);
cestoMet.setBounds(230, 320, 117, 142);
cestoPap.setBounds(100, 320, 112, 141);
this.add(painel);
painel.setLayout(null);
painel.add(pontos);
painel.add(cestoVid);
painel.add(cestoPla);
painel.add(cestoMet);
painel.add(cestoPap);
addObjTela();
Toolkit tKit = getToolkit();
Dimension dim = tKit.getScreenSize();
this.setBounds(dim.width / 2 - 350, dim.height / 2 - 250, 700, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.Movimento();
desce.start();
}
public class Panel extends JPanel {
public void paintComponent(Graphics g) { //define o background do painel
super.paintComponent(g);
Image img = fundo.getImage();
g.drawImage(img, 0, 0, this);
}
}
public void addObjTela() {
objEscolhido = objAtual.sorteiaObj();
painel.add(objEscolhido);
}
public class Cair extends Thread {
public void run() {
while (true) {
try {
sleep(25); //atualizar jlabel após 25ms
} catch (Exception erro) {
System.out.println("Erro!");
}
objEscolhido.setBounds(posX, objEscolhido.getY() + 2, objEscolhido.getWidth(), objEscolhido.getHeight());
if (objAtual.Colisao(objEscolhido, cestoVid)) {
pontuacao++;
pontos.setText("Pontos:" + pontuacao);
posX = 300;
posY = 0;
painel.remove(objEscolhido);
addObjTela();
objEscolhido.setBounds(posX, posY, objEscolhido.getWidth(), objEscolhido.getHeight());
}
}
}
}
public void Movimento() {
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37) { //esquerda
posX -= 10;
}
if (e.getKeyCode() == 39) { //direita
posX += 10;
}
objEscolhido.setBounds(objEscolhido.getX(), objEscolhido.getY(), objEscolhido.getWidth(), objEscolhido.getHeight());
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
public static void main(String[] args) {
DesignTela janela = new DesignTela();
janela.setVisible(true);
}
}