• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

annoying images

 
Ranch Hand
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 97
    Python VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 97
    Python VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 97
    Python VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 97
    Python VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I fixed it! I did it by just making a new rabbit.gif, it must of not saved properly. Thanks again!
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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!
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic