datab
Apr 25th, 2002, 09:44 PM
I need to display randomly generated triangles in different colors. Each triangle should be filled with different color. use class GeneralPath and fill of class Graphics2D to draw triangles.
Here is the code I have written. Its not working correctly.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class Triangles extends JFrame
{
public Triangles()
{
super( "Drawing 2d shapes" );
getContentPane().setBackground( Color.black );
setSize( 400, 400 );
setVisible( true );
}
public void paint( Graphics g )
{
super.paint( g );
int xP, yP, zP;
Graphics2D g2d = ( Graphics2D ) g;
GeneralPath triangle = new GeneralPath();
for( int count = 1; count <= 5; count++ )
{
xP = ( int ) ( Math.random() * 256 );
yP = ( int ) ( Math.random() * 256 );
zP = ( int ) ( Math.random() * 256 );
triangle.moveTo( xP, yP );
triangle.lineTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 150 ) );
triangle.lineTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 150 ) );
triangle.closePath();
g2d.setColor( new Color( xP, yP, zP ) );
g2d.fill( triangle );
}
}
public static void main( String args[] )
{
Triangles application = new Triangles();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
Thank you
Here is the code I have written. Its not working correctly.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class Triangles extends JFrame
{
public Triangles()
{
super( "Drawing 2d shapes" );
getContentPane().setBackground( Color.black );
setSize( 400, 400 );
setVisible( true );
}
public void paint( Graphics g )
{
super.paint( g );
int xP, yP, zP;
Graphics2D g2d = ( Graphics2D ) g;
GeneralPath triangle = new GeneralPath();
for( int count = 1; count <= 5; count++ )
{
xP = ( int ) ( Math.random() * 256 );
yP = ( int ) ( Math.random() * 256 );
zP = ( int ) ( Math.random() * 256 );
triangle.moveTo( xP, yP );
triangle.lineTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 150 ) );
triangle.lineTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 150 ) );
triangle.closePath();
g2d.setColor( new Color( xP, yP, zP ) );
g2d.fill( triangle );
}
}
public static void main( String args[] )
{
Triangles application = new Triangles();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
Thank you