Background image for a simple game?

I followed the Java Game Programming for Beginners series and wanted to experiment using a background image. Unfortunately, when I process it using the method paintComponent, it moves along with my sprite (albeit continuously on one block, unlike five); and when I process it using the drawing method, I get a strange flickering box that matches the color specified in the property setBackground (color) JFrame, and it moves with a sprite identical to the sprite of the previous instance (inside paintComponent).

How can I encode an image to stay still as the background should be?

the code:

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();

}

Images:

rendered through 'paint'rendered through 'paintComponent'

+5
source share
1 answer

/ ? . , ( , ). ( , ). ImageIO.read(URL) .

:

  • , , File. URL-.
  • Swing EDT (. main()).
  • super.paint(g); ( paintComponent(g)) .
  • , . 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();
                }
            }
        });
    }
}
+6

All Articles