Ethan Brouwer
Ethan Brouwer

Reputation: 1005

Why does the Java Ellipse2D when drawn small, turn out as a rectangle?

I think I'll just start with the code that's giving me the problem.

class AnimationPanel extends JPanel
{   
    OfficeLoad load;
    Timer timer = new Timer();

    private static final long serialVersionUID = 1L;
    public AnimationPanel()
    {
        setBackground(new Color(240, 240, 240));
        setBorder(null);
        setOpaque(false);
        setBounds(10, 143, 400, 21);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        load  = new OfficeLoad(g);
    }
    ...
    }

...

final Color WHITE = Color.WHITE;
public OfficeLoad(Graphics g)
{
    Graphics2D g2 = (Graphics2D)g.create();

    g2.setPaint(WHITE);
    g2.fill(new Ellipse2D.Double(30, 1, 5, 5));
    g.fillOval(40, 1, 5, 5);
    g.fillOval(50, 1, 5, 5);
    g.fillOval(60, 1, 5, 5);
    g.fillOval(70, 1, 5, 5);
    g.setColor(new Color(0, 102, 51));
    g.fillRect(0, 0, 10, 21);
}

Both when I use g.fillOval() and when I use g2.fill(new Ellipse2D()) it turns out as a square. Just for some extra information, I'm, just for fun of course, trying to replicate the excel 2013 splash screen when it's starting up. This part is for the loading dots underneath the "Excel". I already made it with a gif, which was much easier, but my friend challenged me to use the paint, repaint, and so forth. But I can't really do that, unless they turn up as circles rather than squares... Any help would be appreciated. :)

Upvotes: 2

Views: 296

Answers (2)

Daan Luttik
Daan Luttik

Reputation: 2855

First of all I advice to not make a new OfficeLoad object everytime you paint you can either save the officeload object or make a static void in the OfficeLoad so you don't need the object at all.

also you will need antialiassing

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

Upvotes: 1

Ethan Brouwer
Ethan Brouwer

Reputation: 1005

Thank you Halex! :D For everyone that didn't look at his comment, Halex gave this following answer.

Does it work when you enable antialiasing with g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

It did. Thanks! :D

Upvotes: 2

Related Questions