gpt4 book ai didi

java - paintComponent 不在 Jpanel 上绘制 Sprite

转载 作者:行者123 更新时间:2023-11-30 07:16:19 27 4
gpt4 key购买 nike

我正在尝试使用 paintComponent 方法将 Sprite 绘制到 Jpanel 上,以用于我正在创建的游戏。我正在使用 Toolkit 来获取图像。图片没有显示,我不知道是什么问题。 Sprite 在一个数组中,因为我将为同一功能使用不同的 Sprite 。

这是我的类(class):

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(..) 加载图像的原因之一。当它失败时,它会提供许多可爱的堆栈跟踪来警告我们。 :)

关于java - paintComponent 不在 Jpanel 上绘制 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17089268/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com