Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Background Image

  1. #1
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Background Image

    Hello,
    I want to set a background image in Java but nothing is working...
    I used this code:

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.FlowLayout;
    
    class Background extends JPanel{
        
        public Background()
        {
            setOpaque(false);
            setLayout(new FlowLayout());
        }
        
        public static void main(String args[]){
            JFrame myFrame = new JFrame("x.png");
            JButton button1 = new JButton("Button1!");
            JButton button2 = new JButton("Button2!");
            Background c = new Background();
            c.add(button1);
            c.add(button2);
            myFrame.add(c);
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setSize(800,600);
            myFrame.setVisible(true);
        }
        public void paint (Graphics g){
            Image a = Toolkit.getDefaultToolkit().createImage("b.png");
            g.drawImage(a, 0, 0, getSize().width, getSize().height, this);
            super.paint(g);
        }
    }
    Everything works fine except the picture, it's not showing. The b.png file is in the folder where the java file is.
    What should I do?

    Thank you!

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,976
    Rep Power
    5

    Default Re: Background Image

    Don't override paint(), override paintComponent().

    And just to note: the 'problem' here is likely that that super.paint() is painting a background over your image so you don't see it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Re: Background Image

    After I add the background I want to add some text on it in different places. How can I do that?

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,976
    Rep Power
    5

    Default Re: Background Image

    You go back to the Swing tutorial and you figure it out. Don't fall in the trap that you now demand ALL your questions to be answered because you started a forum post. Is your original problem solved or not?
    Flopet17 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Re: Background Image

    Yes, I managed to add a background, but now when I add text with JPanel it doesn't show.
    But you are right, I have to figure it out myself. Thank you

  6. #6
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Background Image

    @Flopet17, your other post at No text should similarly apply here.

Similar Threads

  1. Applet Background image
    By eagerfiza in forum Java Applets
    Replies: 3
    Last Post: 06-21-2011, 08:05 PM
  2. use image for background
    By tony_stark in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2011, 08:20 PM
  3. [Help] W/ background image
    By gundum584 in forum New To Java
    Replies: 9
    Last Post: 01-10-2011, 05:48 AM
  4. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 PM
  5. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •