import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class Shapes extends JFrame
{
public double max=2;
public double min=1;
public boolean test;
public double size;
Thread th;
public Shapes()
{
super("Drawing 2d Shapes");
size=min;
mouseHandler mHand = new mouseHandler();
addMouseListener(mHand);
keyHandler kHand=new keyHandler();
addKeyListener(kHand);
getContentPane().setBackground(Color.green);
setSize(400,400);
setVisible(true);
}
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};
Graphics2D g2d=(Graphics2D)g;
GeneralPath star= new GeneralPath();
star.moveTo(xPoints[0]*size, yPoints[0]*size);
for(int count=1;count<xPoints.length;count++){
star.lineTo(xPoints[count]*size, yPoints[count]*size);
}
star.closePath();
g2d.translate(200,200);
for(int count=1; count<=20 ;count++){
g2d.rotate(Math.PI/10.0);
g2d.setColor(new Color(
(int)(Math.random()*256),
(int)(Math.random()*256),
(int)(Math.random()*256)));
g2d.fill(star);
}
}
public class mouseHandler implements MouseListener{
public void mouseReleased(MouseEvent e){
th=new Thread(new RunnableObject());
th.start();
}
public void mouseClicked(MouseEvent e){
th=new Thread(new RunnableObject());
th.start();
}
public void mousePressed(MouseEvent e){
if(test){
test=false;
}else{
test=true;
}
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
public class keyHandler implements KeyListener{
public void keyReleased(KeyEvent e){
if(e.getKeyText(e.getKeyCode()).equalsIgnoreCase(" Up")){
size+=.2;
if(size>max){
size=max;
}
}else if(e.getKeyText(e.getKeyCode()).equalsIgnoreCase(" Down")){
size-=.2;
if(size<min){
size=min;
}
}
repaint();
}
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e){
}
}
public class RunnableObject implements Runnable{
public void run(){
while(test){
try{
th.sleep(100);
}catch(InterruptedException e){
}
repaint();
}
}
}
public static void main(String args[]){
Shapes s=new Shapes();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}