5

我一直在关注Java Game Programming for Beginners教程系列,并希望通过应用背景图像来进行实验。不幸的是,当我通过该paintComponent方法渲染它时,它会随着我的精灵移动(尽管连续一个单位而不是五个);当我通过paint方法渲染它时,我得到一个奇怪的闪烁框,它与setBackground (color)属性中指定的颜色相匹配,JFrame并且它与精灵一起移动,与前一个实例的精灵相同(在 内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();

}

图片:

通过“绘画”渲染 通过“paintComponent”渲染

4

1 回答 1

6

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

以下是一些简短的提示:

  1. 当它被部署时,图像可能会成为嵌入式资源,并且不会被File对象访问。将它们添加到运行时类路径并通过 URL 访问它们。
  2. Swing GUI 应该在 EDT 上启动和更改(参见对 的更改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();
                }
            }
        });
    }
}
于 2012-08-23T00:51:25.823 回答