Buonasera,
Sto credando un programma per fare una gara tra 4 auto utilizzando i thread.
solo che mi da un errore nel codice e non so come risolverlo
lerrore e in questa istruzione:
probabilmente sara anche nelle 3 istruzioni successive ho provato a usare il final ma nullacodice:auto auto1=new auto(bottone1,y[0]);
Grzie a prestocodice: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 javax.swing.JButton; import javax.swing.ImageIcon; 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; public class gara extends JFrame { private JPanel contentPane; /** * Launch the application. */ 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(); } } }); } /** * Create the frame. */ 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://images.clipartpanda.com/car-top-view-clipart-red-racing-car-top-view-fe3a.png"; int y[]=new int[4]; JButton bottone1 = null; try { bottone1 =new JButton("Regular",new ImageIcon(new URL(immagine))); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } bottone1.setBounds(10, 11, 130, 95); contentPane.add(bottone1); JButton bottone2 = null; try { bottone2 =new JButton("Regular",new ImageIcon(new URL(immagine))); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } bottone2.setBounds(10, bottone1.getY()+130, 130, 95); contentPane.add(bottone2); JButton bottone3 = null; try { bottone3 =new JButton("Regular",new ImageIcon(new URL(immagine))); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } bottone3.setBounds(10, bottone2.getY()+130, 130, 95); contentPane.add(bottone3); JButton bottone4 = null; try { bottone4 =new JButton("Regular",new ImageIcon(new URL(immagine))); } 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(bottone1,y[0]); auto auto2=new auto(bottone2,y[1]); auto auto3=new auto(bottone3,y[2]); auto auto4=new auto(bottone4,y[3]); @Override public void mouseClicked(MouseEvent arg0) { } }); avvia.setBounds(318, 533, 146, 69); contentPane.add(avvia); } public class auto extends Thread{ JButton bottone = new JButton(); int x=10; int nuovax=x; int y; public auto(JButton bottone,int y) { super(); this.bottone = bottone; this.y=y; } int distanza=470; int spaziopercorso; Random ran=new Random(); public void run(){ while(distanza>=0){ spaziopercorso=ran.nextInt(7); distanza=distanza-spaziopercorso; nuovax=nuovax+spaziopercorso; bottone.setBounds(nuovax, y, bottone.getWidth(), bottone.getHeight()); try { this.sleep(5); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //fine gara } void reset(){ bottone.setBounds(x, bottone.getY(), bottone.getWidth(), bottone.getHeight()); } } }

Rispondi quotando

