imulsion
imulsion

Reputation: 9040

Something strange happening with JPanel and java g.fillOval() method

I am using Graphics to draw ovals on a JPanel. Something is happening with the lowest oval, which, instead of staying one colour, is multiple colours in stripes across the oval. This doesn't happen with the other ovals.

My JFrame size is 600 by 600. Here is my code:

package virus;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.util.Random;

public class VirusGamePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    Random colour = new Random();
    int score = 0;

    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.magenta);
        g.drawString("Score: "+ score,200,200);
        g.setColor(Color.orange);
        g.drawOval(200,150,200,200);
        Color rand = new Color(colour.nextInt(255),colour.nextInt(255),colour.nextInt(255));
        g.setColor(rand);
        g.fillOval(270,50,50,50);
        g.fillOval(100,100,50,50);
        g.fillOval(450,100,50,50);//this line is causing the problem
        g.fillOval(100,400,50,50);
    }
}

Upvotes: 0

Views: 1558

Answers (1)

halex
halex

Reputation: 16403

I wrote a small test program that draws your panel in a JFrame and everything looks fine for me (I have no stripes). I'm using Java 7. Maybe your error is in an other part of your program :).

Do you get the same error with this program?

import javax.swing.*;    
import java.awt.*;
import java.util.Random;

public class VirusGamePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    Random colour = new Random();
    int score = 0;

    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.magenta);
        g.drawString("Score: "+ score,200,200);
        g.setColor(Color.orange);
        g.drawOval(200,150,200,200);
        Color rand = new Color(colour.nextInt(255),colour.nextInt(255),colour.nextInt(255));
        g.setColor(rand);
        g.fillOval(270,50,50,50);
        g.fillOval(100,100,50,50);
        g.fillOval(450,100,50,50);//this line is causing the problem
        g.fillOval(100,400,50,50);
    }

    public static void main(String[] args) {
        JFrame f=new JFrame();
        f.setSize(600,600);
        f.add(new VirusGamePanel());
        f.setVisible(true);

    }
}

Upvotes: 2

Related Questions