Testing Forum

Would you like to react to this message? Create an account in a few clicks or log in to continue.

4 posters

    Java Traffic Simulation

    wendy~jap
    wendy~jap


    Posts : 45
    Join date : 2008-04-12
    Location : Kuching

    Java Traffic Simulation Empty Java Traffic Simulation

    Post by wendy~jap Fri May 02, 2008 6:30 pm

    Hey guys,


    I've figured out the way to use Timer properly in order to decrease the speed of your animation! Refer to the sample code in this following link: http://forum.java.sun.com/thread.jspa?threadID=5140432&messageID=9516333

    Pay attention at the way he uses the Timer. The constructor takes two arguments: period of the delay (in milliseconds) and the object itself (this). The Timer is started in the constructor of the public class. Then he put a handler in the actionPerformed method. Each time the delay period is up, the codes inside the actionPerformed method will be run!

    Good luck on your assignment!


    Cheers
    GeneratoR
    GeneratoR
    Administrator
    Administrator


    Posts : 103
    Join date : 2008-04-02
    Age : 39
    Location : Away for Final Exam

    Character sheet
    Score:
    Java Traffic Simulation Left_bar_bleue100/100Java Traffic Simulation Empty_bar_bleue  (100/100)

    Java Traffic Simulation Empty Re: Java Traffic Simulation

    Post by GeneratoR Sun May 04, 2008 10:09 pm

    thanks...
    it is usefull, btw i use another method which i add an attributes(double speed) in my car class. so when i construct my car i can set diffrent speed to my cars...
    and i still strugling with how it detect other cars....
    my car always crash each others...

    many driver are injuried... pale
    hot_at_fusion.jr
    hot_at_fusion.jr


    Posts : 27
    Join date : 2008-04-07

    Java Traffic Simulation Empty Re: Java Traffic Simulation

    Post by hot_at_fusion.jr Tue May 06, 2008 1:52 pm

    One thing i want to point out is adding listener is quite a tricky part in this assignment.
    So don't relax yet once you got your simulation running.
    I find it quite confusing at the beginning to put the listener at the right place.
    Well i don't know if this would be the case with all of you or not, but if anyone stuck at this part just tell me and I might be able to help.
    Cheers.
    Anyways, i'd like to express my thanks to Nia for her brilliant design on my assignment. Laughing

    >>> THIS IS MY FIRST POST jocolor
    avatar
    ~Nia_VaL~
    Moderator
    Moderator


    Posts : 52
    Join date : 2008-04-02
    Age : 36
    Location : Kempas

    Java Traffic Simulation Empty Re: Java Traffic Simulation

    Post by ~Nia_VaL~ Fri May 09, 2008 10:25 am

    U r welcome Jr... Embarassed
    Dont forget to treat me shopping for one day...HAHAHA
    just kidding... geek
    GeneratoR
    GeneratoR
    Administrator
    Administrator


    Posts : 103
    Join date : 2008-04-02
    Age : 39
    Location : Away for Final Exam

    Character sheet
    Score:
    Java Traffic Simulation Left_bar_bleue100/100Java Traffic Simulation Empty_bar_bleue  (100/100)

    Java Traffic Simulation Empty Re: Java Traffic Simulation

    Post by GeneratoR Fri May 09, 2008 4:21 pm

    What is all this... dont understand at all..

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class DrawingExample extends JPanel implements ActionListener {
        private Timer timer = new Timer(200, this);
        private Rectangle[] rects = new Rectangle[10];
        private int currentUB;
        private boolean desc;
     
        public DrawingExample() {
            for(int i=0; i<rects.length; ++i)
                rects[i] = new Rectangle(i*15, i*15, 40, 40);
            timer.start();
        }
     
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            for(int i=0; i<currentUB; ++i)
                g2.draw(rects[i]);
        }
     
        public static void main(String[] args) {
            EventQueue.invokeLater(buildApp);
        }
     
        public void actionPerformed(ActionEvent evt) {
            currentUB += desc ? -1 : +1;
            if (currentUB == -1) {
                desc = false;
                currentUB = 1;
            } else if (currentUB == rects.length+1) {
                desc = true;
                currentUB = rects.length-1;
            }
            repaint();
        }
     
        static Runnable buildApp = new Runnable() {
            public void run() {
                JComponent comp = new DrawingExample();
                comp.setPreferredSize(new Dimension(300,300));
                JFrame f = new JFrame();
                f.getContentPane().add(comp);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        };
    }

    wendy~jap
    wendy~jap


    Posts : 45
    Join date : 2008-04-12
    Location : Kuching

    Java Traffic Simulation Empty Re: Java Traffic Simulation

    Post by wendy~jap Fri May 09, 2008 5:04 pm

    Nah, what I mean is that you just look at the way he implemented the Timer. The rest of the codes, just ignore them. I don't know them as well.

    Look at that:
    Code:

    private Timer timer = new Timer( 200, this );    // declaration, interval 200 milliseconds

    private DrawRectangle()
    {
      timer.start();    // in the class constructor, tell the timer to start counting
    }

    // Each time the interval's time out (means every 200 milliseconds), this method will be run
    public void actionPerformed( ActionEvent evt )
    {
      bla blaa bla....    // modify your cars' position here
      // then, each time the JPanel updates (paintComponent method is automatically called between times), a new position of the car will be drawn
    }

    That's the idea. I hope you get what I mean?
    hot_at_fusion.jr
    hot_at_fusion.jr


    Posts : 27
    Join date : 2008-04-07

    Java Traffic Simulation Empty Re: Java Traffic Simulation

    Post by hot_at_fusion.jr Fri May 09, 2008 5:07 pm

    Shocked Nice method

    Sponsored content


    Java Traffic Simulation Empty Re: Java Traffic Simulation

    Post by Sponsored content


      Current date/time is Thu May 16, 2024 7:49 am