// TUMBLING STAR
// Frans Coenen
// Friday 8 may 2003
// The University of Liverpool, UK
/* Based on example given by Deitel and Deital */
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
// Java extension packages
import javax.swing.*;
public class TumblingStar extends JFrame {
// ------------------- FIELDS ------------------------
/* None */
// ------------------ CONSTRUCTORS -------------------
public TumblingStar() {
super("TumblingStar");
getContentPane().setBackground(Color.yellow);
setSize(400,400);
setVisible(true);
}
// ------------------ METHODS ------------------------
/* PAINT */
/** Draws general paths. */
public void paint(Graphics g) {
super.paint(g);
int xPoints[] = {55, 67, 109, 73, 83, 55, 27, 37, 1, 43};
int yPoints[] = {0, 36, 36, 54, 96, 72, 96, 54, 36, 36};
// Create 2D by casting g to graphics2D
Graphics2D g2d=(Graphics2D) g;
// Create a star from a series of points
GeneralPath star = new GeneralPath();
// Set the initiakl coordinates of the general Path
star.moveTo(xPoints[0],yPoints[0]);
// Create the star. Note: this does not draw the star.
for(int index=1;index < xPoints.length;index++)
star.lineTo(xPoints[index],yPoints[index]);
// Close the shape
star.closePath();
// translate the origin (200,200)
g2d.translate(200,200);
// rotate around origin and draw stars in random colours
for (int index=1;index<=20;index++) {
// Rotate coordinate system
g2d.rotate(Math.PI/10.0);
// Set random drawing color
g2d.setColor(new Color((int) (Math.random()*256), (int) (Math.random()*256),
(int) (Math.random()*256)));
// Draw dilled star
rest();
g2d.fill(star);
}
}
private void rest() {
for (double index=1.0;index<=20000.0;index++) System.out.print(".");
}
/* MAIN METHOD */
public static void main(String args[]) {
TumblingStar application = new TumblingStar();
application.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
}
|