对于动画,我使用了一条不同方向的 pacman 图像(嘴张开、半闭、全闭),并通过以下方式绘制它们:
frame = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(PacRIGHT.getSubimage(frame, 0, 28, 28), x, y, null);
}
为 pacman 设置动画,帧是 * 30 以向右移动 30 像素,>= 60 以完成 1 个动画周期,这将是:
frame = 0*30 ;
frame = 1*30 ;
frame = 2*30 ;
在这里我们为每个方向绘制了 4 个不同的图像条,我面临的第一个问题是想办法做到这一点,而且我也无法移动它,这是代码
更新:这次使用摇摆计时器 代码已更新,现在我移动 Pacman 和正确方向的动画嘴问题现在我收到多个错误。
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Runtest extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(4, this);
int x, y, velocx, velocy = 0;
int keyCode;
int frame = 0;
BufferedImage PacUP;
BufferedImage PacDOWN;
BufferedImage PacLEFT;
BufferedImage PacRIGHT;
public Runtest() throws IOException {
t.start();
addKeyListener(this);
setFocusable(true);
PacRIGHT = ImageIO.read(new File("E:\\javapacman\\newright.png"));
}
public int frame() {
frame++;
if (frame > 3)
frame = 0;
return frame;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(PacRIGHT.getSubimage(frame() * 30, 0, 28, 28), x, y, null);
// Graphics2D g2 = (Graphics2D) g ;
// g2.fill(new Ellipse2D.Double(x,y,40,40));
}
public void actionPerformed(ActionEvent ae) {
repaint();
x += velocx;
y += velocy;
}
public void up() {
velocy = -1;
velocx = 0;
}
public void down() {
velocy = 1;
velocx = 0;
}
public void left() {
velocx = -1;
velocy = 0;
}
public void right() {
velocx = 1;
velocy = 0;
}
@Override
public void keyPressed(KeyEvent ke) {
keyCode = ke.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
up();
}
if (keyCode == KeyEvent.VK_DOWN) {
down();
}
if (keyCode == KeyEvent.VK_LEFT) {
left();
}
if (keyCode == KeyEvent.VK_RIGHT) {
right();
}
}
}
主要课程:
public class PacMan {
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Test");
Runtest test = new Runtest();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(test);
frame.setVisible(true);
}
}
我得到的错误
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at pactest.Runtest.keyReleased(Runtest.java:128)
at java.awt.Component.processKeyEvent(Component.java:6466)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2828)
at java.awt.Component.processEvent(Component.java:6282)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)