• Post Reply Bookmark Topic Watch Topic
  • New Topic

paintCompontnt() is not being called  RSS feed

 
Nidhi Nagre
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello experts,

i have little difficulty to understand the actual scene behind the excution of paintComponent() method.
i have a japplet class that has been embedded in jsp page, JPanel serves as contentPane of this class. due to add of few requerements an image have to be drawn at specified coordinates into this JPanel.
in fact, the image has been successfully tracked by mediatracker but due to failure in calling the paintComponent method, it is not been painted yet.

the sample code given below



i made a cross check and found that the excution stops after *run()* has been excuted.
why paintComponent() method is not being called even though repaint() has been witten inside run()'s block.

any help would be much appreciated

regards
san
[ December 18, 2007: Message edited by: Nidhi Nagre ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bit hard to say what's going wrong, since this seems to be only part of the code (there's a try block outside of any method declaration).

But in any case, JApplet does not have a paintComponent method that would be called. You need to define that method in a class that extends JComponent, e.g. a JPanel. You also need to take care to name it properly (paintComponent, not paintComponents as you have it right now). The latter is an AWT method that you should not call.
 
Nidhi Nagre
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it was working just couple of days before when japplet was running using appletviewer. it is now embedded in jsp and obiously running in web browser. one more point, it was not called in appletiviewer too unless mouse drag applet window.

what should i do if i must have to paint an image into JPanel in this context?
[ December 18, 2007: Message edited by: Nidhi Nagre ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what should i do if i must have to paint an image into JPanel in this context?

The Graphics.drawImage method is used for that. It accepts an Image or BufferedImage object as parameter.
 
Nidhi Nagre
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the Graphics.drawImage() method must reside into paintComponent(), right? or can it be used explicitly anywhere?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you need a Graphics object to draw the image, and inside of paintComponent you have one. Where (or why) would you want to do the drawing else where?

(If the image never changes, you can draw it into a BufferedImage, and then draw that into the Graphics object during paintComponent.)
 
Nidhi Nagre
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mr Ulf wrote:
Where (or why) would you want to do the drawing else where?


well, i am intending to paint it into JPanel at specified dimention.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "where" I meant "where in the code". Why wouldn't you want to put drawing code into the paintComponent method?
 
Nidhi Nagre
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, i got solution and its working fine

well, i want to attach a progressbar along with the download process. after looking around sun's tutorial, i came to know the key points to use progress bar but there is an additional class given to use named Task. but just intantiating it only how can i relate it to the image loading thread?

in other words, how can i relate the progress bar to this thread?

regards
san
[ December 19, 2007: Message edited by: Nidhi Nagre ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!