Tuesday, March 16, 2010

Repain/paint/update in Java..how to use?

Could someone explain how to use these in Java, and perhaps what the syntax is.





ThanksRepain/paint/update in Java..how to use?
Hello,





Basically, each JAVA component (swing, awt, swt) has a paint method that you could override to paint/alter something on the screen after each screen refresh tick rate.





So when you have a JPanel, you can override the paint by introducing anonymous class from the paint method, or just extend that JPanel to your own panel called MyJPanel and override the paint method as well.





Within the paint method that you overrided you can paint anything ontop of it, the most convenient way is to create a graphics object since it is in the form of public void paint(Graphics g);





Whatever you do with that Graphics class will be applied to that JPanel since you overrided its paint method. If you draw a line like, g.drawLine(0,0,50,0); you will see a line. If you want to use Graphics2D which is a more advance Graphics class, you can just cast your graphics to Graphics2D and your set.





You can do many stuff with that, but you need practice first before doing anything. Take a look at this article and after your done reading it, you will understand a good amount of knowledge based on this:


http://java.sun.com/products/jfc/tsc/art鈥?/a>





Good Luck

No comments:

Post a Comment