Java-如何从一组图像中按顺序绘制图像
我试图创建一个精灵类,每个数组中包含3帧,表示每个方向。这个类将实现一个actionlistener和keylistener,所以当我按下W键时,这个类将设置精灵向前移动的动画,绘制图像帧,给人一种行走的错觉 我在网上研究了一些关于这个问题的话题,结果是一次失败的尝试 我必须初始化当前位置积分和速度,显示它在屏幕上移动的速度。我需要一个计时器类来运行动画 我必须得到矩形边界,这样才能计算碰撞之间的交点。我正在学习游戏开发,并学习了计算机编程的基础知识。我仍然是一个业余的java编程者,并且在解决我的问题上只做了几次尝试 这里是图像加载的地方。取决于KeyEvent,它会发生变化。getImage()递增当前帧整数Java-如何从一组图像中按顺序绘制图像,java,arrays,image,animation,Java,Arrays,Image,Animation,我试图创建一个精灵类,每个数组中包含3帧,表示每个方向。这个类将实现一个actionlistener和keylistener,所以当我按下W键时,这个类将设置精灵向前移动的动画,绘制图像帧,给人一种行走的错觉 我在网上研究了一些关于这个问题的话题,结果是一次失败的尝试 我必须初始化当前位置积分和速度,显示它在屏幕上移动的速度。我需要一个计时器类来运行动画 我必须得到矩形边界,这样才能计算碰撞之间的交点。我正在学习游戏开发,并学习了计算机编程的基础知识。我仍然是一个业余的java编程者,并且在解决
public class Sophia {
private int dx;
private int dy;
private int x;
private int y;
private Image img;
private Image[] sophia_down;
private Image[] sophia_left;
private Image[] sophia_right;
private Image[] sophia_up;
public Sophia(){
x = 40;
y = 60;
}
public void move(){
x += dx;
y += dy;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Image getImage(){
return img;
}
int current_frame = 0;
public int tick(){
if(current_frame == 4)current_frame =0;
return current_frame++;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
dx = -1;
ImageIcon ii = new ImageIcon("C:\\imgs\\zion\\sophia_l1.png");
sophia_left = new Image[4];
sophia_left[0] = new ImageIcon("C:\\imgs\\zion\\sophia_l1.png").getImage();
sophia_left[1] = new ImageIcon("C:\\imgs\\zion\\sophia_l2.png").getImage();
sophia_left[2] = new ImageIcon("C:\\imgs\\zion\\sophia_l3.png").getImage();
sophia_left[3] = new ImageIcon("C:\\imgs\\zion\\sophia_l4.png").getImage();
img = sophia_left[tick()];
}
if(key == KeyEvent.VK_RIGHT){
dx = 1;
sophia_right = new Image[4];
sophia_right[0] = new ImageIcon("C:\\imgs\\zion\\sophia_r1.png").getImage();
sophia_right[1] = new ImageIcon("C:\\imgs\\zion\\sophia_r2.png").getImage();
sophia_right[2] = new ImageIcon("C:\\imgs\\zion\\sophia_r3.png").getImage();
sophia_right[3] = new ImageIcon("C:\\imgs\\zion\\sophia_r4.png").getImage();
img = sophia_right[tick()];
}
if(key == KeyEvent.VK_UP){
dy = -1;
sophia_up = new Image[4];
sophia_up[0] = new ImageIcon("C:\\imgs\\zion\\sophia_u1.png").getImage();
sophia_up[1] = new ImageIcon("C:\\imgs\\zion\\sophia_u2.png").getImage();
sophia_up[2] = new ImageIcon("C:\\imgs\\zion\\sophia_u3.png").getImage();
sophia_up[3] = new ImageIcon("C:\\imgs\\zion\\sophia_u4.png").getImage();
img = sophia_up[tick()];
}
if(key == KeyEvent.VK_DOWN){
dy = 1;
ImageIcon ii = new ImageIcon("C:\\imgs\\zion\\sophia_d1.png");
sophia_down = new Image[4];
sophia_down[0] = new ImageIcon("C:\\imgs\\zion\\sophia_d1.png").getImage();
sophia_down[1] = new ImageIcon("C:\\imgs\\zion\\sophia_d2.png").getImage();
sophia_down[2] = new ImageIcon("C:\\imgs\\zion\\sophia_d3.png").getImage();
sophia_down[3] = new ImageIcon("C:\\imgs\\zion\\sophia_d4.png").getImage();
img = sophia_down[tick()];
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
dx = 0;
current_frame = 0;
}
if(key == KeyEvent.VK_RIGHT){
dx = 0;
current_frame = 0;
}
if(key == KeyEvent.VK_UP){
dy = 0;
current_frame = 0;
}
if(key == KeyEvent.VK_DOWN){
dy = 0;
current_frame = 0;
}
}
}
这里是绘制图像的地方
public class Map extends JPanel implements ActionListener {
Timer t;
Sophia sophia;
public Map(){
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.black);
setDoubleBuffered(true);
sophia = new Sophia();
npc = new NPC();
t = new Timer(5, this);
t.start();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(sophia.getImage(), sophia.getX(), sophia.getY(), this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e){
sophia.move();
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e){
sophia.keyReleased(e);
}
public void keyPressed(KeyEvent e){
sophia.keyPressed(e);
}
}
}
这是主课
public class Zion extends JFrame {
int screenWidth = 640;
int screenHeight = 480;
public Zion(){
Map m = new Map();
add(m);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,600);
setLocationRelativeTo(null);
setTitle("Zion - Version: 0.3");
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Zion();
}
}
如何减慢动画的速度。图像变快了。没有延迟我找到了如何通过使用计时器增加帧来减慢动画的速度,因为在保存所有图形调用的JPanel中有一个单独的计时器可以重新绘制 在本课程中,你将看到一个精灵来回移动。我甚至为碰撞检测初始化了一个边界框
public class NPC {
private int x;
private int y;
private int dx;
private int dy;
Image npc_img;
Image[] npc_l;
Image[] npc_r;
Image[] npc_d;
Image[] npc_u;
Rectangle boundBox;
int current_frame = 0;
int distance = 0;
Timer t = new Timer(300, new ActionListener(){
public void actionPerformed(ActionEvent e){
current_frame++;
if(current_frame == 4)current_frame = 0;
initLeft(current_frame);
if(dx == -1 && dy == 0)initLeft(current_frame);
if(dx == 1 && dy == 0)initRight(current_frame);
if(dy == -1 && dx == 0)initUp(current_frame);
if(dy == 1 && dx == 0)initDown(current_frame);
distance++;
if(distance <= 5){
dx = -1;
dy = 0;
}
if(distance >= 5){
dx = 1;
dy = 0;
}
if(distance == 9)distance = 0;
}
});
public int getX(){return x;}
public int getY(){return y;}
public void move(){
x += dx;
y += dy;
boundBox = new Rectangle(x, y, 60, 60);
}
public Image getImage(){return npc_img;}
public void initLeft(int frame){
npc_l = new Image[4];
npc_l[0] = new ImageIcon("C:\\imgs\\zion\\npc_l1.png").getImage();
npc_l[1] = new ImageIcon("C:\\imgs\\zion\\npc_l2.png").getImage();
npc_l[2] = new ImageIcon("C:\\imgs\\zion\\npc_l3.png").getImage();
npc_l[3] = new ImageIcon("C:\\imgs\\zion\\npc_l4.png").getImage();
npc_img = npc_l[frame];
}
public void initRight(int frame){
npc_r = new Image[4];
npc_r[0] = new ImageIcon("C:\\imgs\\zion\\npc_r1.png").getImage();
npc_r[1] = new ImageIcon("C:\\imgs\\zion\\npc_r2.png").getImage();
npc_r[2] = new ImageIcon("C:\\imgs\\zion\\npc_r3.png").getImage();
npc_r[3] = new ImageIcon("C:\\imgs\\zion\\npc_r4.png").getImage();
npc_img = npc_r[frame];
}public void initDown(int frame){
npc_d = new Image[4];
npc_d[0] = new ImageIcon("C:\\imgs\\zion\\npc_d1.png").getImage();
npc_d[1] = new ImageIcon("C:\\imgs\\zion\\npc_d2.png").getImage();
npc_d[2] = new ImageIcon("C:\\imgs\\zion\\npc_d3.png").getImage();
npc_d[3] = new ImageIcon("C:\\imgs\\zion\\npc_d4.png").getImage();
npc_img = npc_d[frame];
}
public void initUp(int frame){
npc_u = new Image[4];
npc_u[0] = new ImageIcon("C:\\imgs\\zion\\npc_u1.png").getImage();
npc_u[1] = new ImageIcon("C:\\imgs\\zion\\npc_u2.png").getImage();
npc_u[2] = new ImageIcon("C:\\imgs\\zion\\npc_u3.png").getImage();
npc_u[3] = new ImageIcon("C:\\imgs\\zion\\npc_u4.png").getImage();
npc_img = npc_u[frame];
}
public void interact(Graphics g){
g.setColor(Color.red);
g.drawString("Greetings", (x - 20), (y - 10));
}
public NPC(){
x = 250;
y = 250;
t.start();
}
}
更新了索菲亚的课程。哪个是运动员
public class Sophia {
private int dx;
private int dy;
private int x;
private int y;
private Image img;
private Image[] sophia_down;
private Image[] sophia_left;
private Image[] sophia_right;
private Image[] sophia_up;
Rectangle boundBox;
public Sophia(){
x = 40;
y = 60;
}
public void move(){
x += dx;
y += dy;
boundBox = new Rectangle(x, y, 62, 62);
t.start();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Image getImage(){
return img;
}
int current_frame = 0;
Timer t = new Timer(150, new ActionListener(){
public void actionPerformed(ActionEvent e){
current_frame++;
if(current_frame == 4)current_frame = 0;
}
});
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
dx = -1;
sophia_left = new Image[4];
sophia_left[0] = new ImageIcon("C:\\imgs\\zion\\sophia_l1.png").getImage();
sophia_left[1] = new ImageIcon("C:\\imgs\\zion\\sophia_l2.png").getImage();
sophia_left[2] = new ImageIcon("C:\\imgs\\zion\\sophia_l3.png").getImage();
sophia_left[3] = new ImageIcon("C:\\imgs\\zion\\sophia_l4.png").getImage();
img = sophia_left[current_frame];
}
if(key == KeyEvent.VK_RIGHT){
dx = 1;
sophia_right = new Image[4];
sophia_right[0] = new ImageIcon("C:\\imgs\\zion\\sophia_r1.png").getImage();
sophia_right[1] = new ImageIcon("C:\\imgs\\zion\\sophia_r2.png").getImage();
sophia_right[2] = new ImageIcon("C:\\imgs\\zion\\sophia_r3.png").getImage();
sophia_right[3] = new ImageIcon("C:\\imgs\\zion\\sophia_r4.png").getImage();
img = sophia_right[current_frame];
}
if(key == KeyEvent.VK_UP){
dy = -1;
sophia_up = new Image[4];
sophia_up[0] = new ImageIcon("C:\\imgs\\zion\\sophia_u1.png").getImage();
sophia_up[1] = new ImageIcon("C:\\imgs\\zion\\sophia_u2.png").getImage();
sophia_up[2] = new ImageIcon("C:\\imgs\\zion\\sophia_u3.png").getImage();
sophia_up[3] = new ImageIcon("C:\\imgs\\zion\\sophia_u4.png").getImage();
img = sophia_up[current_frame];
}
if(key == KeyEvent.VK_DOWN){
dy = 1;
sophia_down = new Image[4];
sophia_down[0] = new ImageIcon("C:\\imgs\\zion\\sophia_d1.png").getImage();
sophia_down[1] = new ImageIcon("C:\\imgs\\zion\\sophia_d2.png").getImage();
sophia_down[2] = new ImageIcon("C:\\imgs\\zion\\sophia_d3.png").getImage();
sophia_down[3] = new ImageIcon("C:\\imgs\\zion\\sophia_d4.png").getImage();
img = sophia_down[current_frame];
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
dx = 0;
}
if(key == KeyEvent.VK_RIGHT){
dx = 0;
}
if(key == KeyEvent.VK_UP){
dy = 0;
}
if(key == KeyEvent.VK_DOWN){
dy = 0;
}
}
}
“…结果是一次失败的尝试。”
——您没有向我们显示(?)。您需要知道动画的总体帧速率和循环中的当前帧,例如,如果渲染25fps,精灵工作表中的每一帧都需要在每个动画周期中显示8.3帧。每移动2个像素,该帧将向哪个方向递增。我可以加载图像并将其显示在Jpanel上。我无法做到的是加载一组图像,并在屏幕上每移动2个像素,每帧显示一幅图像。我也不想要像一系列图像一样的轨迹。我将不得不处理和重新油漆。我很难把代码拼在一起。如果有人能写一个有效的例子就好了。这就是我的研究让我走了多远。编辑
public class Sophia {
private int dx;
private int dy;
private int x;
private int y;
private Image img;
private Image[] sophia_down;
private Image[] sophia_left;
private Image[] sophia_right;
private Image[] sophia_up;
Rectangle boundBox;
public Sophia(){
x = 40;
y = 60;
}
public void move(){
x += dx;
y += dy;
boundBox = new Rectangle(x, y, 62, 62);
t.start();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Image getImage(){
return img;
}
int current_frame = 0;
Timer t = new Timer(150, new ActionListener(){
public void actionPerformed(ActionEvent e){
current_frame++;
if(current_frame == 4)current_frame = 0;
}
});
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
dx = -1;
sophia_left = new Image[4];
sophia_left[0] = new ImageIcon("C:\\imgs\\zion\\sophia_l1.png").getImage();
sophia_left[1] = new ImageIcon("C:\\imgs\\zion\\sophia_l2.png").getImage();
sophia_left[2] = new ImageIcon("C:\\imgs\\zion\\sophia_l3.png").getImage();
sophia_left[3] = new ImageIcon("C:\\imgs\\zion\\sophia_l4.png").getImage();
img = sophia_left[current_frame];
}
if(key == KeyEvent.VK_RIGHT){
dx = 1;
sophia_right = new Image[4];
sophia_right[0] = new ImageIcon("C:\\imgs\\zion\\sophia_r1.png").getImage();
sophia_right[1] = new ImageIcon("C:\\imgs\\zion\\sophia_r2.png").getImage();
sophia_right[2] = new ImageIcon("C:\\imgs\\zion\\sophia_r3.png").getImage();
sophia_right[3] = new ImageIcon("C:\\imgs\\zion\\sophia_r4.png").getImage();
img = sophia_right[current_frame];
}
if(key == KeyEvent.VK_UP){
dy = -1;
sophia_up = new Image[4];
sophia_up[0] = new ImageIcon("C:\\imgs\\zion\\sophia_u1.png").getImage();
sophia_up[1] = new ImageIcon("C:\\imgs\\zion\\sophia_u2.png").getImage();
sophia_up[2] = new ImageIcon("C:\\imgs\\zion\\sophia_u3.png").getImage();
sophia_up[3] = new ImageIcon("C:\\imgs\\zion\\sophia_u4.png").getImage();
img = sophia_up[current_frame];
}
if(key == KeyEvent.VK_DOWN){
dy = 1;
sophia_down = new Image[4];
sophia_down[0] = new ImageIcon("C:\\imgs\\zion\\sophia_d1.png").getImage();
sophia_down[1] = new ImageIcon("C:\\imgs\\zion\\sophia_d2.png").getImage();
sophia_down[2] = new ImageIcon("C:\\imgs\\zion\\sophia_d3.png").getImage();
sophia_down[3] = new ImageIcon("C:\\imgs\\zion\\sophia_d4.png").getImage();
img = sophia_down[current_frame];
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
dx = 0;
}
if(key == KeyEvent.VK_RIGHT){
dx = 0;
}
if(key == KeyEvent.VK_UP){
dy = 0;
}
if(key == KeyEvent.VK_DOWN){
dy = 0;
}
}
}