[b]Voltei com o programinha ja pronto, porem existem algumas funcionalidades que nao estao muito boas ou nao funcionam.
Ps: Não assutem com o tamanho do codigo
`
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
public class TP_Joguinho_Thread extends javax.swing.JFrame {
int voltar_posicao_zero = 0;
/** Creates new form TP_Joguinho_Thread */
public TP_Joguinho_Thread() {
initComponents();
setSize(1300, 500);
Iniciar_desenho();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
botao1 = new javax.swing.JToggleButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jSeparator2 = new javax.swing.JSeparator();
jSeparator3 = new javax.swing.JSeparator();
jSeparator4 = new javax.swing.JSeparator();
jLabel7 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
botao1.setText("INICIAR");
botao1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botao1ActionPerformed(evt);
}
});
getContentPane().add(botao1);
botao1.setBounds(0, 340, 90, 23);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Carro 1");
jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel1.setPreferredSize(new java.awt.Dimension(80, 50));
getContentPane().add(jLabel1);
jLabel1.setBounds(10, 11, 80, 50);
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Carro2");
jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(jLabel2);
jLabel2.setBounds(10, 100, 80, 50);
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("Carro 3");
jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(jLabel3);
jLabel3.setBounds(10, 190, 80, 50);
jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel4.setText("Carro 4");
jLabel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 280, 80, 50);
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(jLabel5);
jLabel5.setBounds(880, 0, 20, 420);
jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(jLabel6);
jLabel6.setBounds(90, 0, 20, 420);
getContentPane().add(jSeparator1);
jSeparator1.setBounds(0, 340, 1000, 2);
getContentPane().add(jSeparator2);
jSeparator2.setBounds(0, 260, 1000, 2);
getContentPane().add(jSeparator3);
jSeparator3.setBounds(0, 170, 1000, 2);
getContentPane().add(jSeparator4);
jSeparator4.setBounds(0, 80, 1000, 2);
jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(jLabel7);
jLabel7.setBounds(480, 350, 50, 70);
jButton1.setText("VOLTAR");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(0, 360, 90, 23);
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(1010, 90, 240, 170);
jButton2.setText("SAIR");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(0, 380, 90, 23);
pack();
}// </editor-fold>
private void botao1ActionPerformed(java.awt.event.ActionEvent evt) {
mover();
}
//==================================================
//==================================================
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// VOLTAR
jLabel1.setLocation(10, 11);
jLabel2.setLocation(10, 100);
jLabel3.setLocation(10, 190);
jLabel4.setLocation(10, 280);
jTextArea1.setText(null);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// SAIR
}
//============================================================
//============================================================
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TP_Joguinho_Thread().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JToggleButton botao1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JSeparator jSeparator4;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
public void mover() {
car1.start();
car2.start();
car3.start();
car4.start();
}
Thread car1 = new Thread() {
@Override
public void run() {
boolean entrou = false;
boolean reabasteceu = false;
boolean thread = false;
while (!thread) {
int latencia = (int) (30 + Math.random() * 25);
int velocidade = (int) (1 + Math.random() * 4);
jLabel1.setLocation((jLabel1.getX() + velocidade), jLabel1.getY());
//verificar o vencedor
if (jLabel1.getX() >= 800 && !entrou) {
jTextArea1.setText(jTextArea1.getText() + "\n" + "Carro 1");
entrou = true;
}
//parada para gasolina
if (jLabel1.getX() >= 450 && !reabasteceu) {
jTextArea1.setText(jTextArea1.getText() + "\n" + "PIT-STOP 1");
reabasteceu = true;
try {
sleep((int) (1000 + Math.random() * 5000));
} catch (InterruptedException ex) {
Logger.getLogger(TP_Joguinho_Thread.class.getName()).log(Level.SEVERE, null, ex);
}
}
//execucao thread
try {
sleep(latencia);
} catch (InterruptedException ex) {
Logger.getLogger(TP_Joguinho_Thread.class.getName()).log(Level.SEVERE, null, ex);
}
if (jLabel1.getX() >= 900) {
car1.stop();
}
}
}
};
Thread car2 = new Thread() {
@Override
public void run() {
boolean entrou = false;
boolean reabasteceu = false;
boolean thread = false;
while (!thread) {
int latencia = (int) (30 + Math.random() * 25);
int velocidade = (int) (1 + Math.random() * 4);
jLabel2.setLocation((jLabel2.getX() + velocidade), jLabel2.getY());
//verificar o vencedor
if (jLabel2.getX() >= 800 && !entrou) {
jTextArea1.setText(jTextArea1.getText() + "\n" + "Carro 2");
entrou = true;
}
//parada para gasolina
if (jLabel2.getX() >= 450 && !reabasteceu) {
jTextArea1.setText(jTextArea1.getText() + "\n" + "PIT-STOP 2");
reabasteceu = true;
try {
sleep((int) (1000 + Math.random() * 5000));
} catch (InterruptedException ex) {
Logger.getLogger(TP_Joguinho_Thread.class.getName()).log(Level.SEVERE, null, ex);
}
}
//execucao thread
try {
sleep(latencia);
} catch (InterruptedException ex) {
Logger.getLogger(TP_Joguinho_Thread.class.getName()).log(Level.SEVERE, null, ex);
}
if (jLabel2.getX() >= 900) {
car2.stop();
}
}
}
};
Thread car3 = new Thread() {
@Override
public void run() {
boolean entrou = false;
// boolean reabasteceu = false;
boolean thread = false;
while (!thread) {
int latencia = (int) (30 + Math.random() * 25);
int velocidade = (int) (1 + Math.random() * 3.1);
jLabel3.setLocation((jLabel3.getX() + velocidade), jLabel3.getY());
//verificar o vencedor
if (jLabel3.getX() >= 800 && !entrou) {
jTextArea1.setText(jTextArea1.getText() + "\n" + "Carro 3");
entrou = true;
}
//parada para gasolina
// if (jLabel3.getX() >= 450 && !reabasteceu) {
// System.out.println("PIT-STOP 3 ");
// reabasteceu = true;
// try {
// sleep((int) (1000 + Math.random() * 3000));
// } catch (InterruptedException ex) {
// Logger.getLogger(TP_Joguinho_Thread.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
//execucao thread
try {
sleep(latencia);
} catch (InterruptedException ex) {
Logger.getLogger(TP_Joguinho_Thread.class.getName()).log(Level.SEVERE, null, ex);
}
if (jLabel3.getX() >= 900) {
car3.stop();
}
}
}
};
Thread car4 = new Thread() {
@Override
public void run() {
boolean entrou = false;
boolean reabasteceu = false;
boolean thread = false;
while (!thread) {
int latencia = (int) (30 + Math.random() * 25);
int velocidade = (int) (1 + Math.random() * 4);
jLabel4.setLocation((jLabel4.getX() + velocidade), jLabel4.getY());
//verificar o vencedor
if (jLabel4.getX() >= 800 && !entrou) {
jTextArea1.setText(jTextArea1.getText() + "\n" + "Carro 4");
entrou = true;
}
//parada para gasolina
if (jLabel4.getX() >= 450 && !reabasteceu) {
jTextArea1.setText(jTextArea1.getText() + "\n" + "PIT-STOP 4");
reabasteceu = true;
try {
sleep((int) (1000 + Math.random() * 5000));
} catch (InterruptedException ex) {
Logger.getLogger(TP_Joguinho_Thread.class.getName()).log(Level.SEVERE, null, ex);
}
}
//execu'cao da thread
try {
sleep(latencia);
} catch (InterruptedException ex) {
Logger.getLogger(TP_Joguinho_Thread.class.getName()).log(Level.SEVERE, null, ex);
}
if (jLabel4.getX() >= 900) {
car4.stop();
}
}
}
};
private void Iniciar_desenho() {
jLabel1.setIcon(new ImageIcon("c1.gif"));
jLabel2.setIcon(new ImageIcon("c2.jpe"));
jLabel3.setIcon(new ImageIcon("c3.jpe"));
jLabel4.setIcon(new ImageIcon("c4.jpe"));
jLabel5.setIcon(new ImageIcon("linha.gif"));
jLabel6.setIcon(new ImageIcon("linha.gif"));
jLabel7.setIcon(new ImageIcon("gasolina.jpe"));
}
}
Meu problema é:
1º Existem dois metodos que eu nao sei como implementa-los, pois todas implementações as quais eu testei o algoritmo nao funcionava, ou dava erro
—> voltar os carros e jogar novamente
2º Gostaria de saber como posso melhorar o visual do meu codigo, como por exemplo, serar em classes, eu outros padores em JAVA.