Methods, Graphics Programming

Tom Kelliher, CS 116

Sept. 13, 2000

Administrivia

Announcements

On the class home page, there is a link to online documentation for Java 2 packages and classes: Java2 Packages and Classes Reference.

Assignment

Look carefully at the Logo example in Section 2.3.

From Last Time

Applets, methods.

Outline

  1. Applet example: Add.java.

  2. Graphics programming

Coming Up

Logo example: elements of programming style.

Another Applet Example

Observe:

  1. The add() method:
    1. Definition vs. call.

    2. Formal vs. actual parameters.

//======================================================================
//      Project:    CS 116, applet demo
//      Author:     Tom Kelliher
//      File:       Add.java
//      Purpose:    To further demonstrate some simple properties of
//                  applets.
//======================================================================


import java.applet.*;
import java.awt.*;


//======================================================================
//      Class:      add
//      Purpose:    Display the sum of two integers.
//======================================================================

public class Add extends Applet
{
   int augend = -45;
   int addend =  34;
   Font f = new Font("Helvetica",Font.BOLD,18);


   //===================================================================
   //   Method:     init
   //   Purpose:    Set background color.
   //===================================================================

   public void init()
   {
      setBackground(Color.blue);
   }


   //===================================================================
   //   Method:     paint
   //   Purpose:    Set foreground color and font.
   //               Display the sum string.
   //   Input:      A graphics object.
   //===================================================================

   public void paint(Graphics g)
   {
      int sum;

      sum = add(augend, addend);

      g.setColor(Color.yellow);
      g.setFont(f);
      g.drawString(augend + " + " + addend + " = " + sum, 50, 50);
   }


   //===================================================================
   //   Method:     add
   //   Purpose:    Add two integers, returning their value.
   //   Input:      Two integers.
   //   Output:     Returns the sum of the integer.
   //===================================================================

   public int add(int a, int b)
   {
      int s;   // The sum.

      s = a + b;
      return s;
   }
}
Run the applet.

Relationship between method call and method declaration:

Method call sequence:

  1. Compute values of actual parameters.

  2. Assign these values to formal parameters.

  3. Call the method.

Consider this example:
int a = 10;
int b = 12;
int sum;
sum = add(a + 5, b / 3);   // What is sum's value?

Graphics Programming

A few notes:

  1. Drawing methods:
    drawOval(int x, int y, int width, int height);   // Draws oval outline.
    fillOval(int x, int y, int width, int height);   // Draws filled oval.
    
    Figures drawn in current color.

  2. Custom colors:
    Color myColor = new Color(100, 0, 255);
    
    Red, green, blue components in range 0...255.

  3. Working with fonts:
    1. Font: SansSerif, Serif, Monospaced, etc.

    2. Style: plain, bold, italic.

    3. Size: in pixels.

Example

Assume the following applet runs in a window of size 400 pixels by 400 pixels. What will it look like? Predict what it will look like, then grab the source code from the class home page, create a project, and run.

//======================================================================
//      Project:    CS 116, Simple 2-D graphics demo
//      Author:     Tom Kelliher
//      File:       Graphics2d.java
//      Purpose:    To demonstrate some simple 2-D graphics and some
//                  object manipulation
//======================================================================

import java.applet.*;
import java.awt.*;

//======================================================================
//      Class:      Graphics2d
//      Purpose:    Display some simple graphical elements.
//======================================================================

public class Graphics2d extends Applet
{

   //===================================================================
   //   Method:     init
   //   Purpose:    Set background color.
   //===================================================================

   public void init()
   {
      setBackground(Color.black);
   }

   //===================================================================
   //   Method:     paint
   //   Purpose:    Display the graphical elements.
   //   Input:      A graphics object.
   //===================================================================

   public void paint(Graphics g)
   {
      // Create two custom colors.
      Color myColor1 = new Color(200, 0, 100);
      Color myColor2 = new Color(0, 150, 200);

      // Create a rectangular box for sizing graphical elements later.
      Rectangle myRect = new Rectangle(50, 100, 50, 100);

      // Draw a filled rectangle.
      g.setColor(myColor1);
      g.fillRect(myRect.x, myRect.y, myRect.width, myRect.height);

      // Draw a second filled rectangle, shifted from the first.
      myRect.translate(100, 100);
      g.setColor(myColor2);
      g.fillRect(myRect.x, myRect.y, myRect.width, myRect.height);

      // Draw a filled oval, shifted yet again.
      myRect.translate(0, -150);
      g.setColor(myColor1);
      g.fillOval(myRect.x, myRect.y, myRect.width, myRect.height);

      // Shrink the rectangular box by one-half and draw an oval
      // outline.
      myRect.grow(-12, -25);
      g.setColor(Color.black);
      g.drawOval(myRect.x, myRect.y, myRect.width, myRect.height);

      // Draw a line.
      g.setColor(myColor2);
      g.drawLine(50, 50, 350, 350);

      // Draw text.
      g.setColor(Color.red);
      g.setFont(new Font("Serif", Font.ITALIC, 20));
      g.drawString("The 20 Pt. italic Serif font", 20, 40);

      // Draw more text.
      g.setColor(Color.green);
      g.setFont(new Font("SansSerif", Font.BOLD, 30));
      g.drawString("The 30 Pt. bold SansSerif font", 20, 375);
   }
}



Thomas P. Kelliher
Tue Sep 12 20:48:54 EDT 2000
Tom Kelliher