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

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

问题描述

这可能是一个非常菜鸟的问题.我刚开始学习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代码>.我想我的意思是在尝试推出自己的自定义呈现组件之前,确保您无法使用本机 JComponent 和布局执行某些操作.

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天全站免登陆