actionListener在调用frm Jframe时在JPanel上执行两次]

问题描述 投票:0回答:1
我想给我的吃豆精灵制作动画,但是当我的计时器调用actionperformed来更新图像时,它会执行两次并跳过动画

这是我的主班代码

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.concurrent.TimeUnit; import javax.swing.Timer; public class Pacman extends JFrame implements ActionListener, KeyListener{ public static Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); public static int FRAME_HEIGHT = (int)(dim.height-dim.height/5); public static int FRAME_WIDTH = (int)(dim.width-dim.width/5); public static Pacman frame = new Pacman(); public static Game gameInterface = new Game(); public static Timer timer; public Pacman() { super("Pac-man"); timer = new Timer(175, this); timer.start(); addKeyListener(this); } public static void main(String[] args) { ImageIcon frameIcon = new ImageIcon("Icon.png"); frame.setSize(FRAME_WIDTH,FRAME_HEIGHT); frame.setIconImage(frameIcon.getImage()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); frame.setContentPane(gameInterface); gameInterface.requestFocus(); gameInterface.setFocusable(true); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); int JframeX = (dim.width-FRAME_WIDTH)/2; int JframeY = (dim.height-FRAME_HEIGHT)/2; frame.setLocation(JframeX, JframeY); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void paint(Graphics g) { super.paint(g); } public void actionPerformed(ActionEvent ev){ repaint(); } }

这是我的游戏类的代码

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.concurrent.TimeUnit; import javax.swing.Timer; public class Game extends JPanel implements ActionListener, KeyListener{ public static Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); public static int FRAME_HEIGHT = (int)(dim.height-dim.height/5); public static int FRAME_WIDTH = (int)(dim.width-dim.width/5); public static Game gamePanel = new Game(); public static Timer timer; public static boolean gameExit = false; public static int animCounter = 0, imgPacmanX1 = 840, imgPacmanY1 = 0, imgPacmanX2 = 890, imgPacmanY2 = 50; public Game() { timer = new Timer(500, this); timer.start(); addKeyListener(this); } public static void main(String[] args) { } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void paintComponent(Graphics g) { super.paintComponent(g); ImageIcon animations = new ImageIcon("Pacman_Animations.png"); g.setColor(Color.BLUE); g.fillRect(0, 0, FRAME_WIDTH, FRAME_HEIGHT); g.drawImage(animations.getImage(), 150, 200, 200, 250, imgPacmanX1, imgPacmanY1, imgPacmanX2, imgPacmanY2, null); } public void actionPerformed(ActionEvent ev){ if(animCounter == 2){ imgPacmanY1 = 0; imgPacmanY2 = 50; animCounter = 0; } else{ imgPacmanY1 += 50; imgPacmanY2 += 50; animCounter += 1; } repaint(); System.out.print(imgPacmanY1 + " " + imgPacmanY2 + " "); } }

如果我单独运行游戏类,则ActionListener可以正常工作,并且只能执行一次,但是从我的主类调用后,actionListener可以执行两次,动画开始跳过帧。

我希望为pacman sprite制作动画,但是当我的计时器调用actionperformed更新图像时,它会执行两次并跳过动画,这是我的主类import java.awt。*的代码; ...

java user-interface jpanel listener
1个回答
0
投票
您有两个Timer,每个都触发一个repaint,并且……欢迎来到您的体系结构为何出错的奇妙世界。
© www.soinside.com 2019 - 2024. All rights reserved.