• 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

Add image to JPanel

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ):
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out http://faq.javaranch.com/java/BackgroundImageOnJPanel
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your support I'm very gratefull!

I'll try as you said and ask again
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I make mistake again, do I?


That impossible to say without seeing the relevant parts of the code.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code of the JPanel which I created:


I hope it's helpfull
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Added Thank you, Rob
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 https://coderanch.com/t/496280/GUI/java/make-slider-resize-rectangle-move BeForthrightWhenCrossPostingToOtherSites
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't know about that rule, sorry...
 
reply
    Bookmark Topic Watch Topic
  • New Topic