# Using multiple timers to draw images??

• March 25th, 2012, 10:13 AM
athrun8703
Using multiple timers to draw images??
http://img163.imageshack.us/img163/6751/mtimers.png

hi.. from my uploaded images there are buses on 2 route, namely the blue route and the red route.. my task is to move these buses on their respective route and make the bus stop when it is at the end of its route...

my code to make 1 bus move...
Code :

```public void actionPerformed(ActionEvent e) {   if (status == false) { System.out.println("status false"); tm.stop(); } else if (CurI>= i-1)// end of the route { status =false; }   else {   if(numOfInc[0] == 0) {   double BigD = 0; int smallD = 4; // the algorithm for smooth movement but curve not tested BigD = Math.sqrt((xcoord[CurI+1]-xcoord[CurI])*(xcoord[CurI+1]-xcoord[CurI]) + (ycoord[CurI+1]-ycoord[CurI])*(ycoord[CurI+1]-ycoord[CurI])); double numInc = BigD/smallD - 1; numOfInc[0] = (int)numInc;   int changeInX = xcoord[CurI+1] - xcoord[CurI]; double incX_double = changeInX/numOfInc[0]; xm[0] = (int)incX_double;   int changeInY = ycoord[CurI+1] - ycoord[CurI]; double incY_double = changeInY/numOfInc[0]; ym[0] = (int)incY_double; X1 = xcoord[CurI]; Y1 = ycoord[CurI];   repaint(); } else { X1 += xm[0]; Y1 += ym[0];   numOfInc[0] --; if(numOfInc[0] == 0) CurI++; repaint(); }//end else   tm.start(); }     }//end actionPerformed() ...   at the paint method; g.drawImage(img2,X1-15,Y1-15,null);```

need help on editing the codes to work on multiples buses.. the number of buses would be dynamically changing... so the performance has to be considered...

Thanks....
• March 25th, 2012, 12:11 PM
Norm
Re: Using multiple timers to draw images??
To have more than one bus, you will need an array or arraylist to hold the controlling data and possibly code for each bus.
Then loop through the list and process each bus's data
• March 25th, 2012, 06:07 PM
athrun8703
Re: Using multiple timers to draw images??
Sorry I am new in this.... Do u have any reference I could refer to????
• March 25th, 2012, 06:12 PM
Norm
Re: Using multiple timers to draw images??
The API doc
Java Platform SE 6
and the tutorial
The Really Big Index
• March 25th, 2012, 09:13 PM
athrun8703
Re: Using multiple timers to draw images??
Quote:

Originally Posted by Norm
The API doc
Java Platform SE 6
and the tutorial
The Really Big Index

hi.. i read the code but i still don't get what it mean.... also now i encounter another error.. the above code is to be created into a bus object and the main class will create this bus object and draw the image... which is to say the bus object class will not have any paint method.. i tried to create another timer at the main class and also keep the exisiting timer of the bus class.. but the updated coordinates did not get updated..

ActionPerformed in Bus class
Code :

``` public void actionPerformed(ActionEvent ev) { System.out.println("BusRender ActionPerformed"); if (status == false) {   tm.stop(); } else if (CurI>= i-1)// end of the route { status =false; }   else {   if(numOfInc[0] == 0) {   double BigD = 0; int smallD = 4;   BigD = Math.sqrt((xcoord[CurI+1]-xcoord[CurI])*(xcoord[CurI+1]-xcoord[CurI]) + (ycoord[CurI+1]-ycoord[CurI])*(ycoord[CurI+1]-ycoord[CurI])); double numInc = BigD/smallD - 1; numOfInc[0] = (int)numInc;   int changeInX = xcoord[CurI+1] - xcoord[CurI]; double incX_double = changeInX/numOfInc[0]; xm[0] = (int)incX_double;   int changeInY = ycoord[CurI+1] - ycoord[CurI]; double incY_double = changeInY/numOfInc[0]; ym[0] = (int)incY_double; X1 = xcoord[CurI]; Y1 = ycoord[CurI];   } else { X1 += xm[0]; Y1 += ym[0];   numOfInc[0] --; if(numOfInc[0] == 0) CurI++;   }//end else   tm.start(); }   }```

ActionPerformed and Paint of main class
Code :

``` public void actionPerformed(ActionEvent e) { tm2.stop(); repaint(); System.out.println("Main ActionPerformed"); tm2.start(); }   ...   g2d.drawImage(img2,bus.getX1()-15,bus.getY1()-15,null);```

getX1 and getY1 should return the updated coordinates from bus class...

i have check the xcoord and ycoord array and confirmed that the value are correct and it is being pass into the bus class when it is being instantiated...
• March 26th, 2012, 12:48 AM
athrun8703
Re: Using multiple timers to draw images??
please ignore my post above... i made some progress... the issue now is the program only draw the last set of the route.. with respect to my attached image above, it would only draw the blue route.. the red route have being overwritten...
Main class code
Code :

