The Artima Developer Community
Sponsored Link

Java Answers Forum
random triangles and colors

1 reply on 1 page. Most recent reply: Sep 8, 2005 10:22 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Scotty Middleton

Posts: 1
Nickname: mylife
Registered: Sep, 2005

random triangles and colors Posted: Sep 8, 2005 10:27 AM
Reply to this message Reply
Advertisement
I am having problems getting 10 triangles to display. Any suggestions on what to do? Here is the code

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class Hwk1 extends JFrame {

// set window's title bar String, background color and dimensions
public Hwk1()
{
super( "Drawing Triangles" );

getContentPane().setBackground( Color.black );
setSize( 400, 400 );
setVisible( true );
}

// draw general paths
public void paint( Graphics g )
{
super.paint( g ); // call superclass's paint method


int i;
int x[] = new int[3];
int y[] = new int[3];


for (i=0; i<3; i++)
{

x = 1 + ( int ) (Math.random() * 256 );
y = 1 + ( int ) (Math.random() * 256 );



Graphics2D g2d = ( Graphics2D ) g;
GeneralPath triangle = new GeneralPath(); // create GeneralPath object

// set the initial coordinate of the General Path
triangle.moveTo( x[ 0 ], y[ 0 ] );

// create the triangle--this does not draw the triangle
for ( int count = 1; count < x.length; count++ )
triangle.lineTo( x[ count ], y[ count ] );

triangle.closePath(); // close the shape

// set random drawing color
g2d.setColor( new Color( ( int ) ( Math.random() * 256 ),
( int ) ( Math.random() * 256 ),
( int ) ( Math.random() * 256 ) ) );

g2d.translate( 25, 25 ); // set start area

g2d.fill( triangle ); // draw filled triangle

}


} // end method paint


public static void main( String args[] )
{
Hwk1 application = new Hwk1();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class Hwk1


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: random triangles and colors Posted: Sep 8, 2005 10:22 PM
Reply to this message Reply
You never draw your GeneralPath object.

g2d.draw(triangle);

Flat View: This topic has 1 reply on 1 page
Topic: convert numbers to words Previous Topic   Next Topic Topic: Need Some Coding Help w/ program!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use