Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:39:35+00:00 2026-06-01T20:39:35+00:00

I created 2 car objects using my CarPanel. Then I used to keylistener to

  • 0

SnapshotI created 2 car objects using my CarPanel. Then I used to keylistener to repaint the object so it will look like moving forward.
But the problem is the whole panel moves along, even though I just increased the distance to the ‘x’ coordinate.
If I were to resize the window, it sometimes doesn’t even clear the component.

public class RacingCars extends JFrame {
    CarPanel car1;
    CarPanel car2;

    //Constructor
    public RacingCars(){
        setLayout(new GridLayout(2,1));
        car1 = new CarPanel('w',Color.RED);
        car2 = new CarPanel('k',Color.blue);

        car1.setBackground(Color.black);
        this.add(car1, BorderLayout.NORTH);
        this.add(car2, BorderLayout.SOUTH);

        this.addKeyListener(new keyListener());
    }

    class keyListener extends KeyAdapter{
        public void keyPressed(KeyEvent e){
            if(e.getKeyChar()=='w'){
                car1.moveCar();
            }
            if(e.getKeyChar()=='k'){
                car2.moveCar();
            }
        }
    }
}

CarPanel::

public class CarPanel extends JPanel {

    private char forwardKey = 'w';
    private boolean reachedTarget = false;
    private Color color = Color.blue;
    private int x= 10;
    private int y= 10;

    private int panelWidth;
    private int panelHeight;    

    //default Constructor
    public CarPanel(){      
    }

    //overloaded Constructor
    public CarPanel(char key, Color color){
        this.forwardKey=key;
        this.color = color;
    }

    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        panelWidth= getWidth();
        panelHeight= getHeight();

        //draw a Car
        g.setColor(color);

        //polygon points
        int t_x[]= {x+10,x+20,x+30,x+40};
        int t_y[]= {y+10,y,y,y+10};

        g.fillPolygon(t_x,t_y,t_x.length);
        g.fillRect(x, y+10, 50, 10);
        g.fillArc(x+10, y+20, 10, 10, 0, 360);
        g.fillArc(x+30, y+20, 10, 10, 0, 360);
    }

    @Override
    public Dimension getPreferredSize() {
       return new Dimension(750,100);
    }

    public void moveCar(){
        if(this.x < panelWidth){
            this.x+=10;
            repaint();
        }
    }
}

After the resize

Solution

Because of this code, I always had that extra space. but no idea how it supposed to cause to this strange behavior.

 /*
public int getX(){
        return this.x;
    }
*/
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:39:37+00:00Added an answer on June 1, 2026 at 8:39 pm

    But the problem is the whole panel moves along, ..

    No it doesn’t. The ‘green dot’ proves that.

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class RacingCars extends JFrame {
        CarPanel car1;
        CarPanel car2;
    
        //Constructor
        public RacingCars(){
            setLayout(new GridLayout(2,1));
            car1 = new CarPanel('w',Color.RED);
            car2 = new CarPanel('k',Color.blue);
    
            car1.setBackground(Color.black);
            this.add(car1, BorderLayout.NORTH);
            this.add(car2, BorderLayout.SOUTH);
    
            this.addKeyListener(new MyKeyListener());
        }
    
        class MyKeyListener extends KeyAdapter{
            public void keyPressed(KeyEvent e){
                if(e.getKeyChar()=='w'){
                    car1.moveCar();
                }
                if(e.getKeyChar()=='k'){
                    car2.moveCar();
                }
            }
        }
    
        public static void main(String[] args) {
            //Create the frame on the event dispatching thread
            SwingUtilities.invokeLater(new Runnable(){
    
                @Override
                public void run() {
                    RacingCars rc = new RacingCars();
                    rc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    rc.pack();
                    rc.setVisible(true);
                }
    
            });
        }
    }
    
    class CarPanel extends JPanel {
    
        private char forwardKey = 'w';
        private boolean reachedTarget = false;
        private Color color = Color.blue;
        private int x= 10;
        private int y= 10;
    
        private int panelWidth;
        private int panelHeight;
    
        //default Constructor
        public CarPanel(){
        }
    
        //overloaded Constructor
        public CarPanel(char key, Color color){
            this.forwardKey=key;
            this.color = color;
        }
    
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
    
            g.setColor(Color.GREEN);
            g.fillOval(0,0,25,25);
    
            panelWidth= getWidth();
            panelHeight= getHeight();
    
            //draw a Car
            g.setColor(color);
    
            //polygon points
            int t_x[]= {x+10,x+20,x+30,x+40};
            int t_y[]= {y+10,y,y,y+10};
    
            g.fillPolygon(t_x,t_y,t_x.length);
            g.fillRect(x, y+10, 50, 10);
            g.fillArc(x+10, y+20, 10, 10, 0, 360);
            g.fillArc(x+30, y+20, 10, 10, 0, 360);
        }
    
        @Override
        public Dimension getPreferredSize() {
           return new Dimension(750,100);
        }
    
        public void moveCar(){
            if(this.x < panelWidth){
                this.x+=10;
                repaint();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to keep a reference of the objects I've created in one
I have created an object and assigned values as follows: $car_object =& new Car();
I have an array of Car objects and using the following piece of code
So I have an object of ArrayProxy kind var App = Ember.Application.create(); App.car =
Created an OpenGraph action and object. Trying to submit my action. When I click
I created a new project using aspx visual studio 2010. All I am trying
I created a plugin for Internet Explorer (ActiveX object) and installed it successfully, but
Is this possible? For example if i write Car myCar; Then the constructor taking
I have created a class Car that derives form the abstract class TransportMeans and
I've been using my own object cache which works more or less: get objet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.