Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JDesktopPane JPanel Trouble

Status
Not open for further replies.

AndyHollywood

Programmer
Joined
Oct 7, 2001
Messages
30
Location
GB
Hello

I have a Jpanel which has some graphics being drawn in it, just lines etc..

I have a JDesktopPane which has INternal Frames in it, and i want the JPanel to be like a background on the JDesktopPane, how would i go about doing this?

Cheers in advance, is been foxing me for days this problem

Cheers

Andy
 
One way to do it is :
Code:
import java.awt.*;
import javax.swing.*;

public class Desktop extends JFrame {

  private Image image;

  public static void main(String[] args) {
    Desktop example = new Desktop("Title");
    example.doIt();
  }

  public Desktop(String title) {
    super(title);
  }

  public void doIt() {
    setSize(500,500);
    setVisible(true);
    Container cp = getContentPane();
    JDesktopPane dp = new MyDesktopPane();
    cp.add(dp,BorderLayout.CENTER);
    JInternalFrame intf = new JInternalFrame("Internal Frame",true,true,true,true);
    intf.setBounds(10,10,250,100);
    intf.setVisible(true);
    dp.add(intf);
  }

  public Image getImage() {
    if (image == null) {
      try {
        image = ImageLoader.loadImage("D:/images/splash.jpg");
      } catch (InterruptedException e) {
      }
    }
    return image;
  }

  public class MyDesktopPane extends JDesktopPane {

    public MyDesktopPane() {
      super();
    }

    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      // You can replace the next line with your drawing methods (or save your drawing as a jpg, ...)
      g.drawImage(getImage(), 10,10,null);
    }
  }

}
=====================================
Code:
import java.awt.*;

public class ImageLoader extends Component {

  private static ImageLoader imageLoader;

  static {
    imageLoader = new ImageLoader();
  }

  private ImageLoader() {
    super();
  }

  public static Image loadImage(String imageName) throws InterruptedException {
    Image image = Toolkit.getDefaultToolkit().getImage(imageName);
    MediaTracker tracker = new MediaTracker(imageLoader);
    tracker.addImage(image,0);
    tracker.waitForID(0);
    return image;
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top