101
Sh
shapesaver / files
shapesaver / DrawPanel.java
2.9 KB
1/* 2 sean caetano martin 3 seancaetanomartin@gmail.com 4 CIS 255 HY 5 DrawPanel.java 6 assignment#5 7 3/28/11 8*/ 9 10import java.awt.*; 11import java.util.Random; 12import javax.swing.JPanel; 13import java.awt.event.*; 14 15public class DrawPanel extends JPanel implements MouseMotionListener 16{ 17 //prtivate varibales and random object creation 18 private Random randomNumbers = new Random(); 19 private int shapeCounter = 0; 20 private boolean firstTime = true; 21 22 public DrawPanel() 23 { 24 setBackground (Color.BLACK); 25 }// end contructor 26 27 public void mouseDragged (MouseEvent e){}// end method 28 29 public void mouseMoved (MouseEvent e) 30 { 31 if (firstTime == true) 32 { 33 firstTime = false; 34 } 35 else 36 { 37 System.exit(0); 38 } 39 }// end method 40 41 public void paintComponent (Graphics g) 42 { 43 // 100 shape reset function 44 if (shapeCounter == 100) 45 { 46 super.paintComponent (g); 47 shapeCounter = 0; 48 } 49 50 // random color generation 51 Color color1 = new Color(randomNumbers.nextInt(256), 52 randomNumbers.nextInt(256),randomNumbers.nextInt(256)); 53 Color color2 = new Color(randomNumbers.nextInt(256), 54 randomNumbers.nextInt(256),randomNumbers.nextInt(256)); 55 56 // boolean for gradient value 57 boolean cyclic = randomNumbers.nextBoolean(); 58 // filled 59 boolean filled = randomNumbers.nextBoolean(); 60 61 // position generation for x and y variables 62 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 63 int width = screenSize.width; 64 int height = screenSize.height; 65 66 int sizeWidth = randomNumbers.nextInt(width); 67 int sizeHeight = randomNumbers.nextInt(height); 68 69 int x1,y1,x2,y2,x3,y3; 70 x1 = randomNumbers.nextInt(width-sizeWidth); 71 y1 = randomNumbers.nextInt(height-sizeHeight); 72 x2 = x1+sizeWidth; 73 y2 = y1+sizeHeight; 74 x3 = x1; 75 y3 = y2; 76 77 // boundary control function 78 if (x2 >= width) 79 { 80 x1 = width - sizeWidth; 81 x2 = width; 82 }// end if X control 83 if (y2 >= height) 84 { 85 y1 = height - sizeHeight; 86 y2 = height; 87 } // end if y control 88 89 Shape shape; 90 //object creation 91 switch (randomNumbers.nextInt(4)) 92 { 93 case 1: 94 shape = new MyOval (x1,y1,x2,y2, color1, filled); 95 break; 96 case 2: 97 shape = new MyRectangle (x1,y1,x2,y2, color1, filled); 98 break; 99 case 3: 100 shape = new MyTriangle (x1,y1,x2,y2,x3,y3, color1); 101 break; 102 default: 103 shape = new MyLine (x1, x2, y1, y2, color1); 104 break; 105 }// end switch 106 107 // polymorphic draw 108 Graphics2D g2 = (Graphics2D )g.create(); 109 Paint p = new GradientPaint(0, 0, color1, width/50, height/70, color2, cyclic); 110 g2.setPaint(p); 111 shape.draw(g2); 112 g2.dispose(); 113 shapeCounter++; 114 115 try 116 { 117 Thread.sleep(700); 118 } 119 catch(Exception e) {} 120 121 repaint(); 122 }//end method 123}// end class