2

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);
}}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Justus2220
  • 21
  • 2
  • Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. One reason is that a Swing based `JLabel` can display an image! – Andrew Thompson Jun 05 '20 at 05:37
  • We have to use AWT, it is required, unfortunately. – Justus2220 Jun 05 '20 at 08:23
  • Who is 'we'? Oh wait.. *"My professor.."* Your professor needs to drag themselves, kicking and screaming, into the 3rd millennium. What is the point of this exercise, BTW? 1) As an aside: For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). .. – Andrew Thompson Jun 05 '20 at 09:30
  • .. 3) Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` – Andrew Thompson Jun 05 '20 at 09:30
  • 4) BTW - Always copy/paste error and exception output! I'm guessing you are referring to a `StackOverflowError`, but I prefer not having to guess. 5) This `ImageEigen` class is trying to do too much. Define it so that all it does is (accept and) paint an image. Then add that class to another panel (`BorderLayout.CENTER`) and add the buttons to the other panel. – Andrew Thompson Jun 05 '20 at 10:05
  • Thank you for your answer. In class, we actually have to do it in two separate programs, which is fairly easy. Out of interest and to see if it is possible, i wanted to do it in one program. Is there a way to do it without Swing? – Justus2220 Jun 05 '20 at 20:11
  • 1
    *"Thank you for your answer."* It was a comment (OK comments) not an answer. *"In class, we actually have to do it in two separate programs"* I am guessing by 'programs' you mean 'classes'. Point (4) describes a situation where there are ***still*** 2 classes. *"Is there a way to do it without Swing?"* Sure, just not one you are likely to get help with. Many of the newer programmers here have never had experience with AWT. Most people here who *could have* helped with an AWT problem a) could not be bothered or b) have forgotten so much about AWT they'd make basic mistakes. – Andrew Thompson Jun 05 '20 at 23:42

0 Answers0