aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes annoying images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "annoying images" Watch "annoying images" New topic
Author

annoying images

colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

When I run this code lynxIcon pops up but rabbitIcon does not. I have tried everything. The file does exist, it compiles and runs without a problem, the

System.out.println(rabbitIcon);

prints out a valid definition and I have no clue what to try next.



thank you!


www.mormon.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
This reply \u00a9 C Ritchie 2008.

Better way to write �! Or altgraph-shift-C

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.
    colton peterson
    Ranch Hand

    Joined: Nov 18, 2007
    Posts: 97

    How do you know what you omitted is irrelevant?

    It is not called or touched in any way at this point in time.


    Call repaint(); in your actionPerformed() method

    tried that, made it worse(because I don't call my overloaded method from paintComponent(g)


    Try moving the rabbit icon to the same location as the lynx.

    already is in the same file, I have tried moving it around the screen

    Why have you given it public access; it should be protected?

    there is a public paintComponent() as well for JPanel

    If the lynx vanishes and the rabbit appears, it might be because you are only painting enough panel to show one image.


    I tried to move around the location it was being drawn at, no good.
    [ April 08, 2008: Message edited by: colton peterson ]
    colton peterson
    Ranch Hand

    Joined: Nov 18, 2007
    Posts: 97

    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.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    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.

    See whether that helps, and good luck with it.
    colton peterson
    Ranch Hand

    Joined: Nov 18, 2007
    Posts: 97

    Thank you, as of right now I can not try anything, but I will post my results when I get a chance, thanks again!
    colton peterson
    Ranch Hand

    Joined: Nov 18, 2007
    Posts: 97

    I fixed it! I did it by just making a new rabbit.gif, it must of not saved properly. Thanks again!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    Originally posted by colton peterson:
    I fixed it! Thanks again!
    Well done, and you're welcome. It is surprising how simple the answer to some of these annoying questions can be!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: annoying images
     
    Similar Threads
    GUI Problem
    My GUI's catch block won't compile
    repaint paints the wrong component
    Repaint() in ActionPerformed.
    custom shaped gui components?