[英]java - why does graphics.drawString() not redraw?
this is beyond me (be forgiving, it's late). 这超出了我(原谅,已经晚了)。 I'm subclassing a WindowsSliderUI, because I want it to draw a bigger thumb (is that the right word?)(that's working) and also display the value of the slider just above it (like, for example, gtk look and feel does)(that's broken).
我正在为WindowsSliderUI子类化,因为我希望它绘制一个更大的拇指(这是正确的单词吗?)(正在运行),并且还在其上方显示滑块的值(例如gtk外观确实)(坏了)。 I'm overriding the paint() method, and at the moment it looks like this: (it's long, my changes are near the bottom):
我覆盖了paint()方法,此刻看起来像这样:(很长,我的更改接近底部):
Rectangle knobBounds = thumbRect;
int w = knobBounds.width;
int h = knobBounds.height;
g.translate(knobBounds.x, knobBounds.y);
if ( slider.isEnabled() ) {
g.setColor(slider.getBackground());
}
else {
g.setColor(slider.getBackground().darker());
}
Boolean paintThumbArrowShape =
(Boolean)slider.getClientProperty("Slider.paintThumbArrowShape");
if ((!slider.getPaintTicks() && paintThumbArrowShape == null) ||
paintThumbArrowShape == Boolean.FALSE) {
// "plain" version
g.fillRect(0, 0, w, h);
//THE ONES THAT MATTER
g.setColor(Color.BLACK);
String val = slider.getValue()+"";
g.drawString(val, 0, 0);
g.setColor(Color.RED);
g.drawLine(0, 0, 30, 8);
//END
g.setColor(Color.black);
g.drawLine(0, h-1, w-1, h-1);
g.drawLine(w-1, 0, w-1, h-1);
g.setColor(getHighlightColor());
g.drawLine(0, 0, 0, h-2);
g.drawLine(1, 0, w-2, 0);
g.setColor(getShadowColor());
g.drawLine(1, h-2, w-2, h-2);
g.drawLine(w-2, 1, w-2, h-3);
}
all i want is to get the value displayed just above the thumb. 我想要的就是让值显示在拇指上方。 however, what happens now is the string gets displayed initially, but when i drag the slider, it stays in the same place (not changing value) until i release the mouse button, at which point it is redrawn at the right place with the right value.
但是,现在发生的是字符串最初显示出来,但是当我拖动滑块时,它会停留在相同的位置(不改变值),直到我释放鼠标按钮为止,这时它会在右边用正确的位置重新绘制值。 and just to make my head go even funnier, the drawLine() method works fine - the line is always on the thumb when i drag it.
并且只是让我的头变得更有趣,drawLine()方法工作得很好-当我拖动它时,该线始终在拇指上。 now, this is probably trivial mistake (i'm really no good, and tired), but please help me find it.
现在,这可能是微不足道的错误(我真的不好,很累),但是请帮助我找到它。 mind you, if you see a better approach to the whole problem, let me know as well.
请注意,如果您发现解决整个问题的更好方法,也请告诉我。 as i said, i'm really no good with this, and i tend to make things more complicated than they are.
就像我说的那样,我真的做不到,而且我倾向于使事情变得比他们复杂。
thanks a lot 非常感谢
I don't know the complete answer, but your call to 我不知道完整的答案,但您致电给
g.drawString(val, 0, 0);
looks dodgy. 看起来很狡猾。 The co-ordinates you specify to drawString are the baseline of the leftmost character in the string, not the top-left hand corner of the string.
您为drawString指定的坐标是字符串中最左边的字符的基线,而不是字符串的左上角。 Typically a call like yours results in nothing appearing, because all or most of the string is drawn outside of the clipping rectangle.
通常,像您这样的调用不会显示任何内容,因为所有或大部分字符串都在剪切矩形的外部绘制。
Perhaps you meant something like this: 也许您的意思是这样的:
final int height = g.getFontMetrics().getAscent();
g.drawString(s, 0, height);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.