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.

[17155 byte] By [redwattya] at [2007-10-3 3:54:38]
# 1

// <applet code="R" width="650" height="400"></applet>

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class R extends Applet implements ActionListener, Runnable {

private TextFieldname1, name2, name3, name4, name5, resultField;

private Label laneLabel1, laneLabel2, laneLabel3, laneLabel4, laneLabel5, resultLabel;

private Button startButton, newRace;

private Horse horseArray[];

int noHorses;

int winner;

Thread thread;

boolean raceStarted;

int[] laneHeights = { 200, 220, 240, 260, 280 };

//public Graphics g = getGraphics();avoid using this, it's trouble

//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);

public void init() {//initializer

horseArray = new Horse[5];

//this.setSize(800, 800);

//sets colour of background

this.setBackground(Color.cyan);

Panel panel = new Panel(new GridLayout(0,2));

laneLabel1 = new Label("Lane 1.Horse name: ");

panel.add(this.laneLabel1);

name1 = new TextField(3);

panel.add(name1);

name1.addActionListener( this);

laneLabel2 = new Label("Lane 2.Horse name: ");

panel.add(this.laneLabel2);

name2 = new TextField(3);

panel.add(name2);

name2.addActionListener( this);

laneLabel3 = new Label("Lane 3.Horse name: ");

panel.add(this.laneLabel3);

name3 = new TextField(3);

panel.add(name3);

name3.addActionListener( this);

laneLabel4 = new Label("Lane 4.Horse name: ");

panel.add(this.laneLabel4);

name4 = new TextField(3);

panel.add(name4);

name4.addActionListener( this);

laneLabel5 = new Label("Lane 5.Horse name: ");

panel.add(this.laneLabel5);

name5 = new TextField(3);

panel.add(name5);

name5.addActionListener( this);

//setSize(300, 500);

setSize(650, 500);

setLayout(new BorderLayout());

add(panel, "North");

add(getSouthPanel(), "South");

}//close initializer

private Panel getSouthPanel() {

resultLabel = new Label("The winner is: ");

resultField = new TextField(20);

startButton = new Button("Start the Race");

this.startButton.addActionListener(this);

newRace = new Button("New Race");

this.newRace.addActionListener(this);

Panel panel = new Panel();

panel.add(resultLabel);

panel.add(resultField);

panel.add(startButton);

panel.add(newRace);

return panel;

}

public void 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);

for(int j = 0; j < horseArray.length; j++) {

if(horseArray[j] != null)

this.horseArray[j].draw(g1);

}

}//closes paint

public void actionPerformed(ActionEvent theEvent) {//opens action listener

if(theEvent.getSource() instanceof TextField) {

TextField textField = (TextField)theEvent.getSource();

String text = textField.getText();

if(text.equals("") || text.equals(" "))

return;

int index = -1;

if(textField == name1)

index = 0;

if(textField == name2)

index = 1;

if(textField == name3)

index = 2;

if(textField == name4)

index = 3;

if(textField == name5)

index = 4;

this.horseArray[index] = new Horse(text, laneHeights[index]);

noHorses = this.noHorses + 1;

}

if(theEvent.getSource() instanceof Button) {

Button button = (Button)theEvent.getSource();

if(button == startButton) {

if (this.noHorses > 2)

runRace();

else

resultField.setText("Insufficient horses to race");

}

if(button == newRace)

this.reset();

}

}//closes action listener

public void run() {

while(raceStarted) {

try {

Thread.sleep(200);

} catch(InterruptedException ie) {

System.out.println("interrupted");

raceStarted = false;

}

for(int j = 0; j < horseArray.length; j++) {

if (this.horseArray[j] != null) {

this.horseArray[j].move();

if (this.horseArray[j].getPosition() >= 540) {

this.resultField.setText(this.horseArray[j].getName());

System.out.println(horseArray[j]);

raceStarted = false;

thread.interrupt();

thread = null;

break;

}

}

}

repaint();

}

}

private void reset() {

this.resultField.setText("");

repaint();

for(int j = 0; j < horseArray.length; j++)

this.horseArray[j].resetPosition();

}

private void runRace() {

if(!raceStarted) {

raceStarted = true;

thread = new Thread(this);

thread.setPriority(Thread.NORM_PRIORITY);

thread.start();

}

}

}//end of Race class

class Horse {

private int laneNo;

private String name;

private int xPosition;// current position for drawing the 慔orse?/font>

private int minSpeed;// a random number between 3 and 5 inclusive

public Horse(String myName, int lane) {

// code here to initialise name, laneNo, xPosition, g, minSpeed

name = myName;

laneNo = lane;

minSpeed = 3;

xPosition = 0;

}

private int randomVal(int min,int max) {// returns a random int

// in range min to max

return ((int)(Math.random()*10))%(max-min+1)+min;

}

public int getPosition() { return xPosition; }

public String getName() { return name; }

public void resetPosition() { xPosition = 0; }

public void move() {// sets the Horse抯 new position

xPosition+=minSpeed+randomVal(1,3);

}

/** code here to draw the red 慔orse?in the correct lane */

public void draw(Graphics g) {

g.drawRect(xPosition, laneNo, 10, 20);

g.setColor(Color.red);

}

public String toString() {

return "Horse[name:" + name + ", laneNo:" + laneNo +

", xPosition:" + xPosition + "]";

}

}

74philipa at 2007-7-14 21:52:37 > top of Java-index,Security,Cryptography...