necesito ayuda con un programa en java es para mi proyecto final se trata de una carrera de n hilos definidos por el usuario los cuales tienen un avance aleatorio se ejemplifica mejor con el siguiente código:
package hipodromo;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.*;
class Caballo extends Thread
{
JProgressBar caballo;
public Caballo(JProgressBar jp)
{
this.caballo=jp;
}
public void run()
{
int avanse =0;
while (avanse<100)
{
try
{
avanse+=(int)(Math.random()*10);
caballo.setValue(avanse);
sleep((long)(Math.random()*1000));
}
catch (InterruptedException e)
{
}
}
}
}
public class Hipodromo extends JFrame
{
JPanel pista;
JLabel[] nombres=new JLabel[10];
JProgressBar []participantes =new JProgressBar[10];
public Hipodromo()
{
super ("HIPODROMO LAS AMERICAS");
pista =new JPanel();
pista.setLayout(new GridLayout(10,2));
for (int i=0;i<10;i++)
{
nombres=new JLabel("caballo "+i);
pista.add(nombres);
participantes = new JProgressBar();
pista.add(participantes);
}
this.add(pista);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
Hipodromo obj=new Hipodromo();
for(int i=0;i<obj.participantes.length;i++)
{
new Caballo(obj.participantes).start();
}
}
}
la idea es hacerlo y no con jprogress sino con un jlabel o algo similar, y con imágenes espero y me puedan ayudar.
He hecho esto rapido, lo cual requerirá quizás que hagas algunos ajustes:
CaballosForm.java:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CaballosForm {
private JFrame frmCaballos;
private JTextField numeroTxt;
// TODO ...
private HiloCaballo []hiloCaballos = null;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CaballosForm window = new CaballosForm();
window.frmCaballos.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CaballosForm() {
initialize();
}
private void initialize() {
frmCaballos = new JFrame();
frmCaballos.setTitle("Caballos");
frmCaballos.setBounds(100, 100, 450, 300);
frmCaballos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCaballos.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Caballos");
lblNewLabel.setDisplayedMnemonic('C');
lblNewLabel.setBounds(12, 12, 70, 15);
frmCaballos.getContentPane().add(lblNewLabel);
numeroTxt = new JTextField();
lblNewLabel.setLabelFor(numeroTxt);
numeroTxt.setBounds(104, 10, 114, 19);
frmCaballos.getContentPane().add(numeroTxt);
numeroTxt.setColumns(10);
JButton btnNewButton = new JButton("Ok");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int n = Integer.parseInt(numeroTxt.getText());
int y_factor = 39;
hiloCaballos = new HiloCaballo[n];
for(int i = 0; i < n; i++) {
hiloCaballos[i] = new HiloCaballo();
hiloCaballos[i].start();
hiloCaballos[i].getCaballo().setBounds(12, y_factor, 256, 14);
frmCaballos.getContentPane().add(hiloCaballos[i].getCaballo());
y_factor += 15;
}
frmCaballos.revalidate();
frmCaballos.pack();
frmCaballos.setBounds(100, 100, 450, 300);
}
});
btnNewButton.setMnemonic('O');
btnNewButton.setBounds(230, 7, 117, 25);
frmCaballos.getContentPane().add(btnNewButton);
JButton btnStop = new JButton("Stop");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
for(int i = 0; i < Integer.parseInt(numeroTxt.getText()); i++) {
hiloCaballos[i].detenerHilo();
}
}
});
btnStop.setBounds(364, 7, 70, 25);
frmCaballos.getContentPane().add(btnStop);
}
}
HiloCaballo.java:
import javax.swing.JProgressBar;
public class HiloCaballo extends Thread {
private boolean stopFlag = false;
private JProgressBar progressCaballo = null;
public HiloCaballo() {
progressCaballo = new JProgressBar();
progressCaballo.setValue(0);
}
public JProgressBar getCaballo() {
return progressCaballo;
}
@Override
public void run() {
while(!stopFlag) {
progressCaballo.setValue((int)(Math.random() * 100.0));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage());
}
}
}
public void detenerHilo() {
stopFlag = true;
}
}