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 8 of 8

Thread: My code cannot display the Car [HELPED]

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My code cannot display the Car [HELPED]

    Hello friends, i am new to this forum, sorry if i break some rules in this forum ( please notice me)

    this is my code, i don't know what is wrong with it...it should display a car that is moving but it doesn't
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class RacingCar extends JFrame{
     
    	public RacingCar(){
    		add(new MovingCar());
    	}
     
    	public static void main(String[] args){
    		JFrame frame = new RacingCar();
    		frame.setTitle("Racing car");
    		frame.setLocationRelativeTo(null);
    		frame.setSize(300,300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    	static class MovingCar extends JPanel{
    		private int x = 0;
    		private int y = 100;
    		public MovingCar(){	
    			Timer timer = new Timer(1000, new TimerListener() );
    			timer.start();
    		}
     
    		public MovingCar(int x, int y){
    			this.x = x;
    			this.y = y;
     
    			Timer timer = new Timer(1000, new TimerListener() );
    			timer.start();
    		}
     
    		public void paintComponent(Graphics g){	
    			super.paintComponent(g);
     
     
    			if( x > getWidth() )
    				x = - 20;
    			x += 5;
     
     
    			// call the body
    			add(new constructBody(x,y));
     
    		}
     
    		// class for timing
    		class TimerListener implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				repaint();
    			}
    		}
     
    		// class for constructing the body
    		static class constructBody extends JPanel{
    			private int x = 0;
    			private int y = 0;
     
    			public constructBody(int x, int y){
    				this.x = x;
    				this.y = y;
     
    			}
     
    			public void paintComponent(Graphics g){
    				super.paintComponent(g);
    				int xTra [] = {x + 20, x + 30, x + 40,  x + 10};
    				int yTra [] = {y - 30, y - 30, y - 20, y - 20};
    				g.setColor(Color.RED);
    				g.fillPolygon(xTra, yTra, xTra.length);
    				g.setColor(Color.gray);
    				g.fillRect(x, y - 20, 50 , 10);
    				g.setColor(Color.black);
    				g.fillOval(x + 10, y - 10, 10, 10);
    				g.setColor(Color.black);
    				g.fillOval(x + 30, y - 10, 10 , 10);
     
    			}
    		}
     
     
    	}
    }
    Last edited by robert589; June 15th, 2014 at 07:16 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My code cannot display the Car [HELPED]

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code cannot display the Car [HELPED]

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.
    thank you, i have done it

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: My code cannot display the Car [HELPED]

    Are you getting any error messages? Is there a part of the program you think
    is causing the problem?

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. #5
    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: My code cannot display the Car [HELPED]

    Try debugging the code by adding some println statements to all the methods and constructors to see which one are being executed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code cannot display the Car [HELPED]

    Quote Originally Posted by Ada Lovelace View Post
    Are you getting any error messages? Is there a part of the program you think
    is causing the problem?

    Wishes Ada xx
    i dont get any error message
    i think it's from
    // call the body
    add(new constructBody(x,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: My code cannot display the Car [HELPED]

    To see where a component has been placed and how big it is, add a call to getBounds() to the debugging println() statements.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My code cannot display the Car [HELPED]

    thank you, i have done it
    Excellent! And thanks for posting properly indented code.

    Look what happens when you change the MovingCar.paintComponent() method to what I've pasted below, and see if that gives you any hints about what's going on. Continue adding test code to see what else you can learn.
    public void paintComponent(Graphics g)
    { 
        super.paintComponent(g);
     
        // GB: test code to investigate
        g.drawString( "Moving Car", x, y);
     
        if( x > getWidth() )
            x = - 20;
        x += 5;
     
     
        // call the body
        add(new constructBody(x,y));
     
    }

Similar Threads

  1. How can I get this code to display in my GUI? Specifically my TextArea?
    By jakhondi21 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 9th, 2013, 02:24 PM
  2. Display HTMl code in JAVA terminal...
    By Alkirk in forum Java Theory & Questions
    Replies: 3
    Last Post: November 16th, 2011, 03:34 AM
  3. Replies: 8
    Last Post: June 26th, 2011, 07:50 AM
  4. The car/vehicle application will be responsible for providing a car simulation.
    By vikasreddy556 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 20th, 2011, 01:32 AM