File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Add image to JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Add image to JPanel" Watch "Add image to JPanel" New topic
Author

Add image to JPanel

maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
Hello!

I have made applet using Swing, which has one JPanel and one DesktopPane with labels and sliders. I want to draw shapes and images on JPanel, but I don't know how to do it. I tried to draw image, but I lose all my panels and got just picture. I think that main problem is that I don't know how to access to my JPanel on which I want to draw and then to make drawing. Can anybody help?

Here is my code (I made JFrame with all JPanel and DesktopPane and then copied code to applet ):
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Check out http://faq.javaranch.com/java/BackgroundImageOnJPanel


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I already saw it, but I am not sure that I understand. How to specify that I want drawing image and shapes on my JPanel (which is in JApplet)?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

maja neskovic wrote:I already saw it, but I am not sure that I understand. How to specify that I want drawing image and shapes on my JPanel (which is in JApplet)?

Strange. The code you posted does not attempt any painting at all.

1) Subclass JPanel
2) Override paintComponent and include the painting code*
3) Use the an instance of this panel in your applet.

* For custom painting/drawing objects, the Graphics class has convenient drawXXX and fillXXX methods you can use. You should be using the graphics instance available as an argument in the paintComponent. Also check out the Graphics2D class for more advanced painting options. You just cast it like and then invoke the required methods on the g2.
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
For the 1. point: should I remove all code related to my JPanel (which I called glavnipanel) from JApplet code(which I posted) and move all to the subclass? How to maintain connection of subclass with JApplet? I'm sorry if I'm asking silly questions, but I'm beginner in this
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

maja neskovic wrote:For the 1. point: should I remove all code related to my JPanel (which I called glavnipanel) from JApplet code(which I posted) and move all to the subclass? How to maintain connection of subclass with JApplet? I'm sorry if I'm asking silly questions, but I'm beginner in this


Subclass JPanel. Call it say MajaPanel. Put the paint code in this class.
In your existing code, just switch to
That's it. Your existing "connection" is retained.

And not need to say sorry. No question is silly. Asking questions and finding the answers is how we all learn.
So go ahead and ask all you want
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
Thank you for your support I'm very gratefull!

I'll try as you said and ask again
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I did like you said, make the connection, but I still can't display the image. This is what i did in MajaPanel (I inserted image like I did in my previous applet made in AWT - that was my 1. applet where image drawing worked):

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Maneesh Godbole wrote: You just cast it like and then invoke the required methods on the g2.

maja neskovic wrote:Graphics2D g2 = createGraphics2D(d1.width, d1.height);


Why aren't you casting?
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I'm sorry, I missed that part. What is meant by casting? I just replaced my code which you quoted with your quoted code, and wrote g2 everywhere where I messed with painting in my code (replaced g)... Is it ok?

Now I can't see the MajaPanel in my JApplet. I make mistake again, do I?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42933
    
  68
I make mistake again, do I?

That impossible to say without seeing the relevant parts of the code.
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
Here is the code of the JPanel which I created:


I hope it's helpfull
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42933
    
  68
It's not really useful, because we don't know how it's being used by the applet.

Note that Swing does not use the "paint" method, though - it uses "paintComponent": http://java.sun.com/products/jfc/tsc/articles/painting/
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
Ok, thank you

I found one good example for this problem, I modify it and made JPanel look like this:



I hope it can help to anybody with similar problems. I'm going to work, if there any problem occures, I'll have to post it here

Greetings!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19788
    
  20

Add "super.paintComponent(g)" as the first call of your paint component. Otherwise the default painting of the JPanel will not be done. By adding this call you are first painting the JPanel itself, then painting the image over it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
Added Thank you, Rob
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19788
    
  20

You're welcome.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Pity you cross posted this one too just like your other post http://www.java-forums.org/new-java/29024-drawing-image-jpanel.html
Pity for me that is. I feel silly wasting my time.

Like Rob Camick told you in this thread http://www.coderanch.com/t/496280/GUI/java/make-slider-resize-rectangle-move BeForthrightWhenCrossPostingToOtherSites
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I didn't know about that rule, sorry...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add image to JPanel