【问题标题】:How to override fillOval function in Java? [closed]如何在 Java 中覆盖 fillOval 函数? [关闭]
【发布时间】: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


【解决方案1】:

您可能正在寻找Graphics2Dfill() 方法,它接受任何Shape

g.fill(new Ellipse2D.Float(cord.x, cord.y, DIAMETER, DIAMETER));

您还需要探索任何受支持的java.awt.RenderingHints

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    相关资源
    最近更新 更多