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 ):
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
posted
0
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
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
posted
0
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
posted
0
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 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
posted
0
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?
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.