Sunday, February 2, 2014

Coding for birdies (or even albatrosses)! - by Maverick McNealy


As "Nerd Nation" golfers, who hasn't spent hours conjuring up designs for golf holes on Paint or some other computer art program? 

Yes, we've all done it! 

But... how many of us could actually write the artwork's computer code from scratch?


Here below, Maverick McNealy shares with us his coding "artistry" from one of his computer science classes and how he has already coded some great golf shots...


TO JUMP THE CODE AND GO STRAIGHT TO THE PICTURE click here >>



/*
 * File: Artistry.java
 * Name: Maverick McNealy
 * Section Leader: Matt Lathrop
 * ==========================================================
 * This is a pretty awesome picture. It is what I hope happens a lot this year for
 * our team! (Funny thing, because in the week after I programmed this, Keegan English made a hole-in-one on hole 7 at Peninsula G&CC, David Boote made a hole-in--one on hole 1 of the Peter Hay par 3 course at Pebble Beach and Viraat Badhwar made a double eagle on hole 12 at Fort Ord - Black Horse!!!)
 */


import java.awt.Color;

import acm.graphics.GLabel;
import acm.graphics.GOval;
import acm.graphics.GLine;
import acm.graphics.GRect;
import acm.program.*;

public class Artistry extends GraphicsProgram {
     
      //makes the best picture in the history of GraphicsProgram pictures (GO STANFORD GOLF!!)
      public void run() {
            addPuttingGreen();
            addSandTrap1();
            addSandTrap2();
            addLake();
            addHole();
            addPin();
            addBall();
            addSignature();
      }
     
      //Adds a green oval putting green
      private void addPuttingGreen() {
            GOval puttingGreen = new GOval (400, 300);
            puttingGreen.setColor(Color.green);
            puttingGreen.setLocation(150,50);
            puttingGreen.setFillColor(Color.green);
            puttingGreen.setFilled(true);
            add(puttingGreen);
      }
     
      //adds a yellow oval sand trap to the bottom-left of the green
      private void addSandTrap1() {
            GOval sandTrap1 = new GOval (100,100);
            sandTrap1.setColor(Color.yellow);
            sandTrap1.setLocation(100,300);
            sandTrap1.setFillColor(Color.yellow);
            sandTrap1.setFilled(true);
            add(sandTrap1);  
      }
     
      //adds a yellow oval sand trap to the bottom-right of the green
      private void addSandTrap2() {
            GOval sandTrap2 = new GOval (100,200);
            sandTrap2.setColor(Color.yellow);
            sandTrap2.setLocation(550,230);
            sandTrap2.setFillColor(Color.yellow);
            sandTrap2.setFilled(true);
            add(sandTrap2);
      }
     
      //adds a blue oval lake to the top left of the screen
      private void addLake() {
            GOval lake = new GOval (300, 500);
            lake.setColor(Color.blue);
            lake.setLocation(-150, -250);
            lake.setFilled(true);
            add(lake);
      }
     
      //adds a black oval hole in the left center of the putting green
      private void addHole() {
            GOval hole = new GOval (20,10);
            hole.setLocation(250,250);
            hole.setFilled(true);
            add(hole);
      }
     
      /*adds a black line (flagstick), coming out of the hole, with a red rectangle flag on
       * the top of the flagstick
       */
      private void addPin() {
            GLine flagStick = new GLine (260, 260, 260, 140);
            add(flagStick);
           
            GRect flag = new GRect (260, 140, 30, 20);
            flag.setColor(Color.red);
            flag.setFilled(true);
            add(flag);
      }
     
      //adds a white circle golf ball with black outline and two black horizontal lines to its top right
      private void addBall() {
            GOval ball = new GOval (7, 7);
            ball.setLocation(280, 230);
            ball.setFillColor(Color.white);
            ball.setColor(Color.black);
            ball.setFilled(true);
            add(ball);
           
            GLine streak1 = new GLine (291, 230, 301, 220);
            add(streak1);
           
            GLine streak2 = new GLine (287, 227, 297, 217);
            add(streak2);
      }
     
      //adds a red signature flush up against the bottom right corner of the screen.
      private void addSignature() {
            GLabel signature = new GLabel ("Artistry by Maverick McNealy. Go Stanford Golf!");
            signature.setFont("Sans Serif-25");
            signature.setColor(Color.red);
            Double xCoordinate = getWidth() - signature.getWidth();
            Double yCoordinate = getHeight() - signature.getDescent();
            signature.setLocation(xCoordinate, yCoordinate);
            add(signature);
      }

}


hole-in-one!