什么时候getGraphics()会返回一个有效的图形实例? [英] When will getGraphics() return a valid graphics instance?

查看:115
本文介绍了什么时候getGraphics()会返回一个有效的图形实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试渲染一个自定义Swing组件,我扩展了JComponent类。

I'm trying to render a custom Swing component where I've extended the JComponent class.

为了简化组件要求,我们只想总结一下组件需要渲染一些字符串,每个字符串都有自己的字体。

For the purpose of simplifying the component requirements, lets just summarize my component as needing to render a few strings, each with their own fonts.

我需要将组件的大小精确到我渲染字符串的总宽度和高度。

I need my component to be sized exactly to the summed width and height of my rendered strings.

为了确定这个大小,我使用FontMetrics来计算每个字符串的尺寸。有了这些信息,我可以弄清楚我的组件的大小,并适当调整大小。

In order to determine this size, I use the FontMetrics to calculate the dimensions of each string. Having this information I can figure out what size my component will be and resize it appropriately.

问题是,当我访问getGraphics()时,它总是为null,所以我无法获得FontMetrics实例。如果我等待从覆盖的paintComponent()方法计算我的组件大小,它已经太晚了(组件已经有一个大小,对吧?)。

The problem is that when I access the getGraphics() it is always null, so I can't get the FontMetrics instance. If I wait to calculate my component size from the overriden paintComponent() method, its pretty much too late (the component already has a size, right?).

文档说如果此组件当前不可显示,则此方法将返回null。那么我何时知道组件何时可以显示并且有一个Graphics对象来调整我的组件大小?

The documentation says that "This method will return null if this component is currently not displayable". So when do I know when the component is ready to be displayed and has a Graphics object for me to resize my component?

什么是渲染组件的Swing调用顺序一旦框架setVisible(true)被调用?

What is the Swing invokation order for rendering the component once the frame setVisible(true) is called?

谢谢

更新:2010年2月6日星期二23:34

Update: Tuesday, Feb 06, 2010 at 23:34

如评论中所述,GridLayout不尊重任何完全是setXxxSize()。对于任何有兴趣的人,我发布了使用GridLayout,BoxLayout和FlowLayout的结果,使用一个简单的框架,接收5个固定大小的200宽50高的组件(通过设置min,max和preferred)。

As mentioned bellow in the comments, the GridLayout doesn't respect any setXxxSize() at all. For anyone interested, I've posted results of using the GridLayout, BoxLayout and FlowLayout using a simple frame that receives 5 fixed size components of 200 wide by 50 in height (by setting min, max and preferred).

测试结果:

GridLayout 总是沿宽度和高度调整大小(如评论中所述)

The GridLayout is always resized along the width and height (as mentioned in comments)

FlowLayout 始终尊重组件大小。

至于 BoxLayout ...

PAGE_AXIS Y_AXIS 缩小组件的宽度大约一半大小(104),但没有缩小高度。

The PAGE_AXIS and Y_AXIS shrank the width of the components to about half their size (104) but did not shrink the height.

LINE_AXIS X_AXIS 收缩组件的高度似乎为零,但没有触及宽度。

The LINE_AXIS and X_AXIS shrank the height of the components to what seemed zero but did not touch the width.

推荐答案

首先,您可以使用<测量字符串< a href =http://java.sun.com/j2 se / 1.4.2 / docs / guide / 2d / spec / j2d-fonts.htmlrel =noreferrer> TextLayout 类及其相关内容。例如

First, you can measure your Strings using the TextLayout class and its associated stuff. For example

public static void main(final String[] args) throws Exception
{
    final FontRenderContext frc = new FontRenderContext(null, true, true);
    final Font font = new Font("serif", Font.PLAIN, 18);
    final TextLayout layout = new TextLayout("This is a test", font, frc);
    final Rectangle2D bounds = layout.getBounds();
    System.err.println((int) (bounds.getWidth() + .5));
}

其次,当您的组件变为可见时,可以通过< a href =http://java.sun.com/docs/books/tutorial/uiswing/events/componentlistener.html\"rel =noreferrer> ComponentListener ,但这不是测量字符串所必需的提前!

Secondly, you can be informed when your component has become visible by using a ComponentListener, but that's not necessary for measuring your strings in "advance"!

这篇关于什么时候getGraphics()会返回一个有效的图形实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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