paintComponent如何工作? [英] How does paintComponent work?

查看:115
本文介绍了paintComponent如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常noob的问题。我刚刚开始学习Java

This might be a very noob question. I'm just starting to learn Java

我不了解paintComponent方法的操作。我知道如果我想画一些东西,我必须覆盖paintComponent方法。

I don't understand the operation of paintComponent method. I know if I want to draw something, I must override the paintComponent method.

public void paintComponent(Graphics g)
{
   ...
}

但什么时候打电话?我从来没有看到像object.paintComponent(g)这样的东西,但它仍然是在程序运行时绘制的。

But when is it called? I never see anything like "object.paintComponent(g)" but still it is drawn when the program is running.

什么是Graphics参数?这个从哪里来?调用方法时必须提供参数。但正如我之前所说,似乎永远不会明确地调用此方法。那么谁提供这个参数呢?为什么我们必须将其转换为Graphics2D?

And what is the Graphics parameter? Where is it from? Parameter must be supplied when the method is called. But as I said before, it seems like this method is never be explicitly called. So who provides this parameter? And why do we have to cast it to Graphics2D?

public void paintComponent(Graphics g)
{
    ...
    Graphics2D g2= (Graphics2D) g;
    ...
}


推荐答案

(非常)简短的回答你的问题是 paintComponent 被称为当它需要时。有时将Java Swing GUI系统视为黑盒子更容易,其中大部分内部的处理都没有太多的可见性。

The (very) short answer to your question is that paintComponent is called "when it needs to be." Sometimes it's easier to think of the Java Swing GUI system as a "black-box," where much of the internals are handled without too much visibility.

有一个数字确定何时需要重新绘制组件的因素,包括移动,重新调整大小,更改焦点,被其他框架隐藏等等。其中许多事件都是自动检测到的,并且当确定需要执行该操作时,会在内部调用 paintComponent

There are a number of factors that determine when a component needs to be re-painted, ranging from moving, re-sizing, changing focus, being hidden by other frames, and so on and so forth. Many of these events are detected auto-magically, and paintComponent is called internally when it is determined that that operation is necessary.

我和Swing合作多年了,我认为我曾经称为 paintComponent 直接,或者甚至看到它直接从其他东西调用。我最接近的是使用 repaint()方法以编程方式触发某些组件的重绘(我假设调用正确的 paintComponent 下游方法。

I've worked with Swing for many years, and I don't think I've ever called paintComponent directly, or even seen it called directly from something else. The closest I've come is using the repaint() methods to programmatically trigger a repaint of certain components (which I assume calls the correct paintComponent methods downstream.

根据我的经验,很少直接覆盖 paintComponent 。我承认那里是需要这种粒度的自定义渲染任务,但Java Swing确实提供了一套(相当)强大的JComponents和Layouts集合,可用于完成繁重的工作,而无需直接覆盖 paintComponent 。我想我的观点是确保在您尝试滚动自己的自定义渲染组件之前,您无法对本机JComponents和布局执行某些操作。

In my experience, paintComponent is rarely directly overridden. I admit that there are custom rendering tasks that require such granularity, but Java Swing does offer a (fairly) robust set of JComponents and Layouts that can be used to do much of the heavy lifting without having to directly override paintComponent. I guess my point here is to make sure that you can't do something with native JComponents and Layouts before you go off trying to roll your own custom-rendered components.

这篇关于paintComponent如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