```Contsructor of main class { readData("route1.txt");   b[0]=new BusRender(xCoord,yCoord,i); readData("route2.txt"); b[1]= new BusRender(xCoord,yCoord,i); }   readData i=0; xCoord= new int[30]; yCoord= new int[30]; // during integation this method will get the list of coordinates of the route // this bus object is moving on try { Scanner sc = new Scanner(new FileReader(filename)); while (sc.hasNextInt()) { tempX = sc.nextInt(); tempY = sc.nextInt(); xCoord[i] = tempX; yCoord[i] = tempY; i++; }//end while sc.close();```

BusRender Class
Code :

```  public BusRender(int[] newCoordx, int[] newCoordy, int numItem) { i=numItem; for (int y=0; y<i-1;y++) { xcoord[y]= newCoordx[y]; ycoord[y]= newCoordy[y]; } ...... }```
• March 26th, 2012, 07:39 AM
Norm
Re: Using multiple timers to draw images??
Quote:

issue now is the program only draw the last set of the route.
Do you have a loop in the paintComponent method that draws the bus routes in a loop? It should draw all the routes in a list each time it is called.
Are you saying that the code only draws one route and not all the routes?
• March 26th, 2012, 07:49 AM
athrun8703
Re: Using multiple timers to draw images??
Ya... The program only print the last route and the relevant bus for that loop.. What u mean by drawing the bus in a loop... My current code has a paint method for each bus object... Meaning to say the busrender class has the paint component...
• March 26th, 2012, 07:55 AM
Norm
Re: Using multiple timers to draw images??
pseudo code for the paintComponent method:
begin loop
get next bus route from the list of routes
draw the route
end loop
• March 26th, 2012, 09:03 AM
athrun8703
Re: Using multiple timers to draw images??
Quote:

Originally Posted by Norm
pseudo code for the paintComponent method:
begin loop
get next bus route from the list of routes
draw the route
end loop

should the paint component be in the main class or BusRender class(bus class)... my bus render class has a paint method already..

BusRender paintcomponent:

Code :

``` public void paintComponent(Graphics g){   super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g.drawImage(img, 0, 0, null);   for (int z=0; z<(i-1);z++){   g.drawLine(xcoord[z], ycoord[z],xcoord[z+1], ycoord[z+1]);   }//end for   g.drawImage(img2,X1-15,Y1-15,null);   }//end paintComponent()```

the main class does not have any paint method...
• March 26th, 2012, 09:10 AM
KevinWorkman
Re: Using multiple timers to draw images??
• March 26th, 2012, 09:29 AM
Norm
Re: Using multiple timers to draw images??
I can not make any recommendations based on the bits of code you have posted.
• March 26th, 2012, 09:46 AM
athrun8703
Re: Using multiple timers to draw images??
Quote:

Originally Posted by Norm
I can not make any recommendations based on the bits of code you have posted.

1st sorry for the lengthy code..

the main class- SmoothBus:
Code :

```/* * To change this template, choose Tools | Templates * and open the template in the editor. */   /** * * @author raymond1 */ import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.Scanner; import java.io.*; import javax.imageio.ImageIO;   public class SmoothBus extends JFrame implements ActionListener{ BusRender[] b =new BusRender[2]; int [] xCoord = null; int [] yCoord = null; int tempX=0, tempY=0; int i=0; // Image img=null; JButton b1= new JButton("Bus Status");// use to simulate SCH change the status of bus boolean flag=true; public SmoothBus() { readData("route1.txt"); b[0]=new BusRender(xCoord,yCoord,i); readData("route2.txt"); b[1]= new BusRender(xCoord,yCoord,i); b1.addActionListener(this); Container box = getContentPane(); box.setLayout(new BorderLayout()); for (int z=0; z<b.length;z++) { box.add(b[z],BorderLayout.CENTER);   }   box.add(b1,BorderLayout.WEST);     }//end BusRender()   public void readData(String filename) { i=0; xCoord= new int[30]; yCoord= new int[30]; // during integation this method will get the list of coordinates of the route // this bus object is moving on try { Scanner sc = new Scanner(new FileReader(filename)); while (sc.hasNextInt()) { tempX = sc.nextInt(); tempY = sc.nextInt(); xCoord[i] = tempX; yCoord[i] = tempY; i++; }//end while sc.close(); }//end try catch (FileNotFoundException ex) {   }//end catch }//end readData()   public void actionPerformed (ActionEvent e) { // this method will be handle the stop/start signal from SCH Object source = e.getSource();   if (source == b1) { System.out.println(flag); flag=!flag; b[0].setStatus(flag);   }   } public static void main(String[] args) {   SmoothBus frame = new SmoothBus();   frame.setVisible(true); frame.setSize(1280, 1000); frame.setTitle("Bus"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }//end main     }// end class SmoothBus```

BusRender class

Code :

