你好,我真的需要你的帮助,如果你能帮助我,我会非常感激。我最近开始学习java
我正在制作一个java程序,允许我的玩家跳转并移动到鼠标的位置我认为我已经成功地做到了但它并不是真正具体的;
我也希望显示我的精灵表,以便他看起来像是在走路,但我在这部分遇到了问题 这是我的代码:
package animation;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.awt.*;
import javax.swing.*;
import animation.Boulle.move;
import gfx.Sprite;
import gfx.SpriteSheet;
public class deux_bulle extends JFrame implements MouseListener, MouseMotionListener {
Monp p;
int X, Y, x = 150, y = 150;
Thread t, t2, t3,t4;
int f = 0;
boolean b = false;
public Graphics g;
private int framedelay;
public static SpriteSheet sheet;
public static gfx.Sprite Sprite[] = new gfx.Sprite[3]; // my sprite sheet
// has 3 pictures
public deux_bulle() {
setSize(400, 400);
p = new Monp();
sheet = new SpriteSheet("/Untitled.png");
add(p);
p.addMouseListener(this);
p.addMouseMotionListener(this);
for (int i = 0; i < Sprite.length; i++) {
Sprite[i] = new Sprite(sheet, i + 1, 1);
}
}
class Monp extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(deux_bulle.Sprite[f].getBufferedIamge(), x, y, 200, 200, null);
}
}
class moveX implements Runnable {
@Override
public void run() {
while (x < X) {
try {
x++;
if (f < 3)
//g.drawImage(deux_bulle.Sprite[f].getBufferedIamge(), x, y, 200, 200, null);
repaint();
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (x > X) {
try {
x--;
repaint();
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class image implements Runnable{
public void run() {
framedelay++;
if (framedelay>=3)
{f++;
if(f>=3)
{f=0;}
framedelay=0;}
}}
class moveY implements Runnable {
@Override
public void run() {
while (y < Y) {
try {
y++;
repaint();
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while (y > Y) {
try {
y--;
repaint();
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class jump implements Runnable {
@Override
public void run() {
int p = y;
while (y != Y) {
try {
y--;
repaint();
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while (y != p) {
try {
y++;
repaint();
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
deux_bulle db = new deux_bulle();
db.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
Y = 70;
t3 = new Thread(new jump());
t3.start();
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent e) {
X = e.getX();
Y = e.getY();
t4 = new Thread(new image());
t = new Thread(new moveX());
t2 = new Thread(new moveY());
t.start();
t2.start();
t4.start();
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
x = e.getX();
y = e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
我的精灵表类
package gfx;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet {
private BufferedImage sheet;
public SpriteSheet(String path) {
try {
sheet= ImageIO.read(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getSprite(int x,int y)
{
return sheet.getSubimage(x*66-66,y*66-66,66,66);}
}
我的精灵课
package gfx;
import java.awt.image.BufferedImage;
public class Sprite {
public SpriteSheet sheet;
public BufferedImage image;
public Sprite(SpriteSheet sheet,int x,int y) {
image= sheet.getSprite(x, y);
}
public BufferedImage getBufferedIamge(){
return image;
}
}