Hi guys, i am trying to add a image as the background for my project but cant get my code right, heres what I got so far

//beginning of 1
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 */
public class makeB extends JPanel {
    private BufferedImage buffer;

    private void initialise() {
        buffer = new BufferedImage(700, 500, BufferedImage.TYPE_INT_RGB);
    }

    private void drawBuffer() {
        Graphics2D b = buffer.createGraphics();
        createBackGround.makeBackground(b,this);
        b.dispose();
    }

     private void drawScreen() {
        Graphics2D g = (Graphics2D) this.getGraphics();
        g.drawImage(buffer, 0, 0, this);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }
     public void startGame() {
            initialise();
            drawBuffer();
            drawScreen();
    }
}
//end

//beginning of 2
import javax.swing.JFrame;

/**
 */
public class gameScreen {
    private makeB panel = new makeB();
    private JFrame jFrame1;
    public gameScreen(){
            panel = new makeB ();
            jFrame1 = new JFrame("&Wave&");
            jFrame1.setSize(706, 528); //to account for the border; size is actually 800x600
            jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame1.setLocationRelativeTo(null); //to make sure that it is in the centre of the screen
            jFrame1.getContentPane().add(panel);
            jFrame1.setResizable(false);
            jFrame1.setVisible(true);

    }
    private void go() {
        panel.startGame();
    }
    public static void main(String[] args) {
        gameScreen newClass = new gameScreen();
        newClass.go();
    }
}
//end


//beginning of 3
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;

/**
 */
public class createBackGround {
    private static String  imageN = "#big-wave#";
    private static Image backg = new ImageIcon(".\\data"+ imageN +".jpg").getImage();


    public static void makeBackground(Graphics2D g2,ImageObserver o) {
    g2.drawImage(backg, 0, 0, o);
    }


}
//end

there are 3 classes there, can someone help me refine it to get it to work
where there is #...# the image goes there, and where there &...& some name goes there

Edited 4 Years Ago by W1nst0n

I don't think anyone has seriously used this technique since double-buffereing (etc) was introduced aroubd Java 1.5 (?).
Simply create a subclass of JPanel. Load your image into memory in the constructor. Override its paintComponent to paint your image onto the Graphics passed as parameter.

You have all the keywords for a Google search right there. There are so many examples on the web, you can't ask anyone to create yet another one.

@JamesCherrill

is not used, but to avoiding of catching the image, I'm not good in Graphics, but I saw a few suggestion to use byte[] and ImageObserver, but for JPanels Background, there is paintComponent best of ways (with boolean for repaint())

This article has been dead for over six months. Start a new discussion instead.