Question

So I want to move a circle according to a certain velocity, like pong. But I'm having trouble updating the the circle's position. Here is my code that will just draw the circle and rectangle on the side.

import java.awt.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game extends JFrame{
    public Game(){
        GameScreen p1 = new GameScreen();
        add(p1);
        Timer t = new Timer(1000, new ReDraw());
        t.start();
    }

    public static void main(String[] args){
        Game g = new Game();
        g.setLocation(400, 200);
        g.setSize(700, 600);
        g.setVisible(true);
    }
}

class ReDraw implements ActionListener{
    static int count = 0;
        static int posX = 603;
        static int posY = 210;
        static int velX = 50;
        static int velY = 50;
    public void actionPerformed(ActionEvent e){
        count++;

        posX -= velX;
        posY -= velY;
        System.out.println("Flag 1: " + posX + " " + posY);

        if (count == 2)
            ((Timer)e.getSource()).stop();
    }
}

class GameScreen extends JPanel{
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.orange);
        g.fillRect(654, 200, 30, 100);

        g.setColor(Color.red);
        g.fillOval(ReDraw.posX, ReDraw.posY, 50, 50);
    }
}

I would like to use the Timer class in swing but if you have another way I would love to hear it.

EDIT: I added an attempt at updating the position of the circle.

Était-ce utile?

La solution

It is necessary to repaint() the component in question, which will result in the paintComponent(Graphics) method being called again. To do that, the listener will need to have a reference to the animation panel. This is one way to do it, that includes no changes to other parts of the code.

import java.awt.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game001 extends JFrame {

    public Game001() {
        GameScreen p1 = new GameScreen();
        add(p1);
        Timer t = new Timer(1000, new ReDraw(p1));
        t.start();
    }

    public static void main(String[] args) {
        Game001 g = new Game001();
        g.setLocation(400, 200);
        g.setSize(700, 600);
        g.setVisible(true);
    }
}

class ReDraw implements ActionListener {

    static int count = 0;
    static int posX = 603;
    static int posY = 210;
    static int velX = 50;
    static int velY = 50;
    GameScreen gameScreen;

    ReDraw(GameScreen gameScreen) {
        this.gameScreen = gameScreen;
    }

    public void actionPerformed(ActionEvent e) {
        count++;

        posX -= velX;
        posY -= velY;
        System.out.println("Flag 1: " + posX + " " + posY);
        gameScreen.repaint();

        if (count == 4) {
            ((Timer) e.getSource()).stop();
        }
    }
}

class GameScreen extends JPanel {

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

        g.setColor(Color.orange);
        g.fillRect(654, 200, 30, 100);

        g.setColor(Color.red);
        g.fillOval(ReDraw.posX, ReDraw.posY, 50, 50);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top