import java.awt.Color; import java.awt.Graphics; import java.util.Random; /* * DrawPanel.java * * Created on April 22, 2007, 1:10 PM */ /** * * @author DotH */ public class DrawPanel extends javax.swing.JPanel { /** Creates new form DrawPanel */ public DrawPanel(String initialShape) { currentShape=initialShape; initComponents(); } public String currentShape; public int shapeWidth=50; public int shapeHeight=50; /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // //GEN-BEGIN:initComponents private void initComponents() { setLayout(null); }// //GEN-END:initComponents public void paint(Graphics g) { int randomWidthConstraint = getSize().width-shapeWidth; //detemine width limit of panel area int randomHeightConstraint = getSize().height-shapeHeight; //detemine height limit of panel area Random rn= new Random(); this.setBackground( Color.WHITE ); g.clearRect(0,0,getSize().width,getSize().height); //clear panel before painting for (int i=0;i<19;i++){ //draw shape 20 times int shapeX=rn.nextInt(randomWidthConstraint); //Determine next random x position int shapeY=rn.nextInt(randomHeightConstraint);//Determine next random y position if (currentShape=="Square") { //draw Squares g.setColor( Color.BLUE ); g.fillRect ( shapeX,shapeY , shapeWidth, shapeHeight); g.setColor( Color.BLACK ); g.drawRect( shapeX,shapeY , shapeWidth, shapeHeight); } if (currentShape=="Rectangle") {//draw Rectanlges g.setColor( Color.CYAN ); g.fillRect ( shapeX,shapeY , shapeWidth, shapeHeight-15); g.setColor( Color.BLACK ); g.drawRect( shapeX,shapeY , shapeWidth, shapeHeight-15); } if (currentShape=="Circle") {//draw Circles g.setColor( Color.RED ); g.fillOval ( shapeX,shapeY , shapeWidth, shapeHeight); g.setColor( Color.BLACK ); g.drawOval( shapeX,shapeY , shapeWidth, shapeHeight); } if (currentShape=="Oval") {//draw Ovals g.setColor( Color.ORANGE ); g.fillOval ( shapeX,shapeY , shapeWidth, shapeHeight-15); g.setColor( Color.BLACK ); g.drawOval( shapeX,shapeY , shapeWidth, shapeHeight-15); } if (currentShape=="Round Rectangle") {//draw Round Rectangles g.setColor( Color.GREEN ); g.fillRoundRect ( shapeX,shapeY , shapeWidth-10, shapeHeight,10,10); g.setColor( Color.BLACK ); g.drawRoundRect( shapeX,shapeY , shapeWidth-10, shapeHeight,10,10); } if (currentShape=="Star") {//draw Stars g.setColor( Color.YELLOW ); //Definition of a Star by points int[] xPoints={8+shapeX,23+shapeX,35+shapeX,30+shapeX,49+shapeX,31+shapeX,35+shapeX,25+shapeX,3+shapeX,16+shapeX}; int[] yPoints={3+shapeY,17+shapeY,1+shapeY,19+shapeY,19+shapeY,26+shapeY,45+shapeY,32+shapeY,42+shapeY,26+shapeY}; //end star definitions g.fillPolygon ( xPoints,yPoints,10); g.setColor( Color.BLACK ); g.drawPolygon( xPoints,yPoints,10); } } } // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables }