We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,605 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Java panel background image

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

3
Contributors
4
Replies
1 Day
Discussion Span
8 Months Ago
Last Updated
5
Views
W1nst0n
Newbie Poster
2 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,477
Skill Endorsements: 33

Can someone help me do that cause i have no clue

W1nst0n
Newbie Poster
2 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,477
Skill Endorsements: 33

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

mKorbel
Nearly a Posting Virtuoso
1,228 posts since Feb 2011
Reputation Points: 482
Solved Threads: 244
Skill Endorsements: 14

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.3056 seconds using 2.67MB