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); } } } }