Building Java Programs

Lab 3: Parameters and Graphics

Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp.

lab document created by Whitaker Brand and Marty Stepp

Today's lab

Goals for today:

Exercise : Parameters

Exercise : Parameter Mystery practice-it

Exercise : Syntax errors

Exercise - answer

  1. line 5: cannot use variable y without declaring and initializing it
  2. line 5: cannot declare the type of y in the method call
  3. line 6: cannot call printer without the correct number of parameters (2, in this case)
  4. line 7: cannot call printer by passing the correct type of parameters (double, in this case)
  5. line 8: cannot refer to the variable z: it is in scope inside printer, not main
  6. line 11: must provide a type for x
  7. line 11: must provide a variable name for the second parameter
  8. line 12: must refer to the parameters using the exact same spelling
  9. line 13: cannot refer to variables in main that were not passed into printer as a parameter

Exercise - Corrected version

Cumulative algorithms

Exercise : Powers of 2 practice-it

Exercise : Powers of n practice-it

Exercise : Printing Strings practice-it

Exercise : printSquare practice-it

We suggest you skip this problem if the lab time is more than half over already.

Graphics

Now we'll explore several exercises related to drawing graphics. icon (lecture slides)

quilt drawingpanel compare

Exercise : Syntax errors

answer on next slide...

Exercise - answer

  1. line 1: incorrect import statement; should import java.awt.*
  2. line 5: missing word new before 2nd occurrence of DrawingPanel
  3. line 6: method name should be setBackground
  4. line 6: missing panel. before setBackground
  5. line 8: method name should be getGraphics
  6. line 9: the setColor method is part of object g, not panel
  7. line 9: should not write new before Color.BLUE
  8. line 11: method name should be drawRect
  9. line 11: missing two parameters from drawRect (width and height)
  10. line 12: color should be specified as Color.RED, not "RED"
  11. line 13: method name should be fillOval
  12. line 13: wrong parameter types; width/height must be integers

Exercise - corrected version

Exercise : Face

Write a complete Java program that draws the following output:

face

Exercise - answer

import java.awt.*;

public class Face1 {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(220, 150);
        Graphics g = panel.getGraphics();
        
        g.drawOval(10, 30, 100, 100);   // face outline
        g.setColor(Color.BLUE);
        g.fillOval(30, 60, 20, 20);     // eyes
        g.fillOval(70, 60, 20, 20);
        g.setColor(Color.RED);          // mouth
        g.drawLine(40, 100, 80, 100);
    }
}

Exercise : Stairs 1

Write a complete Java program to draw the following output using a for loop to draw the rectangular "stairs." Copy/paste the code template on the next slide into jGRASP as a template to fill in.

stairs 1

Exercise - code template

import java.awt.*;

public class Stairs1 {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(110, 110);
        Graphics g = panel.getGraphics();
        for (int i = 0; i < 10; i++) {
            your code goes here ;
        }
    }
}

Exercise - answer

import java.awt.*;

public class Stairs1 {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(110, 110);
        Graphics g = panel.getGraphics();
        for (int i = 0; i < 10; i++) {
            g.drawRect(5, 5 + 10*i, 10 + 10*i, 10);
        }
    }
}

Exercise - answer, alternative version

import java.awt.*;

public class Stairs1 {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(110, 110);
        Graphics g = panel.getGraphics();
        int x = 5;
        int y = 5;
        int width = 10;
        int height = 10;
        for (int i = 0; i < 10; i++) {
            g.drawRect(x, y, width, height);
            y = y + 10;           // move down and widen by 10
            width = width + 10;
        }
    }
}

Exercise : Stairs 2

Modify your previous Java program to draw each of the following outputs. Modify only the body inside your for loop.

stairs 1stairs 2 stairs 3 stairs 4

Exercise - answer

To get each output, change the for loop body to the following:

