Sunday, February 23, 2014

More Java Learning Chapter 2 exercises... 1-5 done :)

But first a CUTE picture of LOGAN got to hang with him today, trying to get him and puddi to be good friends so when the new baby comes I can watch him while Charles and Debbie are in the hospital.

Now for the Assignment to draw a house using the functions available in the acm.jar.

it should look like this:
import acm.graphics.*;
import acm.program.*;

public class HouseProgram extends GraphicsProgram{

    /**
     * @param args
     */
    public void run() {
       
        drawRoof();
        drawHouse();
        drawWindows();
        drawDoor();
    }

    private void drawRoof(){
        add(new GLine(80, 100, 235, 20));
        add(new GLine(235, 20, 380, 100));
    }
   
    private void drawHouse() {
        add(new GRect(80, 100, 300, 200));
    }
   
    private void drawWindows() {
        add(new GRect(105, 130, 70, 70));
        add(new GRect(285, 130, 70, 70));
    }
   
    private void drawDoor() {
        add(new GRect(195, 180, 70, 120));
        add(new GOval(245, 230, 10, 10));
    }
}

And then my Robot Face: the robot has orange eyes, a black nose and and a white mouth:

/* RobotProgram.java creates a robot face from the ACM package graphics */

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class RobotProgram extends GraphicsProgram{

    public void run() {
        drawFace();
        drawEyes();
        drawNose();
        drawMouth();
    }
   
    private void drawFace() {
        GRect face = new GRect(220, 40, 250, 350);
        face.setFilled(true);
        face.setColor(Color.LIGHT_GRAY);
        add(face);
    }
   
    private void drawEyes() {
        GOval lEye = new GOval(260, 95, 50, 50);
        GOval rEye = new GOval(380, 95, 50, 50);
        lEye.setFilled(true);
        lEye.setColor(Color.ORANGE);
        rEye.setFilled(true);
        rEye.setColor(Color.ORANGE);
        add(lEye);
        add(rEye);
    }
   
    private void drawNose() {
        GRect nose = new GRect(335, 185, 20, 40);
        nose.setFilled(true);
        nose.setColor(Color.BLACK);
        add(nose);
    }
   
    private void drawMouth() {
        GRect mouth = new GRect(270, 280, 155, 50);
        mouth.setFilled(true);
        mouth.setColor(Color.WHITE);
        add(mouth);
    }   
}

this is all just using the basic functions and making a guess and coordinates so a lot of draw and look type programming.. we will see how it changes when the class teaches to figure out the coordinates so it draws it without the guess work :)

Enjoy

No comments:

Post a Comment