* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uts_105314069;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.Random;
import javax.swing.JPanel;
/**
*
* @author admin
*/
public class Soal_1 extends JPanel {
// draw general paths
@Override
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // call superclass's paintComponent
Random random = new Random(); // get random number generator
int xPoints[] = { 0,60,60,0 };
int yPoints[] = { 0,0,60,60};
Graphics2D g2d = ( Graphics2D ) g;
GeneralPath star = new GeneralPath(); // create GeneralPath object
star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
// create the star--this does not draw the star
for ( int count = 1; count < xPoints.length; count++ )
star.lineTo( xPoints[ count ], yPoints[ count ] );
star.closePath(); // close the shape
g2d.translate( 200, 200 ); // translate the origin to (200, 200)
// rotate around origin and draw stars in random colors
for ( int count = 1; count <= 18; count++ )
{
g2d.rotate( Math.PI / 4.0 ); // rotate coordinate system
// set random drawing color
g2d.setColor( new Color( random.nextInt( 256 ),
random.nextInt( 256 ), random.nextInt( 256 ) ) );
g2d.fill( star ); // draw filled star
} // end for
} // end method paintComponent
} // end class Shapes2JPanel
kelas Main
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uts_105314069;
import java.awt.Color;
import javax.swing.JFrame;
/**
*
* @author admin
*/
public class Tes_Soal_1 extends Thread {
// execute application
private static Soal_1 shapes2JPanel;
public static void main(String args[]) {
// create frame for Shapes2JPanel
JFrame frame = new JFrame("Drawing 2D");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shapes2JPanel = new Soal_1();
frame.add(shapes2JPanel); // add shapes2JPanel to frame
frame.setBackground(Color.WHITE); // set frame background color
frame.setSize(400, 400); // set frame size
frame.setVisible(true); // display frame
Tes_Soal_1 sh = new Tes_Soal_1();
sh.start();
} // end main
@Override
public void run() {
while (true) {
try {
Tes_Soal_1.sleep(500);
} catch (InterruptedException ie) {
break;
}
shapes2JPanel.repaint();
}
}
} // end class Shapes2
Tidak ada komentar:
Posting Komentar