我一直在試圖讓我的角色在遊戲中移動我正試圖編程一段時間。我設法讓它顯示,但是當我添加應該移動它的代碼時,它不再顯示。我是新來的編程,所以我一直從我的網上找到的一些代碼中進行一些修改,但我不知道我做錯了什麼。程序運行,但沒有任何顯示
Execute.java
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Execute extends JFrame {
public Execute() {
initUI();
}
public static void initUI() {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(600,600);
jf.setTitle("I am the salad baby");
jf.add(new Board());
}
public static void main (String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Execute ex = new Execute();
ex.setVisible(true);
}
});
}
}
Craft.java
import java.awt.image.BufferedImage;
import java.awt.event.KeyEvent;
public class Craft {
private int dx;
private int dy;
private int x;
private int y;
private BufferedImage image;
public Craft() {
initCraft();
}
private void initCraft() {
Painter painter = new Painter();
BufferedImage image = painter.createBufImg("Images/SpriteSheet.png");
x = 40;
y = 60;
}
public void move() {
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public BufferedImage getImage() {
return image;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
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;
}
}
}
Board.java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Craft craft;
private final int DELAY = 10;
public Board() {
initBoard();
}
private void initBoard() {
addKeyListener(new TAdapter());
setFocusable(true);
craft = new Craft();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
Toolkit.getDefaultToolkit().sync();
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
}
@Override
public void actionPerformed(ActionEvent e) {
craft.move();
repaint();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
craft.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
craft.keyPressed(e);
}
}
}
Painter.java
import java.io.IOException;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.imageio.ImageIO;
public class Painter extends JPanel {
BufferedImage createBufImg(String filepath) {
try {
return ImageIO.read(new File(filepath));
} catch (IOException e) {
System.out.println("Could not read image");
}
return null;
}
}
任何輸入都有幫助!
看起來你只是在一個ActionEvent被觸發時繪畫。我從來沒有使用過它,但我不完全確定它是否觸發了按鍵。你確定函數'actionPerformed'被調用嗎? – jonhopkins