So I am trying to make some kind of platform game as a practice. I managed to create a 'ball' object and a 'platform'. And with some extremely simple gravity method I managed to make the two objects interact and have the platform stop the ball from falling to its doom.

But now I would like to create a second platform, or more. After adjusting the platform method a little bit I now hope to make it more flexible so I can create as many platforms as I would like. But something is going wrong. I do not get any errors while compiling, but the code only creates 1 platform (the last one in the code, it seems). I am still looking and think there is something I have to do with the paint()-method...

This is the code in my Game.java:

public class Game extends JPanel {

    Ball ball = new Ball(this);
    Platform platform = new Platform(this, 100, 280, 180, 20);
    Platform platform2 = new Platform(this, 80, 220, 150, 20);
    
    double speed = 2;
    double Gravity = 3;
    
    public Game() {
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }
            
            @Override
            public void keyReleased(KeyEvent e) {
            ball.keyReleased(e);
            }
            
            @Override
            public void keyPressed(KeyEvent e) {
            ball.keyPressed(e);
            }
        });
        
        setFocusable(true);
    }
        
    private void move() {
        ball.move();
    }
    
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
        ball.paint(g2d);
        platform.paint(g2d);
        platform2.paint(g2d);
        
    }
    
    public void gameOver() {
        JOptionPane.showMessageDialog(this, "Game Over!", "Game Over!"
        , JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
    }
    
    
    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Platform");
        Game game = new Game();
        frame.add(game);
        frame.setSize(300, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        while (true) {
            game.move();
            game.repaint();
            Thread.sleep(10);
        }
    }

}

I think it goes wrong with the paint() method for platform and platform2. It looks like only the last one is created.


And this is Platform.java: (The first values there are a way I thought up to check whether the platform was actually created, it would appear in the upper left corner if something would go wrong with the modification of all the variables.)

public class Platform {
    
    private static int Y = 0;
    private static int WIDTH = 10;
    private static int HEIGHT = 10;
    private static int X = 0;
    
    private Game game;
    
    public Platform(Game game, int x, int y, int width, int height) {
        this.game = game;
        
        Y = y;
        WIDTH = width;
        HEIGHT = height;
        X = x;
    }
    
    
    public void paint(Graphics2D g) {
        g.fillRect(X, Y, WIDTH, HEIGHT);
    }
    
    
    public Rectangle getBounds() {
        return new Rectangle(X, Y, WIDTH, HEIGHT);
    }
    
    
    public int getTopY() {
        return Y;
    }
       
}


Still looking for ways to fix it myself. And when I do I will let you guys know! But I hope somebody here can help out. :)