Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Using multiple timers to draw images??

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using multiple timers to draw images??



    Uploaded with ImageShack.us


    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...
    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....


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using multiple timers to draw images??

    Sorry I am new in this.... Do u have any reference I could refer to????

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using multiple timers to draw images??

    The API doc
    Java Platform SE 6
    and the tutorial
    The Really Big Index
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using multiple timers to draw images??

    Quote Originally Posted by Norm View Post
    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
     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
     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...

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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
     
     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];
            }
    ......
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using multiple timers to draw images??

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using multiple timers to draw images??

    Quote Originally Posted by Norm View Post
    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:

     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...

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Using multiple timers to draw images??

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using multiple timers to draw images??

    I can not make any recommendations based on the bits of code you have posted.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using multiple timers to draw images??

    Quote Originally Posted by Norm View Post
    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:
    /*
     * 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

     
    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()
     
    }

  14. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using multiple timers to draw images??

    Quote Originally Posted by KevinWorkman View Post
    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...

  16. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using multiple timers to draw images??

    Where is the panel for route 1 displayed in the GUI?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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(......);

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using multiple timers to draw images??

    Ya... That's the case.... How should I draw it then???

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with adding multiple images to an application
    By Jumbosize in forum Object Oriented Programming
    Replies: 9
    Last Post: February 26th, 2012, 12:52 PM
  2. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  3. Saving multiple images
    By Dario in forum Java Theory & Questions
    Replies: 3
    Last Post: January 25th, 2011, 01:59 PM
  4. Swing Timers
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 5
    Last Post: November 10th, 2009, 09:10 PM
  5. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 03:15 AM