私が作成しているゲームのために、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);
}
}