java - 更新 JFrame 中圆的位置

标签 java swing jframe paint repaint

所以我想按照一定的速度移动一个圆圈,就像乒乓球一样。但我无法更新圆圈的位置。这是我的代码,它只会在侧面绘制圆形和矩形。

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

我想在 swing 中使用 Timer 类,但如果您有其他方法,我很乐意听到。

编辑:我添加了更新圆圈位置的尝试。

最佳答案

有必要repaint() 有问题的组件,这将导致再次调用paintComponent(Graphics) 方法。为此,监听器需要引用动画面板。这是一种方法,不包括对代码其他部分的更改。

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

关于java - 更新 JFrame 中圆的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21653445/

相关文章:

java - 当 JFrame 全屏时显示标题栏不起作用

java - setSize 函数造成困惑

java - Spring 启动 |多个调度程序 Servlet

java - schtasks : ERROR: Incorrect Start Date

java - 哪些 JFrame 对象支持音高变化?

java - 如何在java中的组合框中添加图像和文本

java - 为什么 ([A-Z]|[0-9])[\\.a-zA-Z0-9_-]{0,}$ 在 Java 中不匹配 abc123?

java - 如何避免将一个变量从一种方法传递到另一种方法

java - 事件派发线程实际上做了什么?

java - 第二个 JFrame 中的组件未显示