import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// This application displays an image
public class ImagePanel extends JPanel {
    private Image anImage;

    public ImagePanel() {
        // Load the image
        anImage = Toolkit.getDefaultToolkit().createImage("altree.gif");
        
        setLayout(new FlowLayout(FlowLayout.CENTER, 50,50));

        add(new JLabel(new ImageIcon("brain.gif")));

        JLabel aLabel = new JLabel("A Label");
        aLabel.setForeground(Color.white);
        add(aLabel);

        JButton button1 = new JButton("Button");
        button1.setBackground(Color.red);
        add(button1);

        // Create a 2nd JButton, this one with an icon
        JButton button2 = new JButton("Button", new ImageIcon("brain.gif"));
        button2.setBackground(Color.white);
        add(button2); 
        
        // Set the preferred size of the panel.   The JFrame will adjust the
        // size accordingly though.   Note that we must wait until the 
        // getWidth() and getHeight() methods return a valid result.
        while ((anImage.getWidth(null) == -1) && (anImage.getHeight(null) == -1));
        setPreferredSize(new Dimension(anImage.getWidth(null), anImage.getHeight(null)));
        
    }

    // Display the image
    public void paintComponent(Graphics g) {
    	super.paintComponent(g);
        g.drawImage(anImage, 0, 0, null);
    }

    // Create main method to execute the application
    public static void main(String args[]) {
        JFrame frame = new JFrame("Image Display Test");
        frame.add(new ImagePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();	// Makes size according to panel's preference
        frame.setVisible(true);
    }
}