Double drawing problem with 2D
Hi, my code works fine all I wanted to ask is my codes prints double of same triangles like it should draw 10 random triangles and it draws 10 random triangles then again it wipes of the screen and draw again 10 random triangles so all I want is 10 not 20 so any inputs will be really helpful thanks.
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class Triangles2 extends JFrame
{
public Triangles2()
{
super( "Random Triangles" );
getContentPane().setBackground( Color.black );
setSize( 400, 600 );
setVisible( true );
}
public void paint( Graphics g )
{
super.paint( g );
int xP, yP, zP;
Graphics2D g2d = ( Graphics2D ) g;
for( int count = 1; count <= 10; count++ )
{
GeneralPath triangle = new GeneralPath();
xP = ( int ) ( Math.random() * 256 );
yP = ( int ) ( Math.random() * 256 );
zP = ( int ) ( Math.random() * 256 );
try
{
Thread.sleep(700);
}
catch (Exception e) {}
triangle.moveTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 400 ) );
triangle.lineTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 400 ) );
triangle.lineTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 400 ) );
triangle.closePath();
g2d.setColor( new Color( xP, yP, zP ) );
g2d.fill( triangle );
}
}
public static void main( String args[] )
{
Triangles2 myTriangles = new Triangles2();
myTriangles.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}//class