3 Replies - 9278 Views - Last Post: 13 July 2011 - 04:32 AM Rate Topic: -----

#1 bizhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-July 11

Set Image As JFrame Background

Posted 12 July 2011 - 04:57 AM

I wanted to set an image as background for a JFrame.

The code i have is

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class GuiTestBed extends JPanel
{
    private static final long serialVersionUID = 1L;

    public static void main(String args[])
    {
        JFrame frame = new JFrame("GUI Test Bed 2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(765,503);
        frame.getContentPane().setBackground(Color.BLUE);
        
        String path = "http://www.brunswick.k12.me.us/bjh/depart/curric/landforms/images/hill.gif";
        
        JLabel myLabel = new JLabel();
        
        try
        {
            URL url = new URL(path);
            BufferedImage image = ImageIO.read(url);
            myLabel = new JLabel(new ImageIcon(image));
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
                
        
        frame.getContentPane().add(myLabel);
        //frame.getContentPane().add(new GuiTestBed());
        frame.setVisible(true);
    }
    
    public void paint(Graphics g)
    {
        g.setColor(Color.WHITE);
        g.setFont(new Font("Comic Sans Ms", Font.BOLD, 14));
        g.drawString("Timer: 1:04:32" , 550, 260);
        g.drawString("Edu XP : 22223" , 550, 280);
    }
}




Now if i uncomment

//frame.getContentPane().add(new GuiTestBed());



The image and background color stop showing.

I need the paint() work (as i am making a paint for another application) with a background image for JFrame (background image will be a screen shot of application i am making the paint).

Anyone know how to get JFrame show background image and get the pain() work ?

Is This A Good Question/Topic? 0
  • +

Replies To: Set Image As JFrame Background

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,646
  • Joined: 27-December 08

Re: Set Image As JFrame Background

Posted 12 July 2011 - 05:44 AM

Really, you're safer implementing the background image in the JPanel, and setting that as the content pane for the JFrame. On the JFrame side, make sure you invoke super.paint() first to handle key super-class functionality, then try using the Graphics drawImage() method to manually paint the background image.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Set Image As JFrame Background

Posted 12 July 2011 - 04:00 PM

sked so many times, I wrote a Snippet ahout it

http://www.dreaminco...snippet5599.htm
Was This Post Helpful? 0
  • +
  • -

#4 bizhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-July 11

Re: Set Image As JFrame Background

Posted 13 July 2011 - 04:32 AM

Thank you very much. I got it working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1