Eilleen
Eilleen

Reputation: 403

Java Images in the GUI class

Hello i am developing a custom clock application.

My GUI works find my functionality is fine however i have one problem for 3 days now. I can't get my GUI to display an image in the background without hiding my components.

here is my code of the GUI class

public void makeFrame()  {
    contentPane.setLayout(new BorderLayout());
    contentPane.add(panel1, BorderLayout.NORTH);
    contentPane.add(panel2, BorderLayout.CENTER);
    contentPane.add(panel3, BorderLayout.SOUTH);
    contentPane.add(panel4, BorderLayout.WEST);
    contentPane.add(panel5, BorderLayout.EAST);
    panel1.add(label1);
    panel2.setLayout(new GridLayout(3,4));
    panel2.add(time);
    panel2.add(label2);
    panel2.add(stopwatch);
    panel3.setLayout(new FlowLayout());
    panel4.setLayout(new FlowLayout());
    panel5.add(alarm);
    panel5.add(change);
    panel5.setLayout(new FlowLayout());
    label1.setFont(new Font("Arial", Font.PLAIN, 90));
    label1.setForeground(Color.BLUE);
    label2.setFont(new Font("Arial", Font.PLAIN, 70));
    label2.setForeground(Color.RED);
    time.setEditable(true);
    time.setText("Sample Time: n/ 13:45:23 ");
    time.setFont(new Font("Arial", Font.PLAIN, 60));
    stopwatch.setFont(new Font("Arial", Font.PLAIN, 45));
    stopwatch.setSize(20,20);
    stopwatch.setText("00 : 00 : 00");
    stopwatch.setEditable(false);
    stopwatch.add(rounds);

    frame = new JFrame("Clock");
    frame.setLayout(null);
    frame.setSize(600,900);
    paint();
    frame.setContentPane(contentPane);
    makeMenu();
    comboBox();
    stopWatch();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.validate();
    frame.pack();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);


}

and the paint() method

 public void paint() {
     BufferedImage img = null;
        try {
            //load the image
            img = ImageIO.read(new File("C:/Users/User/workspace/Alarm Clock/src/Clock/clock.jpg"));

            ImageIcon image = new ImageIcon(img);
            JLabel label = new JLabel(image);
            frame.setContentPane(label);

        } catch (IOException e) {

        }
 }

Upvotes: 0

Views: 66

Answers (3)

camickr
camickr

Reputation: 324118

contentPane.add(panel1, BorderLayout.NORTH);
contentPane.add(panel2, BorderLayout.CENTER);

Your code is adding panels to the content pane, which is fine.

frame.setContentPane(label);

But then in the paint() method you replace the content pane with the label, so you lose all the original panels.

First of all, you should NOT override paint() on a JFrame. If you ever do custom painting you should override the paintComponent() method of a JPanel and add the panel to the frame. Also, a painting method is for painting only, you should NVEVER create and add a component to the GUI in a painting method. YOu should also NOT read an image in a painting method. Painting should be very efficient.

So to solve your problem I suggest you can use the BackgroundPanel. This is a custom panel that supports painting of images and it will make any component you add to it non-opaque. You can paint the background image 1) at its original size, 2) scaled to fill the panel, 3) tiled.

The basic code would be:

//contentPane.setLayout(new BorderLayout());
BackgroundPanel contentPane = new BackgroundPanel(...); // choose your constructor
frame.setContentPane( contentPane );
contentPane.add(panel1, BorderLayout.NORTH);
...

Upvotes: 2

Rahul
Rahul

Reputation: 299

You can try this

setContentPane(new javax.swing.JLabel(new javax.swing.ImageIcon(getClass().getResource("background_image.jpg"))));

or you can draw on your JFrame and add a JPanel on it. Now add your components on your JPanel. Make sure you make the JPanel not opaque .This always comes easy setting background images.

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35011

I think what you are looking for is setOpaque

Upvotes: 0

Related Questions