Tom Kelliher, CS 116
Sept. 15, 2000
Read Section 2.4 and the Lab 2 handout.
Methods and graphics programming.
Lab 2.
typeName identifier; typeName identifier = value;
int x;
int x = 10;
Color Color1, Color2;
Rectangle rect1 = new Rectangle(200, 20, 40, 40),
rect2 = new Rectangle(10, 10, 20, 60);
Example:
circumference = 2 * 3.14159 * radius; // Avoid. float PI = 3.14159; circumference = 2 * PI * radius; // Preferable.Why are constants bad? (Maintenance, readability).
Add, SodaMachine.
add(), accumulateSum().
sum, myColor.
PI, BASE_OFFSET.
import java.applet.Applet;import java.awt.Graphi
cs;import java.awt.Color;
public class Logo1 extends Applet{
public void init()
{setBackground(Color.black);}
public void paint(Graphics g){
g.setColor(Color.red);
g.drawRect(50,80,100,100);
g.setColor(Color.white);
g.drawRect(75,105,150,150);
g.drawRect(76,106,148,148);
g.setColor(Color.blue);
g.drawRect(100,130,200,200);
g.drawRect(101,131,198,198);
g.drawRect(102,132,196,196);}}
//======================================================================
// Project: CS 116, Logo1 applet demo
// Author: Tom Kelliher
// File: Logo1.java
// Purpose: First version of the Logo demo. This applet
// displays a simple logo consisting of three squares
// of different colors. Everything is "hard-coded" in
// this version of the applet.
//======================================================================
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
//======================================================================
// Class: Logo1
// Purpose: Display a simple logo, as described above.
//======================================================================
public class Logo1 extends Applet
{
//===================================================================
// Method: init
// Purpose: Set background color.
//===================================================================
public void init()
{
setBackground(Color.black);
}
//===================================================================
// Method: paint
// Purpose: Display the logo
// Input: A graphics object.
//===================================================================
public void paint(Graphics g)
{
// Render the first square.
g.setColor(Color.red);
g.drawRect(50, 80, 100, 100);
// Render the second square.
g.setColor(Color.white);
g.drawRect(75, 105, 150, 150);
g.drawRect(76, 106, 148, 148);
// Render the third square.
g.setColor(Color.blue);
g.drawRect(100, 130, 200, 200);
g.drawRect(101, 131, 198, 198);
g.drawRect(102, 132, 196, 196);
}
}
//======================================================================
// Project: CS 116, Logo2 applet demo
// Author: Tom Kelliher
// File: Logo2.java
// Purpose: Second version of the Logo demo. This applet
// displays a simple logo consisting of three squares
// of different colors. The code in the paint() method
// has been re-written to ease modification.
//======================================================================
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
//======================================================================
// Class: Logo2
// Purpose: Display a simple logo, as described above.
//======================================================================
public class Logo2 extends Applet
{
//===================================================================
// Method: init
// Purpose: Set background color.
//===================================================================
public void init()
{
Color BACKGROUND_COLOR = Color.black;
setBackground(BACKGROUND_COLOR);
}
//===================================================================
// Method: paint
// Purpose: Display the logo
// Input: A graphics object.
//===================================================================
public void paint(Graphics g)
{
int x = 50; // X coordinate of first square.
int y = 80; // Y coordinate of first square.
int size = 100; // Size of first square.
int OFFSET = 25; // Coordinate offset between squares.
int INCREMENT = 50; // Size increment between squares.
Color COLOR1 = Color.red; // Color of first square.
Color COLOR2 = Color.white; // Color of second square.
Color COLOR3 = Color.blue; // Color of third square.
// Render the first square.
g.setColor(COLOR1);
g.drawRect(x, y, size, size);
// Render the second square.
g.setColor(COLOR2);
// Update coordinates and size for second square.
x = x + OFFSET;
y = y + OFFSET;
size = size + INCREMENT;
// Draw square with a line width of two.
g.drawRect(x, y, size, size);
g.drawRect(x + 1, y + 1, size - 2, size - 2);
// Render the third square.
g.setColor(COLOR3);
// Update.
x = x + OFFSET;
y = y + OFFSET;
size = size + INCREMENT;
// Draw square with a line width of three.
g.drawRect(x, y, size, size);
g.drawRect(x + 1, y + 1, size - 2, size - 2);
g.drawRect(x + 2, y + 2, size - 4, size - 4);
}
}