Download Notes for 6th period

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Read through the slides for your
notes today.
Use the following slide as a
reference for your graphics
program
// Java0708.java
// This program places the six methods from the
// previous program into their own <House> class.
import java.awt.*;
import java.applet.*;
public class Java0708 extends Applet
{
public void paint(Graphics g)
{
House.drawFloors(g);
House.drawRoof(g);
House.drawDoor(g);
House.drawWindows(g);
House.drawChimney(g);
}
}
class House
{
public static void drawFloors(Graphics g)
{
Expo.setColor(g,Expo.blue);
Expo.drawRectangle(g,200,200,500,300);
Expo.drawRectangle(g,200,300,500,400);
}
public static void drawRoof(Graphics g)
{
Expo.setColor(g,Expo.red);
Expo.drawLine(g,200,200,350,100);
Expo.drawLine(g,500,200,350,100);
Expo.drawLine(g,200,200,500,200);
}
public static void drawDoor(Graphics g)
{
Expo.setColor(g,Expo.black);
Expo.drawRectangle(g,330,340,370,400);
Expo.drawOval(g,350,370,10,20);
Expo.fillCircle(g,366,370,3);
}
public static void drawWindows(Graphics g)
{
Expo.setColor(g,Expo.black);
Expo.drawRectangle(g,220,220,280,280);
Expo.drawLine(g,220,250,280,250);
Expo.drawLine(g,250,220,250,280);
Expo.drawRectangle(g,420,220,480,280);
Expo.drawLine(g,420,250,480,250);
Expo.drawLine(g,450,220,450,280);
Expo.drawRectangle(g,320,220,380,280);
Expo.drawLine(g,320,250,380,250);
Expo.drawLine(g,350,220,350,280);
Expo.drawRectangle(g,220,320,280,380);
Expo.drawLine(g,220,350,280,350);
Expo.drawLine(g,250,320,250,380);
Expo.drawRectangle(g,420,320,480,380);
Expo.drawLine(g,420,350,480,350);
Expo.drawLine(g,450,320,450,380);
}
}
public static void drawChimney(Graphics g)
{
Expo.setColor(g,Expo.red);
Expo.drawLine(g,420,146,420,80);
Expo.drawLine(g,420,80,450,80);
Expo.drawLine(g,450,80,450,166);
}
The next two slide are important
for your understanding of using
parameters
Important Rules About Using
Parameters with Methods #1
The number of parameters in the method call must
match the number of parameters in the method heading.
The corresponding parameters in the method call must
be the same type as the parameters in the heading.
The sequence of the parameters in the method call must
match the sequence of the parameters in the heading.
The parameter identifiers in the method call may be the
same identifier or a different identifier as the parameters
in the heading.
Important Rules About Using
Parameters with Methods #2
The number of actual parameters must match the
number of formal parameters.
The corresponding actual parameters must be the same
type as the formal parameters.
The sequence of the actual parameters must match the
sequence of the formal parameters.
The actual parameter identifiers may be the same
identifier as or a different identifier from as the formal
parameters.
These rules say the same thing as the previous slide.
The next two slides are important
for you to understand the
terminology. This will help you talk
to each other intelligently and ask
meaningful questions.
Variable Terminology
Variables that are declared inside a method or block are called
local variables.
Local variables are only accessible inside the method or block
that they are defined in.
Variables that are declared inside a class, but outside any
method, are class variables.
Class variables are accessible by any method of the class.
Class variables are also called attributes.
If a variable is only used by one method, it should be declared
inside that method as a local variable.
If a variable is used by 2 or more methods of a class, it should
be declared as a class variable.
Program Design Notes
This was the first introduction to program design.
Additional design features will be introduced as you learn more
object-oriented programming.
At this stage you can already consider the following:
•
Programs should use self-commenting identifiers.
•
Control structures and block structure need to use a
consistent indentation style.
•
Specific tasks should be placed in modules called methods.
•
Similar methods accessing the same data should be placed
in a class.
•
The main method should be used for program sequence,
not large numbers of program statements.
This next section should give you a
few ideas to help with your
graphics program.
// Java0725.java
// This program demonstrates a <picket> method that will be used to display a fence.
import java.awt.*;
import java.applet.*;
public class Java0725 extends Applet
{
public void paint(Graphics g)
{
Expo.setBackground(g,Expo.black);
Expo.setColor(g,Expo.tan);
Expo.fillRectangle(g,0,500,1000,525);
Expo.fillRectangle(g,0,600,1000,625);
}
Expo.setColor(g,Expo.brown);
for (int x = 2; x < 1000; x+=40)
{
picket(g,x);
}
public static void picket(Graphics g, int x)
{
Expo.fillPolygon(g,x,650,x,500,x+18,450,x+36,500,x+36,650);
Expo.delay(250); // delay for ¼ a second
}
}
The Logic of the picket Method
picket(g,x);
x+18,450
All graphics methods need g.
x is the horizontal value of the bottom
left corner of the picket.
x,500
x+36,500
The y (vertical) value of the bottom left
corner is always 650 since all pickets
will be at the bottom of the screen.
The other 4 coordinates of the picket
are relative to the point (x,650).
x,650
x+36,650
// Java0726.java
// This program uses the <picket> method to create the <fence> method.
import java.awt.*;
import java.applet.*;
public class Java0726 extends Applet
{
public void paint(Graphics g)
{
Expo.setBackground(g,Expo.black);
fence(g);
}
public static void picket(Graphics g, int x)
{
Expo.fillPolygon(g,x,650,x,500,x+18,450,x+36,500,x+36,650);
Expo.delay(250); // delay for ¼ a second
}
public static void fence(Graphics g)
{
// cross beams
Expo.setColor(g,Expo.tan);
Expo.fillRectangle(g,0,500,1000,525);
Expo.fillRectangle(g,0,600,1000,625);
// pickets
Expo.setColor(g,Expo.brown);
for (int x = 2; x < 1000; x+=40)
{
picket(g,x);
}
}
}
// Java0727.java
// This program combines many user-defined methods to create a graphics image.
import java.awt.*;
import java.applet.*;
public static void moon(Graphics g)
{
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,920,85,70);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,895,70,60);
}
public class Java0727 extends Applet
{
public void paint(Graphics g)
{
Expo.setBackground(g,Expo.black);
nightSky(g);
fence(g);
}
public static void picket(Graphics g, int x)
{
Expo.fillPolygon(g,x,650,x,500,x+18,450,
x+36,500,x+36,650);
Expo.delay(250); // delay for ¼ a second
}
public static void randomStar(Graphics g, int x)
{
int y = Expo.random(25,175);
int radius = Expo.random(15,20);
int points = Expo.random(5,10);
Expo.setRandomColor(g);
Expo.fillStar(g,x,y,radius,points);
Expo.delay(250); // delay for ¼ a second
}
public static void nightSky(Graphics g)
{
moon(g);
for (int x = 25; x <= 825; x+= 50)
{
randomStar(g,x);
}
}
public static void fence(Graphics g)
{
// cross beams
Expo.setColor(g,Expo.tan);
Expo.fillRectangle(g,0,500,1000,525);
Expo.fillRectangle(g,0,600,1000,625);
// pickets
Expo.setColor(g,Expo.brown);
for (int x = 2; x < 1000; x+=40)
{
picket(g,x);
}
}
}
// Java0728.java
// This program does the exact same thing as the previous program, but without the structure methods.
// Several method calls are commented out and in their place is the code from that method.
// This is also done for some of the methods from the Expo class.
// The intent is to show that using methods makes your program much more readable and easier to
// work with.
import java.awt.*;
import java.applet.*;
public class Java0728 extends Applet
{
public void paint(Graphics g)
{
///////////////////////////////////////////////////////////////
// Expo.setBackground(g,Expo.black);
Expo.setColor(g,Expo.black);
Expo.fillRectangle(g,0,0,4800,3600);
//////////////////////////////////////////////////////////////
// nightSky(g);
//////////////////////////////////////////////////////////
// moon(g);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,920,85,70);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,895,70,60);
// continued on the next several slides…
for (int centerX = 25; centerX <= 825; centerX += 50)
{
int centerY = Expo.random(25,175);
int radius = Expo.random(15,20);
int points = Expo.random(5,10);
//////////////////////////////////////////////////////
// Expo.setRandomColor(g);
int red = Expo.random(0,255);
int green = Expo.random(0,255);
int blue = Expo.random(0,255);
Expo.setColor(g,red,green,blue);
/////////////////////////////////////////////////////
// Expo.fillStar(g,x,y,radius,points);
int halfRadius = radius / 2;
switch(points)
{
case 3 : halfRadius = 140 * radius / 500; break;
case 4 : halfRadius = 170 * radius / 400; break;
case 5 : halfRadius = 192 * radius / 500; break;
case 6 : halfRadius = 233 * radius / 400; break;
case 7 : halfRadius = 179 * radius / 500; break;
case 8 : halfRadius = 215 * radius / 400; break;
case 9 : halfRadius = 173 * radius / 500; break;
case 10 : halfRadius = 212 * radius / 400; break;
default : if (points < 52)
{
if (points % 2 == 1)
halfRadius = (180-points) * radius / 500;
else
halfRadius = (222-points) * radius / 400;
}
else
halfRadius = radius / 2;
}
int p = points;
points *= 2;
int xCoord[] = new int[points];
int yCoord[] = new int[points];
int currentRadius;
for (int k = 0; k < points; k++)
{
if (k % 2 == 0)
currentRadius = radius;
else
currentRadius = halfRadius;
xCoord[k] = (int) Math.round(Math.cos(2 * Math.PI *
k/points - Math.PI/2) * currentRadius) + centerX;
yCoord[k] = (int) Math.round(Math.sin(2 * Math.PI *
k/points - Math.PI/2) * currentRadius) + centerY;
}
int x = (p-5)/2+1;
if (p >= 5 && p <= 51)
switch(p % 4)
{
case 1 : yCoord[x] = yCoord[x+1] = yCoord[points-x-1] =
yCoord[points-x]; break;
case 2 : yCoord[x] = yCoord[x+1] = yCoord[points-x-1] =
yCoord[points-x];
yCoord[x+3] = yCoord[x+4] = yCoord[points-x-4] =
yCoord[points-x-3]; break;
case 3 : yCoord[x+2] = yCoord[x+3] = yCoord[points-x-3] =
yCoord[points-x-2];
}
g.fillPolygon(xCoord,yCoord,points);
///////////////////////////////////////////////////////
// Expo.delay(250); // delay for ¼ a second
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < 250)
endDelay = System.currentTimeMillis();
}
///////////////////////////////////////////////////////////////
// fence(g);
// cross beams
Expo.setColor(g,Expo.tan);
Expo.fillRectangle(g,0,500,1000,525);
Expo.fillRectangle(g,0,600,1000,625);
// pickets
Expo.setColor(g,Expo.brown);
for (int x = 2; x < 1000; x+=40)
{
//////////////////////////////////////////////////////
// picket(g,x);
//////////////////////////////////////////////////
//Expo.fillPolygon(g,x,650,x,500,x+18,450,x+36,500,x+36,650);
Polygon poly = new Polygon();
poly.addPoint(x,650);
poly.addPoint(x,500);
poly.addPoint(x+18,450);
poly.addPoint(x+36,500);
poly.addPoint(x+36,650);
g.fillPolygon(poly);
//////////////////////////////////////////////////
// Expo.delay(250); // delay for ¼ a second
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < 250)
endDelay = System.currentTimeMillis();
}
}
}
Next open Labs and follow the
directions for the graphics lab. If
your graphic is too easy you will
not receive credit. Due date will be
Friday, Feb. 17.
Related documents