2

对于我正在创建的游戏,我正在尝试使用 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);
    }

}
4

1 回答 1

4

你的例子:

  • 如果我替换"knightsword.jpg"URL("http://i.stack.imgur.com/gJmeJ.png")(需要对代码进行一些调整)。
  • 如果它保持为 ,则静默失败"knightsword.jpg"

结论:找不到图像。

这是我更喜欢ImageIO.read(..)用于加载图像的原因之一。当它失败时,它会提供许多可爱的堆栈跟踪来警告我们。:)

于 2013-06-13T14:41:35.023 回答