我正在尝试使用paintComponent方法在Jpanel上绘制一个精灵,用于我正在创建的游戏。我正在使用Toolkit来获取图像。图像没有显示,我不知道问题是什么。精灵是一个数组,因为我将使用不同的精灵来实现相同的功能。
这是我的班级:
import javax.swing.SwingUtilities;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
public class Game {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public Game() {
createAndShowGUI();
}
private static void createAndShowGUI() {
System.out.println("Game start: " + SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Black Circle");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.pack();
f.setVisible(true);
}
}
class MyPanel extends JPanel {
private int circleX = 500;
private int circleY = 250;
private int circleW = 20;
private int circleH = 20;
private int hiltX1 = 530;
private int hiltY1 = 250;
private int hiltX2 = 536;
private int hiltY2 = 250;
private int bladeX1 = 533;
private int bladeY1 = 255;
private int bladeX2 = 533;
private int bladeY2 = 210;
Image [] weapon = new Image [10];
public int OFFSET = 15;
public MyPanel() {
setFocusable(true);
requestFocusInWindow();
setBorder(BorderFactory.createLineBorder(Color.black));
weapon[0] =
Toolkit.getDefaultToolkit().
getImage("knightsword.jpg");
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_D) {
movecircleRight();
}
else {
e.consume();
}
if (keyCode == KeyEvent.VK_A) {
moveCircleLeft();
}
else {
e.consume();
}
if (keyCode == KeyEvent.VK_W) {
moveCircleUp();
}
else {
e.consume();
}
if (keyCode == KeyEvent.VK_S) {
moveCircleDown();
}
else {
e.consume();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
e.consume();
}
});
}
private void movecircleRight() {
circleX += OFFSET;
bladeX1 += OFFSET;
hiltX1 += OFFSET;
bladeX2 += OFFSET;
hiltX2 += OFFSET;
repaint();
}
private void moveCircleLeft() {
circleX -= OFFSET;
bladeX2 -= OFFSET;
hiltX2 -= OFFSET;
bladeX1 -= OFFSET;
hiltX1 -= OFFSET;
repaint();
}
private void moveCircleDown() {
circleY +=OFFSET;
bladeY1 += OFFSET;
hiltY1 += OFFSET;
bladeY2 += OFFSET;
hiltY2 += OFFSET;
repaint();
}
private void moveCircleUp() {
circleY -= OFFSET;
bladeY2 -= OFFSET;
hiltY2 -= OFFSET;
bladeY1 -= OFFSET;
hiltY1 -= OFFSET;
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(1000, 500);
}
//ImageIcon sword = new ImageIcon( ("knightsword.jpg"));
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(circleX, circleY, circleW, circleH);
g.setColor(Color.BLACK);
g.drawOval(circleX, circleY, circleW, circleH);
g.drawLine(hiltX1, hiltY1, hiltX2, hiltY2); // hilt
g.drawLine(bladeX1, bladeY1, bladeX2, bladeY2); // blade
g.drawImage(weapon[0], 200, 250, this);
}
}
答案 0 :(得分:4)
你的例子:
"knightsword.jpg"
替换为URL("http://i.stack.imgur.com/gJmeJ.png")
(需要对代码进行一些调整)。"knightsword.jpg"
。结论:未找到图像。
这是我更喜欢使用ImageIO.read(..)
加载图片的原因之一。当它失败时,它提供了许多可爱的堆栈跟踪来警告我们。 :)