简体   繁体   English

绘制一个扩展JPanel的类

[英]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.. 我有一个扩展JPanel的类,它包含一个绘制(Graphics页面)方法来绘制自己..如果我正在构建一个测试驱动程序并希望在框架上显示它我应该使用什么...我在涂料之间感到困惑( ),paintComponent(),getGraphics()等。

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! 我的目标是让汽车在屏幕上移动,我将绘制方法更改为'paintComponent()'并显示出来! 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(). 所以我想一遍又一遍地绘制它是draw()而不是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 ). AWT容器(使用Swing)和Swing顶级容器,如JFrameJApplet (不要在顶级容器中绘制 - 添加JPanelJComponent )。 So basically, don't in this age change the method. 所以基本上,不要在这个时代改变方法。

  • paintComponent()

    The vast majority of Swing components. 绝大多数Swing组件。

  • getGraphics()

    BufferedImage (possibly being displayed in a JLabel ). BufferedImage (可能显示在JLabel )。 That will provide a Graphics instance, but I prefer using createGraphics() for a Graphics2D instance (rendering hints are wonderful). 这将提供一个Graphics实例,但我更喜欢使用createGraphics()作为Graphics2D实例(渲染提示非常棒)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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