带有 ActionListener 的 Java 动画

标签 java

所以我试图在彼此的顶部画两个圆圈(有点像雪人),并在用户单击“开始”按钮时将雪人移动到右侧,并在用户单击“开始”按钮时停止移动雪人“停止”按钮。然而,我唯一能想到的是两个雪人并排绘制,对按钮没有反应。 这是我的想法:

import java.awt.Graphics2D;


public interface MoveableShape {
    void draw(Graphics2D g);
    void translate(int dx, int dy);

}

import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;


public class SnowmanShape implements MoveableShape {

private int x;
private int y;
private int width;

public SnowmanShape(int x, int y, int width){
    this.x = x;
    this.y = y;
    this.width = width;
}
@Override
public void draw(Graphics2D g2) {
    // TODO Auto-generated method stub
    Ellipse2D.Double head = new Ellipse2D.Double(0, 0, 10, 10); 
    Ellipse2D.Double body = new Ellipse2D.Double(0, 11, 10, 10); 
    g2.draw(head);
    g2.draw(body);

}

@Override
public void translate(int dx, int dy) {
    // TODO Auto-generated method stub
    x += dx;
    y += dy;
}

}

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;


public class MyPanel extends JPanel{
    MoveableShape s;
    public MyPanel (MoveableShape m){
        s = m;
    }
     public void paintComponent(Graphics g)
     { 
         super.paintComponent(g);
         s.draw((Graphics2D)g);
     } 
}

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class AnimationTester {

    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 200;
    private static final int SNOWMAN_WIDTH = 50;
    final static MoveableShape shape = new SnowmanShape(0, 0, SNOWMAN_WIDTH);
    final static JPanel panel = new MyPanel(shape);


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame();
        frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        JButton startButton = new JButton("Start");
        JButton stopButton = new JButton("Stop");

        frame.setLayout(new BorderLayout());

        frame.add(panel);
        frame.add(startButton, BorderLayout.NORTH);
        frame.add(stopButton, BorderLayout.SOUTH);

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
         final int DELAY = 100;

         // Milliseconds between timer ticks
         Timer t = new Timer(DELAY, translateSnowman());

         startButton.addActionListener(startTimer(t));
         stopButton.addActionListener(stopTimer(t));
    }

    public static ActionListener translateSnowman(){
        return new ActionListener(){
            public void actionPerformed(ActionEvent event){
                 shape.translate(1, 0);
                 panel.repaint();
            }

        };
    }

        public static ActionListener startTimer(final Timer t){
            return new ActionListener(){
                public void actionPerformed(ActionEvent event){
                     t.start();
                }

            };
    }

        public static ActionListener stopTimer(final Timer t){
            return new ActionListener(){
                public void actionPerformed(ActionEvent event){
                     t.stop();
                }

            };
    }
}

有人可以让我知道我哪里出错了或者指出我正确的方向吗?

编辑:我修复了 AnimationListener,所以现在它不会绘制 2 个雪人。然而雪人还是一动不动。我也更新了帖子中的代码。

最佳答案

在绘画之前,将 g2.translate(x, y); 添加到 SnowmanShape#draw:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;

class SnowmanShape implements MoveableShape {
    static final Color headColor = new Color(0xFFE9C9);
    static final Color bodyColor = new Color(0xEAF6FF);
    static final Color outlineColor = new Color(0x252525);

    int x;
    int y;
    int size;
    Ellipse2D.Double head;
    Ellipse2D.Double body;

    SnowmanShape(int x, int y, int size) {
        this.x = x;
        this.y = y;
        this.size = size;
        initModel();
    }

    void initModel() {
        head = new Ellipse2D.Double(0, 0, size, size);
        body = new Ellipse2D.Double(0, head.height, size * 1.3d, size * 1.5d);
        body.x -= (body.width - head.width) * (1 / 2d);
    }

    @Override
    public void draw(Graphics2D g2) {
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2.translate(x, y);
        g2.setColor(headColor);
        g2.fill(head);
        g2.setColor(outlineColor);
        g2.draw(head);
        g2.setColor(bodyColor);
        g2.fill(body);
        g2.setColor(outlineColor);
        g2.draw(body);
    }

    @Override
    public void translate(int dx, int dy) {
        x += dx;
        y += dy;
    }
}

关于带有 ActionListener 的 Java 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28905505/

相关文章:

java - 在静态创建者方法后面隐藏构造函数?

java - hibernate中自定义Long类型ID生成

java - 在 Spring Webflux 中执行阻塞 JDBC 调用

java - Java 9多线程HTTP客户端无法正常工作

java - Java 中的 XOR 神经网络

java - 需要在Java中提取字符串中遵循某种模式的值

java - 从 Activity 保存图像时显示进度条

java - 在 Android 上访问原始 GPS 数据

java - 如何使用 java selenium webdriver ashot firefox 自动获取网页的全屏截图包括固定元素

java - 是否可以将字符串重复数据删除用于生成 excel 的 apache POI?