我正在使用Graphics在JPanel上绘制椭圆。最低的椭圆形出现了一些问题,它不是保持一种颜色,而是在椭圆形上有多种颜色的条纹。其他椭圆不会发生这种情况。
我的JFrame尺寸是600乘600。这是我的代码:
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);
}
}
我写了一个小的测试程序,在JFrame中绘制面板,对我来说一切都很好(我没有条纹)。我使用的是Java 7。也许您的错误在程序的其他部分:)。
这个程序也有同样的错误吗?
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);
}
}