Hey Guys i wonder if any one could help me in setting a background image for JDesktopPane
instead of changeing background colors...

i'll be gratefull for Detailed Describtion of any written code

Thank you people in advance.

Recommended Answers

All 7 Replies

Create a subclass of JDesktopPane and override paintComponent(Graphics) to draw the image.

i already had an instance of JDesktopPane in my Class which Extends JFrame

JDesktopPane Desktop=new JDesktopPane

the Question here is How to use this instance to set Background image on Desktop

As I already said above, you need to subclass JDesktopPane and override paintComponent()

class ImageDesktopPane extends JDesktopPane {
  public void paintComponent(Graphics g){
    g.drawImage( ... )
  }
}

i'm Sorry it Doesn't work ...... try make it Simpler if Could

"It doesn't work" is not a description of the difficulties you're having. How can anyone know what it was that you tried? What errors occurred?

Post the code. Post error messages or specific questions.

I'm not going to guess what it was that you did that did not work and I won't just write the code for you.

that's simplier as I ever seen, what do you meaning about this one

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class Background extends JPanel {

    private static final long serialVersionUID = 1L;
    private BufferedImage image;
    private Ellipse2D.Double ball;

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if (image == null) {
            initImage();
        }
        g2.drawImage(image, 0, 0, this);
        g2.setPaint(Color.red);
        g2.fill(ball);
    }

    private void initImage() {
        int w = getWidth();
        int h = getHeight();
        int type = BufferedImage.TYPE_INT_RGB;
        image = new BufferedImage(w, h, type);
        Graphics2D g2 = image.createGraphics();
        GradientPaint gradient = new GradientPaint(0, 0, Color.white, w, h, Color.darkGray);
        g2.setPaint(gradient);
        g2.fillRect(0, 0, w, h);
        g2.dispose();
        int d = Math.min(w, h) / 4; // initialize ball
        ball = new Ellipse2D.Double(w / 3, h / 4, d, d);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new Background());
        f.setSize(400, 400);
        f.setLocation(200, 200);
        f.setVisible(true);
    }
}

@ Ezzaral,

hmmmm you are right :-), my post can't help for his ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.