Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How To Make Wheel Look Like It's Moving?

  1. #1
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default How To Make Wheel Look Like It's Moving?

    I have a TrainApplet that I have been working on for my first Java Applet. As of right now the train moves across the screen as desired, but how do I get the wheels to look like they are rotating as the train moves? I have tried a couple different things that did not work. Any help would be much appreciated. Also when the Applet is ran the train that is erased is not the same color as the background even though I have it set to the same color...any ideas to why this is?

    Here is my current code:
    Java Code:
    package trainapp;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.Timer;
    
    /**
      @author 
    */
    
    public class TrainApplet extends JApplet {
    	
    	private final int delay = 50; //milliseconds
    	public int TRAIN_x = -50;
    	public int TRAIN_y = 170;
    	private final int TRAIN_WIDTH = 200;
    	private final int TRAIN_HEIGHT = 60;
    	private int TRAIN_WHEEL_WIDTH = 40;
    	private int TRAIN_WHEEL_HEIGHT = 40;
    	private int stepSize = 10;
    	
    	/** (non-Javadoc)
    	  @see java.applet.Applet#init()
    	  */
    	public void init() 
    	{
    		setBackground(Color.blue);
    		startAnimation();
    	}
    	
    	/** (non-Javadoc)
    	  @see java.awt.Container#paint(java.awt.Graphics)
    	 */
    	public void paint(Graphics page) 
    	{	
    		// add your code here
    		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    	    ge.getAllFonts();
    	    
    		int style = Font.BOLD | Font.ITALIC;
    		Font font = new Font ("Garamond", style , 50);
    		page.setFont(font);
    		
    		page.drawString("Choo-Choo", 50, 50);
    		setSize(1000, 600);
    		
        	// Erase new Train
    		page.setColor(getBackground());
    		page.fillRoundRect(TRAIN_x, height() - TRAIN_y, TRAIN_WIDTH, TRAIN_HEIGHT, 10, 10 ); //2nd Rect
    		page.fillRoundRect(TRAIN_x, height() - TRAIN_y - 60, TRAIN_WIDTH - 90, TRAIN_HEIGHT, 10, 10 ); // 3rd Rect
    		page.fillRect(TRAIN_x, height() - TRAIN_y - 50, TRAIN_WIDTH - 90, TRAIN_HEIGHT); // cover gap on bottom rounds
    		page.fillRect(TRAIN_x, height() - TRAIN_y + 60, TRAIN_WIDTH, TRAIN_HEIGHT); // 1st (bottom) Rect
    		page.fillRect(TRAIN_x, height() - TRAIN_y + 50, TRAIN_WIDTH - 150, TRAIN_HEIGHT); // covers middle gap 
    		page.fillRect(TRAIN_x + 30, height() - TRAIN_y - 100, TRAIN_WIDTH - 150, TRAIN_HEIGHT); // 4th (top) Rect
    		page.fillOval(TRAIN_x + 5, height() - TRAIN_y + 118, TRAIN_WHEEL_WIDTH, TRAIN_WHEEL_HEIGHT); // 1st wheel (back)
    		page.fillOval(TRAIN_x + 46, height() - TRAIN_y + 118, TRAIN_WHEEL_WIDTH, TRAIN_WHEEL_HEIGHT); // 2nd Wheel
    		page.fillOval(TRAIN_x + 120, height() - TRAIN_y + 118, TRAIN_WHEEL_WIDTH, TRAIN_WHEEL_HEIGHT); //3rd Wheel
    		page.fillOval(TRAIN_x + 160, height() - TRAIN_y + 118, TRAIN_WHEEL_WIDTH, TRAIN_WHEEL_HEIGHT); //4th Wheel
    		
    		page.setColor(getBackground());
    		page.fillRect(TRAIN_x+40, height() - TRAIN_y - 40, TRAIN_WIDTH - 150, TRAIN_HEIGHT - 20); // 3rd Rect	
    		page.drawLine(TRAIN_x + 10, height() - TRAIN_y + 125,TRAIN_x + 37, height() - TRAIN_y + 155); // Wheel bar
    
    	
    		//Move to new position
    	    TRAIN_x = (TRAIN_x + stepSize) % width();
        	
    	   	// Draw new Train
    		page.setColor(Color.gray);
    		page.fillRoundRect(TRAIN_x, height() - TRAIN_y, TRAIN_WIDTH, TRAIN_HEIGHT, 10, 10 ); //2nd Rect
    		page.fillRoundRect(TRAIN_x, height() - TRAIN_y - 60, TRAIN_WIDTH - 90, TRAIN_HEIGHT, 10, 10 ); // 3rd Rect
    		page.fillRect(TRAIN_x, height() - TRAIN_y - 50, TRAIN_WIDTH - 90, TRAIN_HEIGHT); // cover gap on bottom rounds
    		page.fillRect(TRAIN_x, height() - TRAIN_y + 60, TRAIN_WIDTH, TRAIN_HEIGHT); // 1st (bottom) Rect
    		page.fillRect(TRAIN_x, height() - TRAIN_y + 50, TRAIN_WIDTH - 150, TRAIN_HEIGHT); // covers middle gap 
    		page.fillRect(TRAIN_x + 30, height() - TRAIN_y - 100, TRAIN_WIDTH - 150, TRAIN_HEIGHT); // 4th (top) Rect
    		page.fillOval(TRAIN_x + 5, height() - TRAIN_y + 118, TRAIN_WHEEL_WIDTH, TRAIN_WHEEL_HEIGHT); // 1st wheel (back)
    		page.fillOval(TRAIN_x + 46, height() - TRAIN_y + 118, TRAIN_WHEEL_WIDTH, TRAIN_WHEEL_HEIGHT); // 2nd Wheel
    		page.fillOval(TRAIN_x + 120, height() - TRAIN_y + 118, TRAIN_WHEEL_WIDTH, TRAIN_WHEEL_HEIGHT); //3rd Wheel
    		page.fillOval(TRAIN_x + 160, height() - TRAIN_y + 118, TRAIN_WHEEL_WIDTH, TRAIN_WHEEL_HEIGHT); //4th Wheel
    			
    		page.setColor(Color.black);
    		page.fillRect(TRAIN_x+40, height() - TRAIN_y - 40, TRAIN_WIDTH - 150, TRAIN_HEIGHT - 20); // 3rd Rect
    		page.drawLine(TRAIN_x + 25, height() - TRAIN_y + 120,TRAIN_x + 25, height() - TRAIN_y + 156); // Wheel bar
    					
    	}
    
    
    	/**
     		* Create an animation thread that runs periodically
     		**/
        private void startAnimation()
        {
            ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    repaint();
                }
            };
            new Timer(delay, taskPerformer).start();
        }
        
    	public final int width(){
    		return getWidth();
    	}
    	
    	public final int height(){
    		return getHeight();
    	}
    
    
    }
    Last edited by kraigballa; 10-12-2011 at 04:56 PM.

  2. #2
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: How To Make Wheel Look Like It's Moving?

    Any ideas? Or if anyone knows how to make a ball spin I'm sure I could figure it from there. Thanks!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How To Make Wheel Look Like It's Moving?

    Suggestions:

    • First and foremost, do all of your drawing in the paintComponent method of a JPanel, not the paint method of the JApplet itself. This should allow for smoother graphics since you'll have double buffering to help you.
    • The first method in the paintComponent method should be a call to the super's method: super.paintComponent(g). This will do all you need to erase the old train.
    • Don't perform program logic inside of paint or paint Component -- that method is only for drawing and nothing else as it needs to be as fast as possible. Also, you do not have complete control over when or even if it will be called. So increment trainX in the Swing Timer ActionListener's actionPerformed method.
    • Learn and use Java naming conventions -- use camel case always, capitalize the first letter of classes, capitalize all letters of constants, use lower case first letter for everything else. This will make it easier for others to understand what you're tying to do, be they your instructors, or volunteers in fora trying to help you out.
    • I would draw several BufferedImages of your wheel in various states of rotation, put them in an array and swap out the image that you wish to draw in your game loop (your Swing Timer).
    • I would probably do the rest of the train images as a BufferedImage and just translate where I draw it.
    sunde887 likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How To Make Wheel Look Like It's Moving?

    Suggestions:

    • First and foremost, do all of your drawing in the paintComponent method of a JPanel, not the paint method of the JApplet itself. This should allow for smoother graphics since you'll have double buffering to help you.
    • The first method in the paintComponent method should be a call to the super's method: super.paintComponent(g). This will do all you need to erase the old train.
    • Don't perform program logic inside of paint or paint Component -- that method is only for drawing and nothing else as it needs to be as fast as possible. Also, you do not have complete control over when or even if it will be called. So increment trainX in the Swing Timer ActionListener's actionPerformed method.
    • Learn and use Java naming conventions -- use camel case always, capitalize the first letter of classes, capitalize all letters of constants, use lower case first letter for everything else. This will make it easier for others to understand what you're tying to do, be they your instructors, or volunteers in fora trying to help you out.
    • I would draw several BufferedImages of your wheel in various states of rotation, put them in an array and swap out the image that you wish to draw in your game loop (your Swing Timer).
    • I would probably do the rest of the train images as a BufferedImage and just translate where I draw it.

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: How To Make Wheel Look Like It's Moving?

    I'm not all too familiar with that, but I'll give it a try. Thanks

Similar Threads

  1. Replies: 4
    Last Post: 07-23-2011, 08:08 PM
  2. How to make object stop moving with mouse click
    By mackavelirip in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 03:23 AM
  3. Make a clickable moving image?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 04-08-2011, 07:54 PM
  4. Make shape rounding (moving in circle)
    By mneskovic in forum New To Java
    Replies: 8
    Last Post: 08-17-2010, 03:05 PM
  5. Replies: 1
    Last Post: 08-07-2007, 06:59 AM

Posting Permissions

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