Im suck on what todo next, problems problems and more problems
I am trying to create a horse race with 5 horses but a minimum of 3 are required to race. The horses race along a green track and set off when the start button is pressed. Each horse is represented by a small red rectangle and when the race starts the horses run up the track in various speeds. Once a horse reaches the end all horse movement should stop and the name of the winner then appears. Theres also a recet button that resets all graphics and text fields so that a new race may be started.
I have created my horse class, my array of horses from this class, I have put in all my action listeners for my buttons and have attempted the rest without much luck. I am trying to create if and while statements so that if a name is put in the textfield the actionlistener gets the text if there is text and creates the horse in the corresponding lane on screen.
Any help you can provide would be very much appreciated. Also ignore my first few comments I was messing about trying to do different stuff and commented it out. Heres my code for the class horse and the class race.
import java.awt.*;
publicclass Horse{
privateint laneNo;
private String name;
privateint xPosition;// current position for drawing the 慔orse?/font>
privateint minSpeed;// a random number between 3 and 5 inclusive
private Graphics g;
public Horse(String myName,int lane, Graphics myG){
// code here to initialise name, laneNo, xPosition, g, minSpeed
name = myName;
laneNo = lane;
minSpeed = 4;
xPosition = 0;
g = myG;
}
privateint randomVal(int min,int max){// returns a random int
// in range min to max
return ((int)(Math.random()*10))%(max-min+1)+min;
}
publicint getPosition(){
return laneNo;
//
}
public String getName(){
return name;
//
}
//public Graphics getMyG(){
//g.drawRect(10, 10, 10, 10);
//return g;
// your code here
//}
publicvoid move(){// sets the Horse抯 new position
xPosition+=minSpeed+randomVal(1,3);
}
publicvoid draw(){// code here to draw the red 慔orse?in the correct lane
g.drawRect(xPosition, laneNo, 10, 10);
g.setColor(Color.red);
}
}// end of horse class xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
publicclass Raceextends Appletimplements ActionListener{
private TextFieldname1, name2, name3, name4, name5, resultField;
private Label laneLabel1, laneLabel2, laneLabel3, laneLabel4, laneLabel5, resultLabel;
private Button startButton, newRace;
private Graphics gReset;
private Horse horseArray[];
int noHorses;
int winner;
boolean raceStarted;
public Graphics g = getGraphics();
//public void setBackground( Color param_1);
//public Component add( Component param_1);
//public RaceApplet()
//public void setSize(int , int );
//public void Component(componant param_1){
//add( Component param_1);
publicvoid init()
{//initializer
this.setSize(800, 800);
this.setBackground(Color.cyan);
//sets colour of background
laneLabel1 =new Label("Lane 1.Horse name: ");this.add(this.laneLabel1);
name1 =new TextField(3);add(name1);name1.addActionListener(this);
laneLabel2 =new Label("Lane 2.Horse name: ");this.add(this.laneLabel2);
name2 =new TextField(3);add(name2);name2.addActionListener(this);
laneLabel3 =new Label("Lane 3.Horse name: ");this.add(this.laneLabel3);
name3 =new TextField(3);add(name3);name3.addActionListener(this);
laneLabel4 =new Label("Lane 4.Horse name: ");this.add(this.laneLabel4);
name4 =new TextField(3);add(name4);name4.addActionListener(this);
laneLabel5 =new Label("Lane 5.Horse name: ");this.add(this.laneLabel5);
name5 =new TextField(3);add(name5);name5.addActionListener(this);
setSize(300, 500);
resultLabel =new Label("The winner is: ");this.add(this.resultLabel);
resultField =new TextField(20);this.add(this.resultField);
startButton =new Button("Start the Race");this.add(this.startButton);this.startButton.addActionListener(this);
newRace =new Button("New Race");this.add(this.newRace);this.newRace.addActionListener(this);
}//close initializer
publicvoid paint( Graphics g1)
{//opens paint
g1.setColor(Color.GREEN);
g1.fillRect(50, 200, 500, 100);
g1.setColor(Color.BLACK);
g1.drawString("1", 40, 215);
g1.drawLine(50, 200, 550, 200);
g1.drawString("2", 40, 235);
g1.drawLine(50, 220, 550, 220);
g1.drawString("3", 40, 255);
g1.drawLine(50, 240, 550, 240);
g1.drawString("4", 40, 275);
g1.drawLine(50, 260, 550, 260);
g1.drawString("5", 40, 295);
g1.drawLine(50, 280, 550, 280);
g1.drawLine(50, 300, 550, 300);
}//closes paint
publicvoid
actionPerformed(ActionEvent theEvent)
{//opens actionevent
if (theEvent.getSource() == this.name1)
{
String nameinput;
nameinput = this.name1.getText();
if (nameinput.equals(""))
{
if (nameinput.equals(" ")){}
else//{}
{
this.horseArray[0].draw();
noHorses = this.noHorses + 1;
this.resultField.setText("");
if (theEvent.getSource() == this.name2)
{
nameinput = this.name2.getText();
if (nameinput.equals(""))
{
if (nameinput.equals(" ")){}
else//()
{
this.horseArray[1].draw();
noHorses = this.noHorses + 1;
this.resultField.setText("");
if (theEvent.getSource() == this.name3)
{
nameinput = this.name3.getText();
if (nameinput.equals(""))
{
if (nameinput.equals(" ")){}
else//()
{
this.horseArray[2].draw();
noHorses = this.noHorses + 1;
this.resultField.setText("");
if (theEvent.getSource() == this.name4)
{
nameinput = this.name4.getText();
if (nameinput.equals(""))
{
if (nameinput.equals(" ")){}
else//()
{
this.horseArray[3].draw();
noHorses = this.noHorses + 1;
this.resultField.setText("");
if (theEvent.getSource() == this.name5)
{
nameinput = this.name5.getText();
if (nameinput.equals(""))
{
if (nameinput.equals(" ")){}
else//()
{
this.horseArray[4].draw();
noHorses = this.noHorses + 1;
this.resultField.setText("");
if (theEvent.getSource() == this.startButton)
{
if (this.noHorses > 2)
if (this.raceStarted)
{
winner = this.
runRace();
this.resultField.setText(this.horseArray[this.winner].getName());
raceStarted =true;
}
this.resultField.setText(
"Insufficient horses to race");
}
elseif (theEvent.getSource() == this.newRace)
this.reset();
this.horseArray[4] =
new Horse(nameinput, 5, this.g);
}
}
}
this.horseArray[3] =new Horse(nameinput, 4, this.g);
}
}
}
this.horseArray[2] =new Horse(nameinput, 3, this.g);
}
}
}
this.horseArray[1] =new Horse(nameinput, 2, this.g);
}
}
}
this.horseArray[0] =new Horse(nameinput, 1, this.g);
}
}
}
}//closes action event
privatevoid reset()
{
raceStarted =false;
noHorses = 0;
this.name1.setText("");
this.name2.setText("");
this.name3.setText("");
this.name4.setText("");
this.name5.setText("");
this.resultField.setText("");
this.gReset.setColor(Color.GREEN);
this.gReset.fillRect(50, 201, 500, 18);
this.gReset.fillRect(50, 221, 500, 18);
this.gReset.fillRect(50, 241, 500, 18);
this.gReset.fillRect(50, 261, 500, 18);
this.gReset.fillRect(50, 281, 500, 18);
this.gReset.setColor(Color.cyan);
this.gReset.fillRect(550, 200, 15, 100);
int remove;
remove = 0;
this.horseArray[remove] =null;
if (remove < 5)
goto 191
}
privateint runRace()
{
int running;
running = 0;
if (this.horseArray[running])
{
this.horseArray[running].move();
this.horseArray[running].draw();
if (this.horseArray[running].getPosition() >= 540)
return running;
}
if (running < 5)
this.delay(200);
}
privatevoid sleep(long l){
}
privatevoid delay(int param_1)
{
sleep((long)param_1);
this.g.drawString("Sleep extention", 20, 20);
}
}//end of Race class xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I hope this displays right its my first post.

