简体   繁体   中英

Drawing a class that extends JPanel

I have a class that extends JPanel, it contains a draw(Graphics page) method to draw itself.. if I am building a test driver and want to display it on the frame what should I use.. I'm confused between paint(), paintComponent(), getGraphics() etc..

The Class

class Car extends JPanel {

     void draw (Graphics page) {
         ....draw polygons...
     }

* EDIT *

The goal is to animate the car moving across the screen, I changed the draw method to 'paintComponent()' and it showed up! But it needs to move it and repaint it with a timer. So I guess painting it over and over again is the reason it's draw() not paintComponent(). ?

  • paint()

    AWT containers (use Swing) & Swing top level containers such as JFrame & JApplet (don't paint in top level containers - add a JPanel or JComponent ). So basically, don't in this age change the method.

  • paintComponent()

    The vast majority of Swing components.

  • getGraphics()

    BufferedImage (possibly being displayed in a JLabel ). That will provide a Graphics instance, but I prefer using createGraphics() for a Graphics2D instance (rendering hints are wonderful).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM