The logical problem in turnover in for ?
Welcome
wrote this code, everything is true but there is a logical problem to be solved first idea of this program is a moving train to the contrary, it can stop and operated once again and that can add cars and locomotives and can delete it but there is a problem when adding that the train carriages have remained stable cart after the first turnover Thus ...
THAT shows us the error ..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass Trainextends JFrameimplements ActionListener{
privateboolean check =false;
privateboolean stopped =true;
privateboolean add =false;
privateint trainPlace = 0;
privateint INDEX_START = 0;
publicint speed = 300;
private ImageIcon CurrentImage ;
privateboolean front=true, back=false ;
privateint carrNum = 0 ;
private JPanel screen, area , controlroom;
private JButton[][] street;
public JButton[] keybord;
private String s[] ={"Start","Back","Fast","Slow","Add Carriage","Delete Carriage","Rest","Check","Desiners"};
private ImageIcon images[][] ={{
new ImageIcon("images/UpRight.jpg"),
new ImageIcon("images/RightDown.jpg"),
new ImageIcon("images/NoTrack.jpg"),
new ImageIcon("images/NoTrack.jpg"),
new ImageIcon("images/NoTrack.jpg"),
},
{new ImageIcon("images/Vert.jpg"),
new ImageIcon("images/DownRight.jpg"),
new ImageIcon("images/Horiz.jpg"),
new ImageIcon("images/RightDown.jpg"),
new ImageIcon("images/NoTrack.jpg"),
},{
new ImageIcon("images/Vert.jpg"),
new ImageIcon("images/NoTrack.jpg"),
new ImageIcon("images/NoTrack.jpg"),
new ImageIcon("images/Vert.jpg"),
new ImageIcon("images/NoTrack.jpg"),
},{
new ImageIcon("images/Vert.jpg"),
new ImageIcon("images/UpRight.jpg"),
new ImageIcon("images/Horiz.jpg"),
new ImageIcon("images/RightUp.jpg"),
new ImageIcon("images/NoTrack.jpg"),
},{
new ImageIcon("images/DownRight.jpg"),
new ImageIcon("images/RightUp.jpg"),
new ImageIcon("images/NoTrack.jpg"),
new ImageIcon("images/NoTrack.jpg"),
new ImageIcon("images/NoTrack.jpg")}};
private ImageIcon frontImagesTrain[]={
new ImageIcon("images/TrainDown.jpg"),
new ImageIcon("images/TrainDown.jpg"),
new ImageIcon("images/TrainLeft.jpg"),
new ImageIcon("images/TrainLeft.jpg"),
new ImageIcon("images/TrainDown.jpg"),
new ImageIcon("images/TrainLeft.jpg"),
new ImageIcon("images/TrainUp.jpg"),
new ImageIcon("images/TrainUp.jpg"),
new ImageIcon("images/TrainUp.jpg"),
new ImageIcon("images/TrainUp.jpg"),
new ImageIcon("images/TrainRight.jpg"),
new ImageIcon("images/TrainDown.jpg"),
new ImageIcon("images/TrainRight.jpg"),
new ImageIcon("images/TrainDown.jpg")};
private ImageIcon trackImagesTrain[]={
new ImageIcon("images/Vert.jpg"),
new ImageIcon("images/RightUp.jpg"),
new ImageIcon("images/Horiz.jpg"),
new ImageIcon("images/UpRight.jpg"),
new ImageIcon("images/RightUp.jpg"),
new ImageIcon("images/DownRight.jpg"),
new ImageIcon("images/Vert.jpg"),
new ImageIcon("images/Vert.jpg"),
new ImageIcon("images/Vert.jpg"),
new ImageIcon("images/UpRight.jpg"),
new ImageIcon("images/RightDown.jpg"),
new ImageIcon("images/DownRight.jpg"),
new ImageIcon("images/Horiz.jpg"),
new ImageIcon("images/RightDown.jpg")};
private ImageIcon carrinImagesTrain[]={
new ImageIcon("images/Carriage1Down.jpg"),
new ImageIcon("images/Carriage1Down.jpg"),
new ImageIcon("images/Carriage1Left.jpg"),
new ImageIcon("images/Carriage1Left.jpg"),
new ImageIcon("images/Carriage1Down.jpg"),
new ImageIcon("images/Carriage1Left.jpg"),
new ImageIcon("images/Carriage1Up.jpg"),
new ImageIcon("images/Carriage1Up.jpg"),
new ImageIcon("images/Carriage1Up.jpg"),
new ImageIcon("images/Carriage1Up.jpg"),
new ImageIcon("images/Carriage1Right.jpg"),
new ImageIcon("images/Carriage1Down.jpg"),
new ImageIcon("images/Carriage1Right.jpg"),
new ImageIcon("images/Carriage1Down.jpg")};
privateint[]frontPath={13, 18,17,16,21,20,15,10,5,0,1,6,7,8};
privateint[]frontPath2={8, 13,18,17,16,21,20,15,10,5,0,1,6,7};
private ImageIcon icon;
private JLabel labelInfo;
public Train (){
screen =new JPanel(new GridLayout(1,1));
Font f =new Font ("Sanserif",Font.BOLD,18);
labelInfo =new JLabel("WELCOM" ,JLabel.CENTER);
labelInfo.setFont(f);
screen.setBackground(Color.red);
screen.add(labelInfo);
getContentPane().add(BorderLayout.NORTH,screen );
street =new JButton[5][5];
area =new JPanel (new GridLayout(5,5));
for(int r=0; r<5; r++)
for (int c=0; c<5; c++){
street[r][c] =new JButton("", images[r][c]);
area.add(street[r][c]);
}
getContentPane().add(BorderLayout.CENTER,area );
keybord =new JButton[9];
controlroom =new JPanel(new GridLayout(5,2));
//ActionHandler handler = new ActionHandler();
for (int i=0; i <9; i++)
{
keybord[i] =new JButton(s[i]);
keybord[i].addActionListener(this);
controlroom.add(keybord[i]);
keybord[i].setEnabled(false);
}
keybord[7].setEnabled(true);
getContentPane().add(BorderLayout.SOUTH,controlroom );
}
//
publicvoid moveTrainFront(){
int trainPlace1 = trainPlace;
try{
if (trainPlace == frontPath.length-1) trainPlace = 0;
else trainPlace ++;
if (carrNum == 0 )
{
int i = frontPath[trainPlace]/5;
int j = frontPath[trainPlace] - i * 5;
int r = frontPath[trainPlace1]/5;
int c = frontPath[trainPlace1] - r * 5;
street[i][j].setIcon(frontImagesTrain[trainPlace]);
street[r][c].setIcon(trackImagesTrain[trainPlace1]);
}
else
{
int i1 = frontPath[trainPlace]/5;
int j1 = frontPath[trainPlace] - i1 * 5;
street[i1][j1].setIcon(frontImagesTrain[trainPlace]);
int trainPlace2 = trainPlace;
for (int z = 1; z <= carrNum; z++)
{
if (trainPlace2 - z < 0) trainPlace2 = 13;
int i2 = frontPath[trainPlace2 - z]/5;
int j2 = frontPath[trainPlace2 - z] - i2 * 5;
street[i2][j2].setIcon(carrinImagesTrain[trainPlace2 - z]);
}
int r1 = frontPath[trainPlace1 - carrNum]/5;
int c1 = frontPath[trainPlace1 - carrNum] - r1 * 5;
street[r1][c1].setIcon(trackImagesTrain[trainPlace1 - carrNum]);
}
}catch(ArrayIndexOutOfBoundsException aiex){System.out.println("the array out of index");}
}
// Move train Back
publicvoid moveTrainBack(){
int trainPlace1 = trainPlace;
try{
if (trainPlace == 0) trainPlace = 13;
else trainPlace--;
int i = frontPath[trainPlace]/5;
int j = frontPath[trainPlace] - i * 5;
int r = frontPath[trainPlace1]/5;
int c = frontPath[trainPlace1] - r * 5;
street[i][j].setIcon(frontImagesTrain[trainPlace]);
street[r][c].setIcon(trackImagesTrain[trainPlace1]);
}catch(ArrayIndexOutOfBoundsException aiex){System.out.println("the array out of index");}
}
publicvoid setRest(){
setStop(true);
setSpeed(300);
int i = frontPath[trainPlace]/5;
int j = frontPath[trainPlace] - i * 5;
street[i][j].setIcon(trackImagesTrain[trainPlace]);
trainPlace = 0;
i = frontPath[trainPlace]/5;
j = frontPath[trainPlace] - i * 5;
street[i][j].setIcon(frontImagesTrain[0]);
}
publicvoid setCheck (boolean c)
{
int i = frontPath[trainPlace]/5;
int j = frontPath[trainPlace] - i * 5;
street[i][j].setIcon(frontImagesTrain[0]);
check = c ;
}
publicboolean getCheck (){return check ;}
publicvoid setSpeed (int s){speed = s ;}
publicint getSpeed(){return speed;}
//
publicvoid setStop(boolean s){stopped = s ;}
//
publicboolean getStop(){return stopped ;}
publicvoid setBack(boolean s){back = s ;}
//
publicboolean getBack(){return back;}
publicvoid setAdd(){carrNum++ ;}
//
publicboolean getAdd(){return add;}
// Action
publicvoid actionPerformed(ActionEvent e){
if(e.getSource()==keybord[0])//Start
if (stopped)
{
keybord[0].setText("Stop");
setStop(false);
labelInfo.setText("Running");
}
else
{setStop(true);
keybord[0].setText("Start");
labelInfo.setText("Stop");
}
if(e.getSource()==keybord[1])//Front And Back
if (!back)
{
setBack(true);
keybord[1].setText("Front");
labelInfo.setText("Back");
}
else
{
setBack(false);
keybord[1].setText("Back");
labelInfo.setText("Front");
}elseif (e.getSource()==keybord[2])//Fast
{
if (speed <= 0)
{
labelInfo.setText("You've arrived the maximum speed : " + getSpeed());
}
else{ setSpeed(speed - 10);
labelInfo.setText("The Train speed is slow down to : " + getSpeed());
}
}
elseif(e.getSource()==keybord[3])//Slow
{setSpeed(speed + 10);
labelInfo.setText("The Train speed is pulled up to : "+getSpeed());
}
elseif(e.getSource()==keybord[4])//ADD carrin
{
setAdd();
labelInfo.setText("Add Carriage");
//setStop(false);
}
elseif(e.getSource()==keybord[5])//Delete carrin
{
labelInfo.setText("Delete Carrin");
}
elseif(e.getSource()==keybord[6])//Rest
{
setRest();
labelInfo.setText("Rest");
}
elseif (e.getSource()==keybord[7])//Check
{
setCheck(true);
keybord[7].setEnabled(false);
keybord[0].setEnabled(true);
keybord[1].setEnabled(true);
keybord[2].setEnabled(true);
keybord[3].setEnabled(true);
keybord[4].setEnabled(true);
keybord[5].setEnabled(true);
keybord[6].setEnabled(true);
keybord[8].setEnabled(true);
labelInfo.setText("The Train is Checked");
}
elseif(e.getSource()==keybord[8])//desiners
{
String x ="";
labelInfo.setText("Desiners About");
JOptionPane.showMessageDialog(null,"The Progect is Under The Auspices of:"
+"\n?" +"\n" +"\n ?" +
"\n ?" +"\n?" +"\n ?" +"\n" +"\n" +"\n? " +"\n C 2007");}
}
}
This main ..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Thread;
publicclass TestTrain{
publicstaticvoid main(String[]args){
Train train =new Train ();
MoveTrain mt =new MoveTrain(train);
train.setTitle("Train");
train.setSize(421,550);
train.setVisible(true);
train.addWindowListener (
new WindowAdapter (){
publicvoid windowClosing(WindowEvent e ){
System.exit(0);
}
});
mt.start();
}
}
This Threads
import java.lang.Thread;
publicclass MoveTrainextends Thread{
Train train ;
public MoveTrain (Train train){
this.train= train;
}
publicvoid run(){
while(!train.getCheck());
while (train.getCheck()){
try{sleep (train.getSpeed());}
catch (InterruptedException e ){System.out.println("unknown error");}
if (!train.getStop())
{
if(!train.getBack())
{train.moveTrainFront();
}
else train.moveTrainBack();
}
}
}
}
This Link image icon ..
http://www.geocities.com/alhairan_2003/as/777/UpRight.jpg
http://www.geocities.com/alhairan_2003/as/777/Vert.jpg
http://www.geocities.com/alhairan_2003/as/777/TrainUp.jpg
http://www.geocities.com/alhairan_2003/as/777/TrainRight.jpg
http://www.geocities.com/alhairan_2003/as/777/TrainLeft.jpg
http://www.geocities.com/alhairan_2003/as/777/TrainDown.jpg
http://www.geocities.com/alhairan_2003/as/777/Train.jpg
http://www.geocities.com/alhairan_2003/as/777/RightUp.jpg
http://www.geocities.com/alhairan_2003/as/777/RightDown.jpg
http://www.geocities.com/alhairan_2003/as/777/NoTrack.jpg
http://www.geocities.com/alhairan_2003/as/777/Horiz.jpg
http://www.geocities.com/alhairan_2003/as/777/DownRight.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage3Up.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage3Right.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage3Left.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage3Down.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage2Up.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage2Right.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage2Left.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage2Down.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage1Right.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage1Left.jpg
http://www.geocities.com/alhairan_2003/as/777/Carriage1Down.jpg
.....
thanks:)

