codice:
package gara;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
//import gara.gara.auto.cronometro;
import javax.swing.JLabel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.Label;
import java.awt.Font;
import javax.swing.JProgressBar;
public class gara extends JFrame {
private JPanel contentPane;
private JLabel bottone1;
private JLabel bottone2;
private JLabel bottone3;
private JLabel bottone4;
private JLabel label;
int i=0;
private JLabel cronometro1;
private JLabel cronometro2;
private JLabel cronometro3;
private JLabel cronometro4;
private JList list;
private DefaultListModel lista;
private JProgressBar progressBar1;
private JProgressBar progressBar2;
private JProgressBar progressBar3;
private JProgressBar progressBar4;
private colore colore1;
private colore colore2;
private colore colore3;
private colore colore4;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gara frame = new gara();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public gara() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 866, 652);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
String immagine="http://i68.tinypic.com/2h3mnox.gif";
//String immagine="http://images.clipartpanda.com/car-top-view-clipart-red-racing-car-top-view-fe3a.png";
int y[]=new int[4];
try {
bottone1 =new JLabel("",new ImageIcon(new URL(immagine)),i);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressBar1 = new JProgressBar();
//progressBar1.setForeground(new Color(51, 153, 255));
progressBar1.setMaximum(470);
progressBar1.setBounds(10, 92, 130, 14);
contentPane.add(progressBar1);
progressBar2 = new JProgressBar();
progressBar2.setMaximum(470);
progressBar2.setBounds(10, 222, 130, 14);
contentPane.add(progressBar2);
progressBar3 = new JProgressBar();
progressBar3.setMaximum(470);
progressBar3.setBounds(10, 352, 130, 14);
contentPane.add(progressBar3);
progressBar4 = new JProgressBar();
progressBar4.setMaximum(470);
progressBar4.setBounds(10, 482, 130, 14);
contentPane.add(progressBar4);
bottone1.setBounds(10, 11, 130, 95);
contentPane.add(bottone1);
colore1=new colore(progressBar1);
colore2=new colore(progressBar2);
colore3=new colore(progressBar3);
colore4=new colore(progressBar4);
colore1.start();
colore2.start();
colore3.start();
colore4.start();
try {
bottone2 =new JLabel("",new ImageIcon(new URL(immagine)),i);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bottone2.setBounds(10, bottone1.getY()+130, 130, 95);
contentPane.add(bottone2);
try {
bottone3 =new JLabel("",new ImageIcon(new URL(immagine)),SwingConstants.CENTER);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bottone3.setBounds(10, bottone2.getY()+130, 130, 95);
contentPane.add(bottone3);
try {
bottone4 =new JLabel("",new ImageIcon(new URL(immagine)),SwingConstants.CENTER);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bottone4.setBounds(10, bottone3.getY()+130, 130, 95);
contentPane.add(bottone4);
y[0]=11;
y[1]=141;
y[2]=271;
y[3]=401;
JButton avvia = new JButton("Avvia Gara");
avvia.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
avvia.addMouseListener(new MouseAdapter() {
auto auto1[]=new auto [100];
auto auto2[]=new auto [100];
auto auto3[]=new auto [100];
auto auto4[]=new auto [100];
@Override
public void mouseClicked(MouseEvent arg0) {
lista.clear();
if(i==0){
avvia(i);
i++;
}
else{
if(!auto1[i-1].isAlive() && !auto2[i-1].isAlive() && !auto3[i-1].isAlive() && !auto4[i-1].isAlive() ){
avvia(i);
i++;
}
}
}
public void avvia(int i){
try {
auto1[i-1].reset();
auto2[i-1].reset();
auto3[i-1].reset();
auto4[i-1].reset();
} catch (Exception e) {
e.printStackTrace();
}
auto1[i]=new auto(bottone1,y[0],cronometro1,lista,progressBar1);
auto2[i]=new auto(bottone2,y[1],cronometro2,lista,progressBar2);
auto3[i]=new auto(bottone3,y[2],cronometro3,lista,progressBar3);
auto4[i]=new auto(bottone4,y[3],cronometro4,lista,progressBar4);
auto1[i].start();
auto2[i].start();
auto3[i].start();
auto4[i].start();
auto1[i-1].interrupt();
auto2[i-1].interrupt();
auto3[i-1].interrupt();
auto4[i-1].interrupt();
}
});
avvia.setBounds(318, 533, 146, 69);
contentPane.add(avvia);
String immaginearrivo="https://textureshop.files.wordpress.com/2013/01/chess.jpg";
try {
label = new JLabel("",new ImageIcon(new URL(immaginearrivo)), i);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
label.setBounds(530, 11, 79, 485);
contentPane.add(label);
{
cronometro1 = new JLabel("");
cronometro1.setBounds(649, 11, 130, 95);
contentPane.add(cronometro1);
}
cronometro2 = new JLabel("");
cronometro2.setBounds(649, 141, 130, 95);
contentPane.add(cronometro2);
cronometro3 = new JLabel("");
cronometro3.setBounds(649, 271, 130, 95);
contentPane.add(cronometro3);
cronometro4 = new JLabel("");
cronometro4.setBounds(649, 401, 130, 95);
contentPane.add(cronometro4);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(118, 533, 146, 69);
contentPane.add(scrollPane);
lista=new DefaultListModel();
list = new JList(lista);
scrollPane.setViewportView(list);
Label classifica = new Label("Classifica");
classifica.setFont(new Font("Dialog", Font.PLAIN, 16));
classifica.setBounds(118, 505, 114, 22);
contentPane.add(classifica);
}
public class auto extends Thread{
JLabel bottone = new JLabel();
JProgressBar ProgressBar=new JProgressBar();
int x=10;
int nuovax=x;
int y;
long time;
JLabel cronometro1=new JLabel();
DefaultListModel lista=new DefaultListModel();
long startTime;
public auto(JLabel bottone,int y,JLabel cronometro1,DefaultListModel lista,JProgressBar ProgressBar) {
super();
this.bottone = bottone;
this.y=y;
this.cronometro1=cronometro1;
this.lista=lista;
this.ProgressBar=ProgressBar;
}
int distanza=470;
int spaziopercorso;
Random ran=new Random();
public void run(){
lista.clear();
cronometro1.setText("");
startTime = System.nanoTime();
ProgressBar.setValue(ran.nextInt(470)+ran.nextInt(100)+25);
while(distanza>=0){
spaziopercorso=ran.nextInt(7);
if((ProgressBar.getValue()-spaziopercorso)>=0){
distanza=distanza-spaziopercorso;
nuovax=nuovax+spaziopercorso;
ProgressBar.setValue(ProgressBar.getValue()-spaziopercorso);
bottone.setBounds(nuovax, y, bottone.getWidth(), bottone.getHeight());
ProgressBar.setBounds(nuovax, y, 130, 14);
try {
this.sleep(ran.nextInt(75));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
try {
this.sleep(1250);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<10;i++){
try {
this.sleep(ran.nextInt(300)+50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ProgressBar.setValue(ProgressBar.getValue()+ran.nextInt(25)+10);
}
}
}//
//fine gara
time = (System.nanoTime() - startTime);
cronometro1.setText(""+(double)time/1000000000.0+" S");
int corsia=(y-11)/130+1;
lista.addElement(corsia+"° corsia arrivata!");
}
void reset(){
bottone.setBounds(x, bottone.getY(), bottone.getWidth(), bottone.getHeight());
ProgressBar.setValue(0);
}
}
public class colore extends Thread{
JProgressBar ProgressBar=new JProgressBar();
public colore(JProgressBar progressBar) {
super();
ProgressBar = progressBar;
}
public void run(){
for(int i=0;i<100;i++){
try {
this.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
controllo();
i--;
}
}
private void controllo(){
if(ProgressBar.getValue()>270 && ProgressBar.getValue()<=470){
ProgressBar.setForeground(Color.GREEN);
}
if(ProgressBar.getValue()>250 && ProgressBar.getValue()<=350){
ProgressBar.setForeground(Color.ORANGE);
}
if(ProgressBar.getValue()>100 && ProgressBar.getValue()<=250){
ProgressBar.setForeground(Color.YELLOW);
}
if(ProgressBar.getValue()<100){
ProgressBar.setForeground(Color.RED);
}
}
}
}