// output 2
g.drawRect(5, 5 + 10*i, 100 - 10*i, 10);
// output 3
g.drawRect(95 - 10*i, 5 + 10*i, 10 + 10*i, 10);
// output 4
g.drawRect(5 + 10*i, 5 + 10*i, 100 - 10*i, 10);

Parameterized methods and Graphics

When you want to divide a graphical program into multiple drawing methods, you must pass Graphics g as a parameter in addition to any other parameters. Example:

public static void main(String[] args) {
    DrawingPanel panel = new DrawingPanel(400, 300);
    Graphics g = panel.getGraphics();
    ...
    drawStuff(g, 13, 52, 7);
}

public static void drawStuff(Graphics g, int a, int b, int c) {
    g.drawLine(a, 45, b, c);
    ...
}

Exercise : Face 2

Modify your previous Face program to draw the following output. Write a parameterized method that allows you to draw a face at different positions.

face 2

Exercise - answer

import java.awt.*;

public class Face2 {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(320, 180);
        Graphics g = panel.getGraphics();
        drawFace(g, 10, 30);
        drawFace(g, 150, 50);
    }
    
    public static void drawFace(Graphics g, int x, int y) {
        g.setColor(Color.BLACK);
        g.drawOval(x, y, 100, 100);
        g.setColor(Color.BLUE);
        g.fillOval(x + 20, y + 30, 20, 20);
        g.fillOval(x + 60, y + 30, 20, 20);
        g.setColor(Color.RED);
        g.drawLine(x + 30, y + 70, x + 70, y + 70);
    }
}

Exercise : Face 3

Modify your previous Java program to draw the following output. Use a for loop with your parameterized method to draw faces at different positions.

face 2

Exercise - answer

import java.awt.*;

public class Face3 {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(520, 180);
        Graphics g = panel.getGraphics();
        for (int i = 0; i < 5; i++) {
            drawFace(g, 10 + i * 100, 30);
        }
    }
    
    public static void drawFace(Graphics g, int x, int y) {
        g.setColor(Color.BLACK);
        g.drawOval(x, y, 100, 100);
        g.setColor(Color.BLUE);
        g.fillOval(x + 20, y + 30, 20, 20);
        g.fillOval(x + 60, y + 30, 20, 20);
        g.setColor(Color.RED);
        g.drawLine(x + 30, y + 70, x + 70, y + 70);
    }
}

Exercise : Spiral

Write a Java program that draws the following output using a for loop.

spiral

Exercise - answer

import java.awt.*;

public class Spiral {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(170, 170);
        Graphics g = panel.getGraphics();
        for (int i = 0; i < 8; i++) {
            g.drawLine(      10*i,   10 + 10*i, 160 - 10*i,  10 + 10*i);  // top
            g.drawLine(160 - 10*i,   10 + 10*i, 160 - 10*i, 160 - 10*i);  // right
            g.drawLine( 10 + 10*i,  160 - 10*i, 160 - 10*i, 160 - 10*i);  // bottom
            g.drawLine( 10 + 10*i,   20 + 10*i,  10 + 10*i, 160 - 10*i);  // left
        }
    }
}

Exercise - answer, alternative version

import java.awt.*;

public class Spiral {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(170, 170);
        Graphics g = panel.getGraphics();
        int x = 0, y = 10;
        int len = 160;
        for (int i = 0; i < 8; i++) {
            g.drawLine(x, y, x + len, y);  // right
            x = x + len;
            len = len - 10;
            g.drawLine(x, y, x, y + len);  // down
            y = y + len;
            g.drawLine(x, y, x - len, y);  // left
            x = x - len;
            len = len - 10;
            g.drawLine(x, y, x, y - len);  // up
            y = y - len;
        }
    }
}

Exercise : printGrid practice-it

We suggest you skip this problem if the lab time is more than half over already.

Exercise : Sierpinski Triangle

Write a Java program that draws the following output.... Just kidding!

sierpinski triangle

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from Chapter 3 or Supplement 3G and try to solve them! Note that you won't be able to do every problem from Chapter 3; some involve concepts we have not learned yet, such as return and Scanner.