```  import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.Scanner; import java.io.*; import javax.imageio.ImageIO;     public class BusRender extends JPanel implements ActionListener { int X1,Y1 = 0; int x2; int y2; int x1; int y1; int i = 0; int CurI = 0;   int incX; int incY; boolean status=true; int xcoord[] = new int[30]; int ycoord[] = new int[30]; int numOfInc[] = new int[30]; int xm[] = new int[30]; int ym[] = new int[30];   Image img=null; int tempX = 0, tempY = 0, preX = 0, preY = 0;   Timer tm; // Image img = null; Image img2 = null;   public BusRender(int[] newCoordx, int[] newCoordy, int numItem) { i=numItem; for (int y=0; y<i-1;y++) { xcoord[y]= newCoordx[y]; ycoord[y]= newCoordy[y]; } numOfInc[0] = 0; try { img = ImageIO.read(new File("MAP-4.png")); img2 = ImageIO.read(new File("bus-top.png")); } catch (Exception e) {}   tm = new Timer(100, this); x2 = xcoord[1]; y2 = ycoord[1]; tm.start(); }//end MovingBus()     public void setStatus (boolean newStatus) { status = newStatus; if (newStatus == true)tm.start(); } public void actionPerformed(ActionEvent e) {   if (status == false) { System.out.println("status false"); tm.stop(); } else if (CurI>= i-1)// end of the route { status =false; }   else {   if(numOfInc[0] == 0) {   double BigD = 0; int smallD = 4; // the algorithm for smooth movement but curve not tested BigD = Math.sqrt((xcoord[CurI+1]-xcoord[CurI])*(xcoord[CurI+1]-xcoord[CurI]) + (ycoord[CurI+1]-ycoord[CurI])*(ycoord[CurI+1]-ycoord[CurI])); double numInc = BigD/smallD - 1; numOfInc[0] = (int)numInc;   int changeInX = xcoord[CurI+1] - xcoord[CurI]; double incX_double = changeInX/numOfInc[0]; xm[0] = (int)incX_double;   int changeInY = ycoord[CurI+1] - ycoord[CurI]; double incY_double = changeInY/numOfInc[0]; ym[0] = (int)incY_double; X1 = xcoord[CurI]; Y1 = ycoord[CurI];   repaint(); } else { X1 += xm[0]; Y1 += ym[0];   numOfInc[0] --; if(numOfInc[0] == 0) CurI++; repaint(); }//end else   tm.start(); }     }//end actionPerformed()     public void paintComponent(Graphics g){   super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g.drawImage(img, 0, 0, null);   for (int z=0; z<(i-1);z++){   g.drawLine(xcoord[z], ycoord[z],xcoord[z+1], ycoord[z+1]);   }//end for   g.drawImage(img2,X1-15,Y1-15,null);   }//end paintComponent()   }```
• March 26th, 2012, 10:55 AM
KevinWorkman
Re: Using multiple timers to draw images??
If you know how to update and draw one thing, then the logic for updating and drawing multiple things is the same. Just iterate over a list of things and do the updating and drawing to each thing. No need for multiple Timers.
• March 26th, 2012, 10:59 AM
athrun8703
Re: Using multiple timers to draw images??
Quote:

Originally Posted by KevinWorkman
If you know how to update and draw one thing, then the logic for updating and drawing multiple things is the same. Just iterate over a list of things and do the updating and drawing to each thing. No need for multiple Timers.

the timer is used to draw the buses.. what i am unsure now is where to put that iteration... as for the case of drawing one thing.. only that busRender class has the paint component.. the main class- SmoothBus only add the bus to its JFrame...
• March 26th, 2012, 11:02 AM
KevinWorkman
Re: Using multiple timers to draw images??
Your BusRender class is going to have to render all of the routes. I'd recommend moving all of your route data into a separate Object that knows how to draw itself. Then from your BusRender class you can just loop through all of those Objects to draw them.
• March 26th, 2012, 11:24 AM
athrun8703
Re: Using multiple timers to draw images??
The current BusRender class has the code to draw the route.. It is just that the line for the 1st route is not coming out... Even the bus image for the 1st BusRender object does not show out... The program keep overwritting the set of coordinates before it in the array of BusRender objects.... I was suspecting the culprit is the array reference being pass over... So at the BusRender constructor I pass the int value of the coordinates array element by element into the coordinates array to be used in BusRender class...
• March 26th, 2012, 11:39 AM
Norm
Re: Using multiple timers to draw images??
Where is the panel for route 1 displayed in the GUI?
• March 26th, 2012, 11:44 AM
athrun8703
Re: Using multiple timers to draw images??
At the constructor of SmoothBus.. There is a for loop which add BusRender array to the centre border layout.. Variable name is b[z].add(......);
• March 26th, 2012, 11:56 AM
Norm
Re: Using multiple timers to draw images??
How many components are being added to the center location? Which one is showing? I suspect that it is only the last one added. The first ones are being replaced.
• March 26th, 2012, 11:58 AM
athrun8703
Re: Using multiple timers to draw images??
Ya... That's the case.... How should I draw it then???
• March 26th, 2012, 12:02 PM
Norm
Re: Using multiple timers to draw images??
Have a JPanel that does all the drawing in its paintComponent. First it draws the map and then it goes through the list of buses and calls each one passing the bus object the Graphics reference passed to the paintComponent method