java 如果没有在 main 方法中调用,paint() 是如何运行的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7764602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:19:25  来源:igfitidea点击:

how is paint() running without being called in the main method?

javagraphics

提问by Corey

This is a beginner question for java graphics using the awt package. I found this code on the web to draw some simple graphics.

这是使用 awt 包的 java 图形的初学者问题。我在网上找到了这段代码来绘制一些简单的图形。

import java.awt.*;
public class SimpleGraphics extends Canvas{

    /**
     * @param args
     */
    public static void main(String[] args) {
        SimpleGraphics c = new SimpleGraphics();
        c.setBackground(Color.white);
        c.setSize(250, 250);

        Frame f = new Frame();
        f.add(c); 
        f.setLayout(new FlowLayout()); 
        f.setSize(350,350);
        f.setVisible(true);
    }
    public void paint(Graphics g){
        g.setColor(Color.blue);
        g.drawLine(30, 30, 80, 80);
        g.drawRect(20, 150, 100, 100);
        g.fillRect(20, 150, 100, 100);
        g.fillOval(150, 20, 100, 100); 
    }
}

Nowhere in the main method is paint() being called on the canvas. But I ran the program and it works, so how is the paint() method being run?

main 方法中没有任何地方是在画布上调用paint()。但是我运行了这个程序并且它工作了,那么paint()方法是如何运行的呢?

回答by aioobe

The paintmethod is called by the Event Dispatch Thread (EDT) and is basically out of your control.

paint方法由事件调度线程 (EDT) 调用,基本上不受您的控制。

It works as follows: When you realizea user interface (call setVisible(true)in your case), Swing starts the EDT. This EDT thread then runs in the background and, whenever your component needs to be painted, it calls the paintmethod with an appropriate Graphicsinstance for you to use for painting.

它的工作原理如下:当您实现用户界面(setVisible(true)在您的情况下调用)时,Swing 启动 EDT。然后,这个 EDT 线程在后台运行,并且每当您的组件需要绘制时,它都会paint使用适当的Graphics实例调用该方法以供您用于绘制。

So, when is a component "needed" to be repainted? -- For instance when

那么,什么时候“需要”重新绘制组件?-- 例如当

  • The window is resized
  • The component is made visible
  • When you call repaint
  • ...
  • 窗口已调整大小
  • 组件可见
  • 你打电话的时候 repaint
  • ...

Simply assume that it willbe called, whenever it is necessary.

简单地假设它在需要时被调用。

回答by akhilless

Actually you never invoke paint mathod yourself. It gets called automatically whenever the content pane of your frame needs to be repainted. It happens when your frame is rendered for the first time, resized, maximized (after being minimzed), etc.

实际上,您永远不会自己调用paint mathod。每当需要重新绘制框架的内容窗格时,它就会自动调用。它发生在您的框架第一次渲染、调整大小、最大化(最小化后)等时。

回答by adatapost

If you are not aware of how AWT/Swing (render) painting API works then read this article - Painting in AWT and Swing.

如果您不了解 AWT/Swing(渲染)绘制 API 的工作原理,请阅读这篇文章 -在 AWT 和 Swing 中绘制

The Paint Method Regardless of how a paint request is triggered, the AWT uses a "callback" mechanism for painting, and this mechanism is the same for both heavyweight and lightweight components. This means that a program should place the component's rendering code inside a particular overridden method, and the toolkit will invoke this method when it's time to paint. The method to be overridden is in java.awt.Component.

Paint 方法 不管绘制请求是如何触发的,AWT 都使用“回调”机制进行绘制,这种机制对于重量级和轻量级组件都是相同的。这意味着程序应该将组件的渲染代码放在一个特定的重写方法中,并且工具包将在需要绘制时调用该方法。要覆盖的方法在 java.awt.Component 中。