Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drawing

 
AKEEL MALIK
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to draw using paint method to a panel or a canvas.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ramakanth NVL
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I create a seperate canvas or Jpanel class
 
Ramakanth NVL
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic