gpt4 book ai didi

java - 将移动对象添加到 JFrame

转载 作者:行者123 更新时间:2023-12-02 18:17:43 24 4
gpt4 key购买 nike

我正在创建一个java游戏。游戏中有一个英雄和一个泡泡。当我按下箭头键时,英雄应该移动,并且气泡应该有连续的对角线移动。当我分别将英雄或气泡直接添加到 JFrame 中时,我得到了所需的行为,但是当我将它们都添加时,我只得到一个非常小的正方形!我尝试将它们添加到同一个 JPanel 中,然后将该 JPanel 添加到 JFrame 中,但它不起作用。也许我必须为 JPanel 定义某种类型的布局。

我做错了什么?

代码:

public class Pang {
public static void main(String[] args) {

JFrame f=new JFrame();
JPanel gamePanel=new JPanel();
gamePanel.setPreferredSize(new Dimension(800, 600));


DrawHero d=new DrawHero();
DrawBubble bubble=new DrawBubble();

gamePanel.add(d);
gamePanel.add(bubble);

f.add(gamePanel);

f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 600);
}
}


public class DrawHero extends JPanel implements ActionListener, KeyListener {
Timer myTimer = new Timer(5, this);
int x = 0, y = 0, dx = 0, dy = 0, step = 10;
private transient Image imageHero = null;

public DrawHero() {
myTimer.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);

}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

imageHero = getHeroImage();

g2.drawImage(imageHero, x, y, 40, 40, null);

}

public Image getHeroImage() {
Image image = null;

image = getImage("hero.png");
return image;
}

public Image getImage(String path) {
Image tempImage = null;
try {
URL heroiURL = DrawHero.class.getResource(path);
tempImage = Toolkit.getDefaultToolkit().getImage(heroiURL);
} catch (Exception e) {
System.out.println("Error loading hero image! - "
+ e.getMessage());
}
return tempImage;
}

public void actionPerformed(ActionEvent e) {

repaint();
}

public void moveUp() {

y = y - step;

}

public void moveDown() {
y = y + step;

}

public void moveLeft() {

x = x - step;

}

public void moveRight() {
x = x + step;

}

public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
moveUp();
}
if (keyCode == KeyEvent.VK_DOWN) {
moveDown();
}
if (keyCode == KeyEvent.VK_LEFT) {
moveLeft();
}
if (keyCode == KeyEvent.VK_RIGHT) {
moveRight();
}
}

public void keyTyped(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
}
}


public class DrawBubble extends JPanel implements ActionListener, KeyListener {
Timer myTimer = new Timer(5, this);
int x = 100, y = 200, dx = 0, dy = 0, step = 10;
private transient Image imageHero = null;

public DrawBubble() {
myTimer.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);

}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fill(new Ellipse2D.Double(x, y, 40, 40));


}


public void actionPerformed(ActionEvent e) {

x=x+dx;
y=y+dy;
repaint();
}

public void moveBubble() {
dy=2;
dx=2;
}



public void keyPressed(KeyEvent e) {
moveBubble();

}

public void keyTyped(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
}
}

最佳答案

我建议 DrawHeroDrawBubble(应分别称为 HeroBubble)都不应该扩展任何JComponent。相反,每个人都应该简单地知道如何在请求时将自己绘制到传递给它的 Graphics 对象。

然后,单个 GameFieldPlayingArea 类应保留对所有 Bubble 对象和 Hero 的引用,并且draw 调用这些对象的 draw(Graphics) 方法。

使用这种方法,无需担心 GameField 组件内的布局(它们变得无关紧要)。

这是我在this answer中渲染静止物体所追求的基本策略。至 [ Collision detection with complex shapes .

关于java - 将移动对象添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860228/

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