Re: Why is it running twice?
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.Random;
import javax.swing.JFrame;
public class Test extends JFrame
{
private static final long serialVersionUID = 3864411708478015289L;
int i, z;
int x[] = new int[3];
int y[] = new int[3];
Random rand = new Random();
public Test()
{
super("Drawing Triangles");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
Graphics2D g2d = (Graphics2D) g;
for (z = 0; z < 10; z++)
{ //needs to have 10 triangles
GeneralPath triangle = new GeneralPath();
for (i = 0; i < 3; i++)
{
x[i] = rand.nextInt(600);
y[i] = rand.nextInt(600);
}
triangle.moveTo(x[ 0], y[ 0]);
for (int count = 1; count < x.length; count++)
triangle.lineTo(x[count], y[count]);
triangle.closePath();
g2d.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
g2d.fill(triangle);
try
{
// sleep for 50 ms.
Thread.sleep(50);
} catch (Exception e)
{
}
}
}
public static void main(String[] args)
{
new Test();
}
}
The "getContentPane().setBackground(Color.BLACK);" Causes the content pane to refresh with a black background after the painting of the JFrame is done.