我正在尝试使用paintComponent方法将一个精灵绘制到Jpanel上,用于我正在创建的游戏。我正在使用工具包来获取图像。图片未显示,我不知道问题出在哪里。该精灵位于数组中,因为我将对同一函数使用不同的精灵。
这是我的课:
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);
}
}
最佳答案
你的例子:
如果我将"knightsword.jpg"
替换为URL("http://i.stack.imgur.com/gJmeJ.png")
,则可以工作(需要对代码进行一些调整)。
如果仍为"knightsword.jpg"
,则无提示地失败。
结论:找不到图像。
这是我更喜欢使用ImageIO.read(..)
加载图像的原因之一。当它失败时,它会提供许多可爱的堆栈跟踪来警告我们。 :)