Why are you using addActionListener(this)? Adding actionListsners doesn't seem to be a topic which comes up regularly now, but it did a couple of years back. Look at this sort of post.
Why are you overloading the paintComponent method? Why didn't you call super.paintComponent(g) at the beginning of paintComponent()? Why have you given it public access; it should be protected? How do you know what you omitted is irrelevant? When you call the overloaded paintCompoent method, you are probably only painting part of the panel. Don't know a lot about images myself, but suggest:
Ask a moderator to move this thread to Swing/AWT.
Call repaint(); in your actionPerformed() method
If you insist on overloading paintComponent() call the overloaded method from the overridden method and pass the Graphics object already available.
Try moving the rabbit icon to the same location as the lynx.
If the lynx vanishes and the rabbit appears, it might be because you are only painting enough panel to show one image.
That's all I can suggest. If that doesn't work, maybe somebody else will know better.
perhaps I should have explained a little better. This class is in charge of all graphics for a predator/prey simulator. I am still testing it right now and all the test code does is call the initGui() method. I am using actionListeners because that is how I learned to do it in Head First Java and I don't want to learn a new concept in the middle of a project. The reason I use a overloaded paintComponent() method is because eventually I want to be able to draw my lynxIcon temporally and separately than my rabbitIcon.
I don't want you to think "oh great, another guy who wants us to debug his code for him" Think of it more like a puzzle of why rabbitIcon is not being drawn but lynxIcon is, because as far as I can tell they are both initiated and and called the same way.
ShapeIcon is just a subclass of ImageIcon.
Joined: Oct 13, 2005
We don't have lynxes in this country; we have to make do with foxes. I have just found out that the drawImage() methods of Graphics appear to have a boolean return. Before doing anything complicated, change the bit about System.out.println("Drawn lynx"); to something like thisGo through the API for the graphics and Graphics2D classes for the drawImage methods.
I didn't say not to use actionListeners; I said not to use addActionListener(this). You want a different object for each Listener; whether you use public classes, private classes or anonymous inner classes depends on whether you are going to use the Listener in one place or several.
Lots more details in the older posts.
You are not displaying the two images in the same location, one appears in 0, 50 and the other at 50, 0. Try commenting out the lynx and see whether you can get the rabbit to appear on its own.
The usual way to write an actionListener would be something like thisAnd the conventional way to write paintComponent is like thisMake sure to use paintComponent not paintComponents, and I checked, it is protected access. It is called automatically from repaint(), whenever the JPanel is brought to the front, etc.
I think the overloaded paintComponent method might be the real problem. Try putting a paintYourself(Graphics g, ImageObserver panel) method in rabbitPicture and lynxPicture objects, then you can use that method to show the images. The Graphics object will take a reference to the Panel with it, but you will have to pass a 2nd reference for the Observer parameter. The location will of course be maintained by the rabbitImage and lynxImage objects.