Search code examples
javaswingnetbeansgraphicspaintcomponent

How to override fillOval function in Java?


The issue I am having is that I want to fill an oval, but I am using floats as my 'x' and 'y' values. Here is what I have:

@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

I am using NetBeans IDE 7.2 and I don't know how to override the fillOval method, and I cannot use an import from : import org.newdawn.slick.*; (It wouldn't recognize it). Is there a better Graphics option or easier way to achieve this simple fillOval?


Solution

  • You may be looking for the fill() method of Graphics2D, which accepts any Shape:

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

    You'll also want to explore any supported java.awt.RenderingHints.