我正在创建一个滑块,它将是 JSlider 的子类,作为日志记录 GUI 的一部分。我想做的是能够在滑块上方绘制一些项目(例如“音量”显示,指示特定设置将吐出多少日志信息)。我已经重写了paintComponent()方法,但是我对如何为传递给paintComponent的Graphics对象计算剪切矩形感到有点困惑。
基本上,我想要做的是在可以绘制的组件的可见区域内创建更多空间,但仅限于已绘制的空间之上。因此,例如,在下图中,我有三个滑块:
每个滑块的剪切矩形均显示为红色这些。 (这些有点手绘,但我实际上使用了drawRect(g.getClipBounds())并想出了类似的矩形。如果我根本不增加滑块,我就会得到顶部滑块。如果我重写 getPreferredSize() 以向组件的高度添加一些量(我称之为“体积高度” - 默认情况下为 20),我会得到中间的矩形。 基本上,它就是我想要的。中间的滑块,但剪切矩形向组件顶部平移了体积高度的 1/2
所以,我的问题是“当传递给 PaintComponent() 方法时,如何在 Graphics 对象中计算剪切矩形?” ' 我似乎无法找出系统如何确定给定组件的剪切范围,
谢谢,
〜斯科特。
I'm creating a slider bar, which will be a subclass of JSlider, as part of a logging gui. What I'd like to do is be able to draw some items above the slider bar (e.g. a "volume" display, indicating how much logging info will be spit out by a particular setting). I have overridden the paintComponent() method, but I'm getting a bit confused about how the clipping rectangle is calculated for the Graphics object that is passed in to paintComponent.
Basically, what I want to do is create more space within the visible area of the component that can be drawn, but only ABOVE what is already drawn. So, for example, in the following image I have three slider bars:
The clipping rectangle is shown in red for each of these. (These are somewhat hand-drawn, but I did actually use the drawRect(g.getClipBounds()) and came up with similar rectangles. The top slider bar is what I get if I don't augment the slider bar at all. The middle rectangle is what I get if I override getPreferredSize() to add some amount (I call it the 'volume height' - by default, 20) to the height of the component. The bottom image is the one I want. Basically, it's the middle slider bar, but with the clipping rectangle translated by 1/2 the volume height toward the top of the component.
So, my question is 'How are clipping rectangles computed within the Graphics object when passed in to a paintComponent() method?' I can't seem to find out how the system determines what the clipping bounds should be for a given component.
Thanks,
~Scott
发布评论
评论(1)
java.awt.Graphics
API提到,“用户剪辑和设备剪辑的组合定义了复合剪辑,它决定了最终的剪辑区域”,但剪辑与您的问题并不真正相关。您可能想使用合适的布局。 Box 布局,它启用 使用不可见组件作为填充,可能有用这个背景。附录:
AFAIK,
paintComponent()
只是委托给javax.swing.plaf.SliderUI
。这个示例可能会建议一种方法。The
java.awt.Graphics
API mentions, "The combination of the user clip and device clip defines the composite clip, which determines the final clipping region," but clipping is not really relevant to your question. You probably want to use a suitable layout, instead. Box layout, which enables Using Invisible Components as Filler, may be useful in this context.Addendum:
AFAIK,
paintComponent()
just delegates to a subclass ofjavax.swing.plaf.SliderUI
. This example may suggest an approach.