Results 1 to 5 of 5
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Trying to create picture race using timers and threads and car pcitures wont move.

    Hi, I need my car pictures to move to the end then declare which one is the winner. The amount of cars can be 5 or more. My car pictures will not move. I am not receiving any errors. This is my code:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    
    public class Races extends JFrame{
       //attributes
       private int racers;
       private JLabel jlImage;
       private Timer timer;
       int w = 1;
       JPanel jpWest = new JPanel();
       JPanel jpEast = new JPanel();
       RacerProgress rp = null;
       //Point pt = 0,0;
       //List<RacerProgress> = new LinkedList<RacerProgress>();
       
       
       public Races(int _racers){
          racers = _racers;
          setTitle("Off to the Races - Nicholas Smith");
          setLayout(new BorderLayout());
          setLocationByPlatform(true);
          setLocationRelativeTo(null);
          setSize(600, 600);
          
          jpWest.setLayout(new GridLayout(0,1));
                
          while(0 <= racers){
             rp =  new RacerProgress();
             jpWest.add(rp);
             System.out.println("running");
             racers--;
          }
          
          while(0 <= racers){
             Thread t1 = new Thread(rp);
             t1.start();
             racers--;
          }
          
          //add JPanels
          add(jpWest, BorderLayout.WEST);
          add(jpEast, BorderLayout.EAST);
          
          
          //pack();
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
       
       public class MyTimerListener implements ActionListener{
          public void actionPerformed(ActionEvent ae){
             Point loc = jlImage.getLocation();
             loc.translate(1,0);
             jlImage.setLocation(loc);
             repaint();
          }
       }
       
      public void paint(Graphics g) {
            super.paint(g);  
            Graphics2D g2 = (Graphics2D) g;
            Line2D lin = new Line2D.Float(500, 0, 500, 600);
            g2.draw(lin);
        }
    
       
       class RacerProgress extends JPanel implements Runnable{
          
          public RacerProgress(){
             Icon image = new ImageIcon("races.gif");
             jlImage = new JLabel(image);
             jlImage.setBounds(-100,0,20,20);
             jlImage.setDoubleBuffered(true);
             jpWest.add(jlImage);
          }
          
          public void run(){
             MyTimerListener mt1 = new MyTimerListener();
             timer = new Timer((int)(Math.random()*100), mt1); //every ten ms will call timerlistener
             timer.start();
             //if(jlImage.getLocation() == (500)(1)){
             
             //};
     
          }
       }
       
       public static void main(String[] args){
          Races hw4 = new Races(6);
       }
    
    }
    Please help

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Trying to create picture race using timers and threads and car pcitures wont move

    Why do you have the image inside a JLabel? You can just draw the image directly.

    What layouts are you using? Do they support moving the JLabel to a specific location?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Trying to create picture race using timers and threads and car pcitures wont move

    That is how i was taught to create the pictures on the JPanel. I have it set up with a border layout and then have a grid layout on the west side of border layout...only one car is moving now and then it disappears once it hits the center of the JFrame. I did the border layout then the grid layout because it was only way cars would appear on the left.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Trying to create picture race using timers and threads and car pcitures wont move

    Well, I have been looking at this for about 20 minutes. You are doing a whole lot of unncessary things (in my opinion). You are using threads which really aren't necessary and you add RacerProgress to jpWest and inside of that class you also add something to jpWest. (also your threads never get started because racers is already decremented to 0 before you reach the second while loop.

    My recommendation would be to simply define a class of images to race. Store each instance in an array or List. Have each instance of the class initialize its starting position coordinates. Have each class define its own timer method to simply update those coordinates at random intervals. And then every time they are updated, do a repaint and have the images redrawn in the JPanel. Because the image coordinates might be updated as they are being repainted, you may have some synchronization issues to deal with.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Trying to create picture race using timers and threads and car pcitures wont move

    Thank you and i was starting to go that route and will complete doing that.

Similar Threads

  1. how to create picture gallery?
    By theChameleon in forum AWT / Swing
    Replies: 7
    Last Post: 06-16-2013, 12:58 PM
  2. Picture not showing when create JAR file
    By kmckinley820 in forum New To Java
    Replies: 4
    Last Post: 03-19-2011, 11:11 PM
  3. Rpg game player wont move help please
    By askumi in forum New To Java
    Replies: 19
    Last Post: 12-22-2010, 03:21 AM
  4. Timers, Networking, Threads Error
    By Dan0100 in forum New To Java
    Replies: 4
    Last Post: 09-23-2010, 12:28 AM
  5. How do you move a picture?
    By Addez in forum New To Java
    Replies: 6
    Last Post: 11-03-2009, 09:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •