2 Replies - 776 Views - Last Post: 17 June 2014 - 06:54 PM Rate Topic: -----

#1 porkinspullup   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-January 14

How do i make an image background for a window in Java?

Posted 17 June 2014 - 02:16 PM

I would like to learn how to add an image background to a window in java that I can put controls like buttons, textboxes, and checkboxes in front of. I already tried using a JLabel with an ImageIcon but I cannot overlay controls over the JLabel. From what I understand there are multiple ways to do this. What is the best way and how can I do it?
Is This A Good Question/Topic? 0
  • +

Replies To: How do i make an image background for a window in Java?

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,980
  • Joined: 20-September 08

Re: How do i make an image background for a window in Java?

Posted 17 June 2014 - 03:48 PM

Compile and run this - this is the way i normally do it:

import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.util.*;

import javax.imageio.ImageIO;

import javax.swing.*;


public class BackgroundImageDemo extends JFrame {
    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            ImageBackgroundPanel p = new ImageBackgroundPanel(new URL(
                        "http://daffodil.dyndns.biz/webtmp/q.png"));
            p.load();

            JButton b = new JButton("Get the idea?");
            b.addActionListener(new Responder());
            p.add(B)/>;
            setContentPane(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        BackgroundImageDemo f = new BackgroundImageDemo();
                        f.setGui();
                        f.pack();
                        f.setVisible(true);
                    }
                });
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private class ImageBackgroundPanel extends JPanel {
        private Image backgroundImage;
        private URL url;

        public ImageBackgroundPanel(URL url) {
            this.url = url;
        }

        public void load() throws Exception {
            backgroundImage = ImageIO.read(url.openStream());
        }

        @Override
        public Dimension getPreferredSize() {
            Dimension d = new Dimension(backgroundImage.getWidth(this),
                    backgroundImage.getHeight(this));
            System.out.println(d);

            return d;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(backgroundImage, 0, 0, this);
        }
    }

    private class Responder implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(BackgroundImageDemo.this, "Good!");
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 porkinspullup   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-January 14

Re: How do i make an image background for a window in Java?

Posted 17 June 2014 - 06:54 PM

I actually figured it out before you posted. :D Thanks anyways for the help.

First I had to create a JPanel and set it to fill the entire window, and then I set the background of the JPanel with the following code:

 @Override
		      public void paintComponent(Graphics g) {     
		            g.drawImage(image, 0, 0, null);
		      }


It took me hours to figure out how to get that little piece of code to work, because everytime I pasted it into Eclipse I pasted it within my JPanel's constructor. Then it dawned on me that a method within a method doesn't make any sense. So I moved it out of the constructor and put it just below it (still in the same class) and everything worked like a charm.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1