Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Put image on JPanel  RSS feed

 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to add an image to my JPanel, which is invoked from the local applet's folder. I tried to do that, but there is message that it doesn't recognize method getCodeBase(). Later I read that I should put that code in the init() method in JApplet, and I did, I wrote like this:



but I don't know how to "call" image now from the JPanel.

I hope somebody can help.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a JPanel subclass that takes the image in its constructor. Override paintComponent to first call super.paintComponent, then draw the image. See also Rob Camick's Background Panel.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found this example and made constructor in the JPanel, but it doesn't draw image. I put the image in classes folder. Is it maybe wrong folder?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maja neskovic wrote:I have found this example and made constructor in the JPanel, but it doesn't draw image. I put the image in classes folder. Is it maybe wrong folder?


Without seeing your code and knowing precisely where your images are located in relation to the class files or the user.dir, it's hard to know what you're doing wrong. My first suggestion is that you place a judicious number println statements in your code to see if the image is actually being read (non-null, has a reasonable size), then if not, then give us more information with which we can help you.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using NetBeans. It creates build folder and in it classes folder. Then it creates folder for packages. Since I called my package org.me.gps it creates folders org and in it folder me and in it folder gps. Classes are placed in gps folder. I put the image there and also in classes folder just in case that I should maybe put it there.

In JApplet (in the init method) I wrote:



In JPanel (my JPanel is called GPSPanel1) I declared img variable as Image type and wrote:

 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved it.

For everybody with similar problems, just take look at this: http://faq.javaranch.com/java/BackgroundImageOnJPanel

If you don't want your image to be background image, just define its x and y coordinates.

Thank you all for interesting

Greetings!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!