Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Assignments

DrawShapes


DrawShapes.java

/*
 *DrawShapes.Java
 *Written by Mike Kok
 *Designed by JDE Class of 2006
 *
 *Version 1.0
 *
 *
 *
 */

/*
 *Design Changes:
 *Changed multiplier from 1.1 to 1.5.  Integers weren't increasing under 10.
 *  If a larger coordinate system were used (I was running a box 350x200), 
 *  it then would be possible to bring the multiplier down to 1.1, as long as 
 *  widths, radii, and bases are greater than 10. 
 *Changed triangle algorithm.  Now centers on center of circumscribed circle.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.JApplet;

/* Class Draw Shapes
 *     (Insert short description here)
 *
 */
public class DrawShapes extends JApplet {
    
    // Class attributes
    String b; //base of triangle and rectangle
    String h; //height of triangle and rectangle and square
    String x; //center coordinate - x
    String y; //center coordinate - y
    String s; //choice
    String r; //radius of circle
    String q; //Quantity of objects
 
    //--------------------------------------------------------------------------
    //init() gets input from the user.
    //  (insert slightly more detail)
    //
    public void init() {
      
        do {
            s=JOptionPane.showInputDialog(null,"Please Choose a Shape\n" + 
                                                "1. Circle\n" +
                                                "2. Square\n" + 
                                                "3. Triangle\n" +
                                                "4. Rectangle\n");
        } while( !s.equals("1") && !s.equals("2") && !s.equals("3") 
                 && !s.equals("4") ); 
        //Chose a shape, make sure they enter valid shape
        
        //Input variables according to shape selected
        if(s.equals("1")) {
            r=JOptionPane.showInputDialog(null,"Please enter a radius: ");
        
        }
        else if (s.equals("2")) {
            h=JOptionPane.showInputDialog(null,"Please enter length of side: ");
           
        }
        else if(s.equals("3")) {
            b=JOptionPane.showInputDialog(null,"Please enter Base: ");
            h=JOptionPane.showInputDialog(null,"Please enter Height: ");
        }
        else if(s.equals("4")) {
            b=JOptionPane.showInputDialog(null,"Please enter Width: ");
            h=JOptionPane.showInputDialog(null,"Plesae enter Height: ");
        }

        //input center point
        x=JOptionPane.showInputDialog(null,"Input center (x value): ");
        y=JOptionPane.showInputDialog(null,"Input center (y value): ");
        q=JOptionPane.showInputDialog(null,"Input number of shapes to draw: ");
    }//end of init
        

    //--------------------------------------------------------------------------
    // paint(Graphics g) 
    //  (insert slightly more detail)
    //
    public void paint(Graphics g) {
      super.paint(g);
      int i; //variable to control for is
      int rad, base, height, qty, xcent, ycent;
     
      //parse variables used in all shapes
      qty=Integer.parseInt(q);
      xcent=Integer.parseInt(x);
      ycent=Integer.parseInt(y);
     
      //select shape to draw
      if(s.equals("1")) {
         rad=Integer.parseInt(r);
         
         for(i=1;i<=qty;i++) {
            g.drawOval(xcent-rad,ycent-rad,rad*2,rad*2);
            rad*=1.25; 
         }
      }
      else if (s.equals("2")){
         height=Integer.parseInt(h);
         
         for(i=1;i<=qty;i++) {
            g.drawRect(xcent-height/2,ycent-height/2,height,height);
            height*=1.5;
         }     
      }
      else if(s.equals("3")){
         //double variables to increase accuracy of centering equation
         double x1,x2,x3,y1,y2,y3,db,dh,dx,dy; 
         
         db=Double.parseDouble(b);  //double of base
         dh=Double.parseDouble(h);  //double of height
         dx=Double.parseDouble(x);  //double of xcenter
         dy=Double.parseDouble(y);  //double of ycenter
         
         for(i=1;i<=qty;i++) {
            x3=dx;                                     //top point x = center x
            x1=dx-db/2;                                //left point x
            x2=dx+db/2;                                //right point x
            y1=db/(2*-dh)*(dx-(2*dx-db/2)/2)+dy+dh/2;  //y centering equation
            y2=y1;                                     //bottom y's equal to each other
            y3=y1-dh;                                  //top y is equal to bottom + height
            g.drawLine((int)x1,(int)y1,(int)x2,(int)y2); 
            g.drawLine((int)x1,(int)y1,(int)x3,(int)y3);  //drawline takes integers
            g.drawLine((int)x3,(int)y3,(int)x2,(int)y2);
            db*=1.5;
            dh*=1.5;
         } 
      }
      else if(s.equals("4")){
        base=Integer.parseInt(b);
        height=Integer.parseInt(h);
        
        for(i=1;i<=qty;i++) {
            g.drawRect(xcent-base/2,ycent-height/2,base,height);
            base*=1.5;
            height*=1.5;
        }
      }   
    }
    //--------------------------------------------------------------------------
}


/*
 *Explanation of draw equations
 *I figured there might be a little confusion to why I have things
 *like -base/2 and such in the drawing equations.  The system java uses
 *to draw things is by selecting the upper left corner.  To properly
 *center on the variables input by the user, it is necessary to do
 *this to place the upper left corner the correct distance from the center.
 */