This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Drawing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing" Watch "Drawing" New topic
Author

Drawing

AKEEL MALIK
Greenhorn

Joined: Feb 03, 2005
Posts: 26
I have go a JApplet that displays a Image through tha Paint method. Can Anyone please tell me how to draw the image in a panel and also on canvas.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The procedure for drawing an image on a java.awt.Panel or on a Canvas is much the same as shown below. It is not recommended to mix AWT and Swing so you use a java.awt.Applet vis–a–vis JApplet and the Panel/Canvas paint method with signature

In AWT drawing you may find value in overriding the update method; sometimes like this to avoid flickering

but this will vary according to what you are doing and want to achieve. Swing painting tends to be easier than painting in the AWT. The override of the getPreferredSize method is for the JScrollPane. You can use the setPreferredSize instead if you like. setPreferredSize is part of the Component api (okay for use in AWT) as of j2se 1.5
AKEEL MALIK
Greenhorn

Joined: Feb 03, 2005
Posts: 26
Do you know how to draw using paint method to a panel or a canvas.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Ramakanth NVL
Greenhorn

Joined: Feb 04, 2005
Posts: 3
You need to get the container's graphics context and then load the image or any graphics element onto the particular container.

Example here:

AKEEL MALIK
Greenhorn

Joined: Feb 03, 2005
Posts: 26
Do I create a seperate canvas or Jpanel class
Ramakanth NVL
Greenhorn

Joined: Feb 04, 2005
Posts: 3
You have to getgraphics() for which ever panel you are want to draw into.

Say for eg.
You are adding imagepanel to the applet which I suppose u would have done this way.


When u draw an image using pgraphics the (0,0) position is wherever the imagepanel starts. And similarly for cgraphics. Hope its clear to u now.
AKEEL MALIK
Greenhorn

Joined: Feb 03, 2005
Posts: 26
Can you please tell me why when I invoke the paint method my panels and Menubar and sometimes buttons are painted over.
It'll paint the image in the panel but ma other components and panel will be jst covered white
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing