I'm trying to add an image in the center of my BorderLayout, with the ultimate goal to hide the image with a click on the "Hide"-button. I'm trying to add the image with the add()-function. My professor added a new object of the class in the add function, but with the Layout and the Imageclass in two seperate .java-files. I'm trying to do the same in one .java-file. But I can't get it to run without an error and show the image. Could you please help me? What am I doing wrong here?
public class ImageEigen extends Panel
{
Image img;
Button hide_button = new Button("Hide");
Button ende_button = new Button("Quit");
public ImageEigen(Image img)
{
setLayout(new BorderLayout());
setFont(new Font("System", Font.PLAIN, 24));
this.img = img;
add(hide_button, BorderLayout.WEST);
add(ende_button, BorderLayout.EAST);
// add(new ImageEigen(img), BorderLayout.NORTH);
// add(img, BorderLayout.NORTH);
// add(ImageEigen(img), BorderLayout.NORTH);
add(IE(img), BorderLayout.NORTH);
ende_button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 1);
try
{
mt.waitForID(1);
} catch(Exception ex)
{}
}
@Override
public void paint(Graphics g)
{
g.drawImage(img, 0, 0, this);
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(img.getWidth(this), img.getHeight(this));
}
@Override
public void update(Graphics g)
{
paint(g);
}
public static void main(String args[])
{
Frame frame = new Frame("Image-Oberflächentest");
Image img = frame.getToolkit().getImage(args[0]);
ImageEigen IE = new ImageEigen(img);
frame.add(IE);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}}