gpt4 book ai didi

java - 简单游戏的背景图片?

转载 作者:搜寻专家 更新时间:2023-10-31 19:56:58 25 4
gpt4 key购买 nike

我一直在关注 Java Game Programming for Beginners教程系列,并希望通过应用背景图像进行实验。不幸的是,当我通过 paintComponent 方法渲染它时,它会随着我的 sprite 移动(尽管连续移动一个单位而不是五个);当我通过 paint 方法渲染它时,我得到一个奇怪的、闪烁的框,它与 JFramesetBackground (color) 属性中指定的颜色相匹配,它随着 Sprite 与先前实例的 Sprite 相同(在 paintComponent 中)。

我如何编码图像以保持静态,就像背景应该的那样?

代码:

public class JavaGame extends JFrame{

int x, y;
private Image dbImage;
private Graphics dbg;
Image ghost;
Image bg;


public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();

if(keyCode == e.VK_LEFT){
if(x <= 8)
x = 8;
else
x += -5;
}
if(keyCode == e.VK_RIGHT){
if(x >= 235)
x = 235;
else
x += +5;
}
if(keyCode == e.VK_UP){
if(y <= 18)
y = 18;
else
y += -5;
}
if(keyCode == e.VK_DOWN){
if(y >= 235)
y = 235;
else
y += +5;
}
}
public void keyReleased(KeyEvent e){

}

}

public JavaGame(){
//Load images
ImageIcon i = new ImageIcon("C:/Users/Taylor/workspace/Java game/src/ghost.png");
ghost = i.getImage();

ImageIcon j = new ImageIcon("C:/Users/Taylor/workspace/Java game/src/bg.png");
bg = j.getImage();

//Game properties
addKeyListener(new AL());
setTitle("Java Game");
setSize(500, 500);
setResizable(false);
setVisible(true);
setBackground(Color.GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

x = 150;
y = 150;
}

public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);

dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, x, y, this);
}

public void paintComponent(Graphics g){
g.setColor(Color.WHITE);
g.drawImage(ghost, x, y, this);

repaint();
}


public static void main(String[] args) {
new JavaGame();

}

图片:

rendered through 'paint' rendered through 'paintComponent'

最佳答案

您是随机复制/粘贴代码吗?这就是它的样子。该代码有太多奇怪的方面,我没有将它们全部记录下来(也许对代码审查来说是一个很好的方面)。该示例使用异步方法加载图像(为了获得动画图像,动画)。使用 ImageIO.read(URL) 以同步方式加载静态图像。

这里有一些简短的提示:

  1. 到部署时,图像可能会成为嵌入式资源,File 对象将无法访问这些资源。将它们添加到运行时类路径并通过 URL 访问它们。
  2. 应在 EDT 上启动和更改 Swing GUI(请参阅对 main() 的更改)。
  3. 始终在方法开始时调用 super.paint(g);(或 paintComponent(g))。
  4. 不要扩展框架,不要绘制到顶级组件。而是扩展面板并覆盖 paintComponent()。将面板添加到框架。

代码

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class JavaGame extends JPanel {

int x, y;
private Image dbImage;
private Graphics dbg;
Image ghost;
Image bg;

public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();

if (keyCode == e.VK_LEFT) {
if (x <= 8)
x = 8;
else
x += -5;
}
if (keyCode == e.VK_RIGHT) {
if (x >= 235)
x = 235;
else
x += +5;
}
if (keyCode == e.VK_UP) {
if (y <= 18)
y = 18;
else
y += -5;
}
if (keyCode == e.VK_DOWN) {
if (y >= 235)
y = 235;
else
y += +5;
}
}

public void keyReleased(KeyEvent e) {
}
}

public JavaGame() throws Exception {
// Load images
//ImageIcon i = new ImageIcon(
// "C:/Users/Taylor/workspace/Java game/src/ghost.png");
URL urlGhost = new URL("http://1point1c.org/gif/thum/plnttm.gif");
ghost = Toolkit.getDefaultToolkit().createImage(urlGhost);

//ImageIcon j = new ImageIcon(
// "C:/Users/Taylor/workspace/Java game/src/bg.png");
URL urlBG = new URL("http://pscode.org/media/stromlo2.jpg");
bg = Toolkit.getDefaultToolkit().createImage(urlBG);

setFocusable(true);

// Game properties
addKeyListener(new AL());
x = 150;
y = 150;

ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
repaint();
}
};
Timer timer = new Timer(50,al);
timer.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg, 0, 0, null);

//dbImage = createImage(getWidth(), getHeight());
//dbg = dbImage.getGraphics();
//paintComponent(dbg);
g.drawImage(dbImage, x, y, this);

g.setColor(Color.WHITE);
g.drawImage(ghost, x, y, this);

//repaint();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
JFrame f = new JFrame("Java Game");
f.setSize(500, 500);
f.setResizable(false);
f.setVisible(true);
f.setBackground(Color.GRAY);
f.setContentPane(new JavaGame());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

关于java - 简单游戏的背景图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12082660/

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