【发布时间】:2012-10-24 00:26:44
【问题描述】:
我遇到的问题是我想填充一个椭圆,但我使用浮点数作为我的“x”和“y”值。这是我所拥有的:
@Override
public void paintComponent(Graphics g)
{
.
.
.
for(Coordinates cord : tempVertices)
{
g.fillOval(cord.x,cord.y,DIAMETER,DIAMETER);
}
// DIAMETER = 10
// Coordinate Class is just a variable with an 'x' and 'y' value stored in floats
// Floats range from 0 to 1, so type casting won't work...
// tempVertices is just an ArrayList with Coordinates values
我正在使用 NetBeans IDE 7.2,但我不知道如何覆盖 fillOval 方法,并且我无法使用 import from : import org.newdawn.slick.*;(它不会识别它)。有没有更好的Graphics 选项或更简单的方法来实现这个简单的fillOval?
【问题讨论】:
-
g是什么?看起来你想覆盖“别人的”代码? -
这是在重载的paintComponent(Graphics g) 函数中...
-
你能把你的浮点数转换成整数吗?
g.fillOval((int)(coord.x), (int)(coord.y)... -
不,它们的范围是从 0 到 1,所以它们都会被截断为 0,除非它们当然是 1。
-
Graphics处理离散画布上的像素。除非您出于某种原因尝试将椭圆椭圆定位在分数像素处,否则您必须将 0..1 范围显式转换为绝对坐标。不幸的是,你还没有真正解释你最终想要画什么。
标签: java swing netbeans graphics paintcomponent