172

Introduction

  • Upload
    nickan

  • View
    58

  • Download
    0

Embed Size (px)

DESCRIPTION

Section 15.1. Introduction. Layout of Chapter 15. This final chapter is special and focuses primarily on graphics. There are no homework exercises, quizzes or tests with this chapter. However, there are 2 MAJOR Lab Projects based on the information in this chapter. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction
Page 2: Introduction

Layout of Chapter 15This final chapter is special and focuses primarily on graphics.

There are no homework exercises, quizzes or tests with this chapter.

However, there are 2 MAJOR Lab Projects based on the information in this chapter.

The first half of the chapter focuses on skills needed for the first project. The second half focuses on skills needed for the Final Project.

Page 3: Introduction
Page 4: Introduction

Ignore Deprecated API Warning Messages

Java is constantly improving. When newer and improved classes become available, the older classes are officially labeled “deprecated”. When you use a deprecated class, you get a warning message as seen below. Warnings are not the same thing as errors. Warnings can be ignored. We are choosing to use the older technique for mouse interaction because it is simpler. The newer technique may be preferred, but it requires knowledge of AP Computer Science topics.

Page 5: Introduction

// Java1501.java// This program counts the number of times a mouse is clicked.// Both left-clicks and right-clicks are counted using the <mouseDown> event. // Ignore the "deprecated API" warning.

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

public class Java1501 extends Applet{

int numClicks;

public void init() { numClicks = 0; }public void paint(Graphics g){

g.drawString("Mouse is clicked " + numClicks + " times.",20,20);}

public boolean mouseDown(Event e, int x, int y){

numClicks++;repaint();return true;

}}

Page 6: Introduction

// Java1502.java// This program displays the position of the mouse every time it is clicked// using the <mouseDown> method.

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

public class Java1502 extends Applet{

int xCoord, yCoord;

public void paint(Graphics g){

g.drawString("Mouse clicked at (" + xCoord + "," + yCoord + ")",20,20);}

public boolean mouseDown(Event e, int x, int y){

xCoord = x;yCoord = y;repaint();return true;

}}

Page 7: Introduction

// Java1503.java// This program demonstrates how to determine if the mouse is inside or// outside the Applet window using the <mouseEnter> and <mouseExit> methods.import java.applet.Applet;import java.awt.*;public class Java1503 extends Applet{

boolean insideApplet;public void paint(Graphics g){

if (insideApplet)g.drawString("Mouse is inside applet",20,20);

elseg.drawString("Mouse is outside applet",20,20);

}public boolean mouseEnter(Event e, int x, int y){

insideApplet = true;repaint();return true;

}public boolean mouseExit(Event e, int x, int y){

insideApplet = false;repaint();return true;

}}

Page 8: Introduction

// Java1504.java// This program determines if a mouse is clicked once or twice using the// <clickCount> method. This method works for the left or right button. 

boolean singleClick,doubleClick;

public void paint(Graphics g){

if (singleClick)g.drawString("Mouse was clicked once",20,20);

if (doubleClick)g.drawString("Mouse was clicked twice",20,20);

}

public boolean mouseDown(Event e, int x, int y){

switch (e.clickCount){

case 1: singleClick = true; doubleClick = false;break;

case 2:doubleClick = true; singleClick = false;

}repaint();return true;

}

Page 9: Introduction

// Java1505.java// This program displays the position of the mouse every time it moves// using the <mouseMove> method.

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

public class Java1505 extends Applet{

int xCoord, yCoord;

public void paint(Graphics g){

g.drawString("Mouse is located at (" + xCoord + "," + yCoord + ")",20,20);}

public boolean mouseMove(Event e, int x, int y){

xCoord = x;yCoord = y;repaint();return true;

}}

Page 10: Introduction
Page 11: Introduction

// Java1506.java// This program demonstrates how display to a single graphics image from a .gif file// at any x,y location on the screen. The same image can be displayed multiple times.

import java.awt.*;

public class Java1506 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"LSchram.gif");}

public void paint(Graphics g){

g.drawImage(picture,0,0,this);g.drawImage(picture,750,200,this);g.drawImage(picture,350,450,this);

}}

Page 12: Introduction
Page 13: Introduction

// Java1507.java// This program demonstrates that several different images can be displayed.import java.awt.*;public class Java1507 extends java.applet.Applet{

Image picture1, picture2, picture3, picture4, picture5, picture6;public void init(){

picture1 = getImage(getDocumentBase(),"Knight.gif");picture2 = getImage(getDocumentBase(),"MouseWithCheese.gif");picture3 = getImage(getDocumentBase(),"smile.gif");picture4 = getImage(getDocumentBase(),"shark.gif");picture5 = getImage(getDocumentBase(),"Spider.gif");picture6 = getImage(getDocumentBase(),"TEDDY.gif");

}public void paint(Graphics g){

g.drawImage(picture1,100,100,this);g.drawImage(picture2,400,100,this);g.drawImage(picture3,700,100,this);g.drawImage(picture4,100,400,this);g.drawImage(picture5,400,400,this);g.drawImage(picture6,700,400,this);

}}

Page 14: Introduction
Page 15: Introduction

// Java1508.java// This program demonstrates that animated gifs are inserted in the same way as// normals gifs. Animated gifs can cause a flickering side-effect on the screen.import java.awt.*;public class Java1507 extends java.applet.Applet{

Image picture1, picture2, picture3, picture4, picture5, picture6;public void init(){

picture1 = getImage(getDocumentBase(),"space.gif");picture2 = getImage(getDocumentBase(),"computer.gif");picture3 = getImage(getDocumentBase(),"gears.gif");picture4 = getImage(getDocumentBase(),"butterfly.gif");picture5 = getImage(getDocumentBase(),"pizza.gif");picture6 = getImage(getDocumentBase(),"jet.gif");

}public void paint(Graphics g){

g.drawImage(picture1,100,100,this);g.drawImage(picture2,450,100,this);g.drawImage(picture3,700,100,this);g.drawImage(picture4,100,400,this);g.drawImage(picture5,400,350,this);g.drawImage(picture6,700,400,this);

}}

Page 16: Introduction
Page 17: Introduction

// Java1509.java// This program demonstrates that .png files also work.import java.awt.*;public class Java1509 extends java.applet.Applet{

Image picture1, picture2, picture3, picture4, picture5, picture6;public void init(){

picture1 = getImage(getDocumentBase(),"Bunny.png");picture2 = getImage(getDocumentBase(),"pineapple.png");picture3 = getImage(getDocumentBase(),"Penguin.png");picture4 = getImage(getDocumentBase(),"Koala.png");picture5 = getImage(getDocumentBase(),"house.png");picture6 = getImage(getDocumentBase(),"bird.png");

}public void paint(Graphics g){

g.drawImage(picture1,100,100,this);g.drawImage(picture2,400,100,this);g.drawImage(picture3,700,100,this);g.drawImage(picture4,100,400,this);g.drawImage(picture5,400,400,this);g.drawImage(picture6,700,400,this);

}}

Page 18: Introduction
Page 19: Introduction

// Java1510.java// This program demonstrates .jpg files can be used as well.// .jpg files are typically used for photographs.

import java.awt.*;

public class Java1510 extends java.applet.Applet{

Image picture1, picture2, picture3, picture4;

public void init(){

picture1 = getImage(getDocumentBase(),"Paris.jpg");picture2 = getImage(getDocumentBase(),"LetSleepingDogsLie.jpg");picture3 = getImage(getDocumentBase(),"AllSmiling.jpg");picture4 = getImage(getDocumentBase(),"Schrams.jpg");

}

public void paint(Graphics g){

g.drawImage(picture1,0,0,this);g.drawImage(picture2,500,0,this);g.drawImage(picture3,0,330,this);g.drawImage(picture4,316,330,this);

}}

Page 20: Introduction
Page 21: Introduction
Page 22: Introduction

// Java1511.java // Problem #1 -- The image file has the wrong format.// This program demonstrates that .bmp files cannot be displayed. // Nothing will show up.// To fix this, load the bitmap file in Paint and resave it as a .gif, // .png or .jpg file.

import java.awt.*;

public class Java1511 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"ShortCircuit.bmp");}

public void paint(Graphics g){

g.drawImage(picture,100,100,this);}

}

Page 23: Introduction
Page 24: Introduction

Fixing the ProblemLoad ShortCircut.bmpin Microsoft Paint.

If double-clicking the file does not work, right click the file, select “Open with” and then select “Paint”.

Page 25: Introduction

Click this tab.

Page 26: Introduction

Click “Save as”.

Page 27: Introduction

Now select PNG, JPEG or GIF.

Page 28: Introduction

Click Save.

Page 29: Introduction

// Java1511.java // Problem #1 -- The image file has the wrong format.// This program demonstrates that .bmp files cannot be displayed. // Nothing will show up.// To fix this, load the bitmap file in Paint and resave it as a .gif, // .png or .jpg file.

import java.awt.*;

public class Java1511 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"ShortCircuit.png");}

public void paint(Graphics g){

g.drawImage(picture,100,100,this);}

}

Return to JCreator.Remember to alter the nameof the file so it matches thename of the new file that youjust created. Execute again.

Page 30: Introduction
Page 31: Introduction

// Java1512.java// Problem #2 -- The image file is not in the correct location.// This program demonstrates that an image cannot be displayed if it is not in the// correct folder. When you execute the program, nothing is displayed. Move the // "DallasSkyline.png" file from the "Dallas" subfolder to the "Programs15" folder // and execute the program again. After the file is moved, the program should work.

import java.awt.*;

public class Java1512 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"DallasSkyline.png");}

public void paint(Graphics g){

g.drawImage(picture,0,100,this);}

}

Page 32: Introduction
Page 33: Introduction

Fixing the ProblemThe problem is that the image file is in the Dallas subfolder and not in the Programs15 folder with the rest of the files.

Open the Dallas subfolder.

Page 34: Introduction

Right-click this file and select “Copy”.

Page 35: Introduction

Return to the Programs15 folder and paste the image file.

Page 36: Introduction

// Java1512.java// Problem #2 -- The image file is not in the correct location.// This program demonstrates that an image cannot be displayed if it is not in the// correct folder. When you execute the program, nothing is displayed. Move the // "DallasSkyline.png" file from the "Dallas" subfolder to the "Programs15" folder // and execute the program again. After the file is moved, the program should work.

import java.awt.*;

public class Java1512 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"DallasSkyline.png");}

public void paint(Graphics g){

g.drawImage(picture,0,100,this);}

}

Return to JCreator.Execute the program again.This time no alterations to the program are necessary.

Page 37: Introduction
Page 38: Introduction

Images in the Viewerare Bigger than They Appear

If you double click the file castillo.JPG it might show up in a picture viewer like the one on the right.

This is NOT the actual size of the picture.

The true size of the picture is demonstrated in the next program.

Page 39: Introduction

// Java1513.java// Problem #3 -- The image file is too big.// This program will be used to demonstrate what to do when a picture is too big.

import java.awt.*;

public class Java1513 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"castillo.jpg");}

public void paint(Graphics g){

Expo.setFont(g,"Snap ITC",Font.PLAIN,48);Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);g.drawImage(picture,0,100,this);

}}

Page 40: Introduction
Page 41: Introduction

Load the picture in Paint and click Resize.

Page 42: Introduction

Select Pixels and enter 550 for vertical.

Since “Maintain aspect ratio” is selected the Horizontal value will adjust automatically after you change the vertical.

You can also change the picture size by entering a Percentage. 50 percent would make the picture have half the length and half the width.

Page 43: Introduction

Press OK and save the resized picture.

Do NOT close this file! We will use it again soon.

Page 44: Introduction

// Java1513.java// Problem #3 -- The image file is too big.// This program will be used to demonstrate what to do when a picture is too big.

import java.awt.*;

public class Java1513 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"castillo.jpg");}

public void paint(Graphics g){

Expo.setFont(g,"Snap ITC",Font.PLAIN,48);Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);g.drawImage(picture,0,100,this);

}}

Return to JCreator.Execute the program again.No alterations are necessary.

Page 45: Introduction
Page 46: Introduction

Solution #2 – Specify Image Dimensions

Return to Paint.Hit <Control>-<Z> to“Undo” the resizing.Re-save the image.

Page 47: Introduction

// Java1513.java// Problem #3 -- The image file is too big.// This program will be used to demonstrate what to do when a picture is too big.

import java.awt.*;

public class Java1513 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"castillo.jpg");}

public void paint(Graphics g){

Expo.setFont(g,"Snap ITC",Font.PLAIN,48);Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);g.drawImage(picture,0,100,752,500,this);

}}

Return to JCreator. Add the 2 numbers shown below to the drawImage command.Re-compile and execute.The picture will display ¼ its actual size of 3008 by 2000.

Page 48: Introduction

Actual Size: 3008 by 20003008 / 4 = 7522000 / 4 = 500With dimensionsof 752 by 500 the image is ¼ the width and ¼ the height or 1/16 the total area.

Page 49: Introduction

// Java1513.java// Problem #3 -- The image file is too big.// This program will be used to demonstrate what to do when a picture is too big.

import java.awt.*;

public class Java1513 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"castillo.jpg");}

public void paint(Graphics g){

Expo.setFont(g,"Snap ITC",Font.PLAIN,48);Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);g.drawImage(picture,0,100,376,250,this);

}}

Change the 2 numbers in the drawImage command.Re-compile and execute.The picture will display ⅛ its actual size of 3008 by 2000.

Page 50: Introduction

Actual Size: 3008 by 20003008 / 8 = 3762000 / 8 = 250With dimensions of 376 by 250 the image is ⅛ the width and ⅛ the height or 1/64 the total area.

Page 51: Introduction

// Java1513.java// Problem #3 -- The image file is too big.// This program will be used to demonstrate what to do when a picture is too big.

import java.awt.*;

public class Java1513 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"castillo.jpg");}

public void paint(Graphics g){

Expo.setFont(g,"Snap ITC",Font.PLAIN,48);Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);g.drawImage(picture,0,100,1000,250,this);

}}

Change only the 1st number in the drawImage command.Re-compile and execute.What happened to the output?

Page 52: Introduction

Actual Size: Display Size:3008 by 2000 1000 by 250

The display dimensions are not proportional to the original dimensions. The result is that the image gets warped.

Page 53: Introduction

Solution #3 – Crop the Image

Return to Paint.

Page 54: Introduction

Use the scroll bars to find this image:

Page 55: Introduction

Use the Select tool to outline what you want

Page 56: Introduction

Click the Copy icon.

Page 57: Introduction

Click the icon and create a new file.

Page 58: Introduction

Click Paste.

Page 59: Introduction

DO NOT SAVE YET!

All of this extra white space is currently part of the picture.You need to click the bottom right corner and drag it toward the picture.

Page 60: Introduction

Now you can save.

Save this file as castillo.JPG,return to JCreator and executethe program again.

Page 61: Introduction

// Java1513.java// Problem #3 -- The image file is too big.// This program will be used to demonstrate what to do when a picture is too big.

import java.awt.*;

public class Java1513 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"castillo.jpg");}

public void paint(Graphics g){

Expo.setFont(g,"Snap ITC",Font.PLAIN,48);Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);g.drawImage(picture,0,100,this);

}}

Return to JCreator. Remove the extra numbers from the drawImage command so that it will display the image actual size.Re-compile and execute.

Page 62: Introduction
Page 63: Introduction

Graphics EditorsThere are many Graphics Editors, any of which could be used to edit these images.

Some are simplistic like Microsoft Paint and some are more advanced and complicated.

Examples were shown with Microsoft Paint because that software is readily available.

Page 64: Introduction
Page 65: Introduction

// Java1514.java// This program demonstrates 84 of the 85 colors of the Deluxe Expo class// There is no white circle drawn since white is the background color.

// NOTE: The 10 primary colors in the Deluxe Expo class are// red, green, blue, orange, cyan, magenta, yellow, gray, pink and tan.// Each of these colors actually has 5 shades.// Example: The 5 shades of red are red, darkRed, lightRed, darkerRed and lighterRed.// There are also 35 special colors like lime, teal, navy and chartreuse to name a few.

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

public class Java1514 extends Applet{

public void paint(Graphics g){

int radius = 60;Expo.setColor(g,Expo.darkerRed); Expo.fillCircle(g, 60, 60,radius);Expo.setColor(g,Expo.darkRed); Expo.fillCircle(g,140, 60,radius);Expo.setColor(g,Expo.red); Expo.fillCircle(g,220, 60,radius);Expo.setColor(g,Expo.lightRed); Expo.fillCircle(g,300, 60,radius);Expo.setColor(g,Expo.lighterRed); Expo.fillCircle(g,380, 60,radius);Expo.setColor(g,Expo.brown); Expo.fillCircle(g,460, 60,radius);Expo.setColor(g,Expo.violet); Expo.fillCircle(g,540, 60,radius);Expo.setColor(g,Expo.darkerGreen); Expo.fillCircle(g,620, 60,radius);Expo.setColor(g,Expo.darkGreen); Expo.fillCircle(g,700, 60,radius);Expo.setColor(g,Expo.green); Expo.fillCircle(g,780, 60,radius);Expo.setColor(g,Expo.lightGreen); Expo.fillCircle(g,860, 60,radius);Expo.setColor(g,Expo.lighterGreen); Expo.fillCircle(g,940, 60,radius);

Page 66: Introduction

Expo.setColor(g,Expo.darkerBlue); Expo.fillCircle(g, 60,140,radius);Expo.setColor(g,Expo.darkBlue); Expo.fillCircle(g,140,140,radius);Expo.setColor(g,Expo.blue); Expo.fillCircle(g,220,140,radius);Expo.setColor(g,Expo.lightBlue); Expo.fillCircle(g,300,140,radius);Expo.setColor(g,Expo.lighterBlue); Expo.fillCircle(g,380,140,radius);Expo.setColor(g,Expo.purple); Expo.fillCircle(g,460,140,radius);Expo.setColor(g,Expo.turquoise); Expo.fillCircle(g,540,140,radius);Expo.setColor(g,Expo.darkerOrange); Expo.fillCircle(g,620,140,radius);Expo.setColor(g,Expo.darkOrange); Expo.fillCircle(g,700,140,radius);Expo.setColor(g,Expo.orange); Expo.fillCircle(g,780,140,radius);Expo.setColor(g,Expo.lightOrange); Expo.fillCircle(g,860,140,radius);Expo.setColor(g,Expo.lighterOrange); Expo.fillCircle(g,940,140,radius);

Expo.setColor(g,Expo.darkerCyan); Expo.fillCircle(g, 60,220,radius);Expo.setColor(g,Expo.darkCyan); Expo.fillCircle(g,140,220,radius);Expo.setColor(g,Expo.cyan); Expo.fillCircle(g,220,220,radius);Expo.setColor(g,Expo.lightCyan); Expo.fillCircle(g,300,220,radius);Expo.setColor(g,Expo.lighterCyan); Expo.fillCircle(g,380,220,radius);Expo.setColor(g,Expo.plum); Expo.fillCircle(g,460,220,radius);Expo.setColor(g,Expo.indigo); Expo.fillCircle(g,540,220,radius);Expo.setColor(g,Expo.darkerMagenta); Expo.fillCircle(g,620,220,radius);Expo.setColor(g,Expo.darkMagenta); Expo.fillCircle(g,700,220,radius);Expo.setColor(g,Expo.magenta); Expo.fillCircle(g,780,220,radius);Expo.setColor(g,Expo.lightMagenta); Expo.fillCircle(g,860,220,radius);Expo.setColor(g,Expo.lighterMagenta); Expo.fillCircle(g,940,220,radius);

Page 67: Introduction

Expo.setColor(g,Expo.darkerYellow); Expo.fillCircle(g, 60,300,radius);Expo.setColor(g,Expo.darkYellow); Expo.fillCircle(g,140,300,radius);Expo.setColor(g,Expo.yellow); Expo.fillCircle(g,220,300,radius);Expo.setColor(g,Expo.lightYellow); Expo.fillCircle(g,300,300,radius);Expo.setColor(g,Expo.lighterYellow); Expo.fillCircle(g,380,300,radius);Expo.setColor(g,Expo.aqua); Expo.fillCircle(g,460,300,radius);Expo.setColor(g,Expo.aquaMarine); Expo.fillCircle(g,540,300,radius);Expo.setColor(g,Expo.darkerPink); Expo.fillCircle(g,620,300,radius);Expo.setColor(g,Expo.darkPink); Expo.fillCircle(g,700,300,radius);Expo.setColor(g,Expo.pink); Expo.fillCircle(g,780,300,radius);Expo.setColor(g,Expo.lightPink); Expo.fillCircle(g,860,300,radius);Expo.setColor(g,Expo.lighterPink); Expo.fillCircle(g,940,300,radius);

Expo.setColor(g,Expo.goldenRod); Expo.fillCircle(g, 60,380,radius);Expo.setColor(g,Expo.gold); Expo.fillCircle(g,140,380,radius);Expo.setColor(g,Expo.silver); Expo.fillCircle(g,220,380,radius);Expo.setColor(g,Expo.bronze); Expo.fillCircle(g,300,380,radius);Expo.setColor(g,Expo.teal); Expo.fillCircle(g,380,380,radius);Expo.setColor(g,Expo.maroon); Expo.fillCircle(g,460,380,radius);Expo.setColor(g,Expo.fuschia); Expo.fillCircle(g,540,380,radius);Expo.setColor(g,Expo.lavender); Expo.fillCircle(g,620,380,radius);Expo.setColor(g,Expo.lime); Expo.fillCircle(g,700,380,radius);Expo.setColor(g,Expo.navy); Expo.fillCircle(g,780,380,radius);Expo.setColor(g,Expo.chartreuse); Expo.fillCircle(g,860,380,radius);Expo.setColor(g,Expo.fireBrick); Expo.fillCircle(g,940,380,radius);

Page 68: Introduction

Expo.setColor(g,Expo.moccasin); Expo.fillCircle(g, 60,460,radius);Expo.setColor(g,Expo.salmon); Expo.fillCircle(g,140,460,radius);Expo.setColor(g,Expo.olive); Expo.fillCircle(g,220,460,radius);Expo.setColor(g,Expo.khaki); Expo.fillCircle(g,300,460,radius);Expo.setColor(g,Expo.crimson); Expo.fillCircle(g,380,460,radius);Expo.setColor(g,Expo.orchid); Expo.fillCircle(g,460,460,radius);Expo.setColor(g,Expo.sienna); Expo.fillCircle(g,540,460,radius);Expo.setColor(g,Expo.melon); Expo.fillCircle(g,620,460,radius);Expo.setColor(g,Expo.tangerine); Expo.fillCircle(g,700,460,radius);Expo.setColor(g,Expo.terraCotta); Expo.fillCircle(g,780,460,radius);Expo.setColor(g,Expo.pumpkin); Expo.fillCircle(g,860,460,radius);Expo.setColor(g,Expo.mahogany); Expo.fillCircle(g,940,460,radius);

Expo.setColor(g,Expo.darkerTan); Expo.fillCircle(g, 60,540,radius);Expo.setColor(g,Expo.darkTan); Expo.fillCircle(g,140,540,radius);Expo.setColor(g,Expo.tan); Expo.fillCircle(g,220,540,radius);Expo.setColor(g,Expo.lightTan); Expo.fillCircle(g,300,540,radius);Expo.setColor(g,Expo.lighterTan); Expo.fillCircle(g,380,540,radius);Expo.setColor(g,Expo.amber); Expo.fillCircle(g,460,540,radius);Expo.setColor(g,Expo.black); Expo.fillCircle(g,540,540,radius);Expo.setColor(g,Expo.darkerGray); Expo.fillCircle(g,620,540,radius);Expo.setColor(g,Expo.darkGray); Expo.fillCircle(g,700,540,radius);Expo.setColor(g,Expo.gray); Expo.fillCircle(g,780,540,radius);Expo.setColor(g,Expo.lightGray); Expo.fillCircle(g,860,540,radius);Expo.setColor(g,Expo.lighterGray); Expo.fillCircle(g,940,540,radius);

}}

Page 69: Introduction
Page 70: Introduction

The drawRoundedRectangle MethodExpo.drawRoundedRectangle(g, x1, y1, x2, y2, pixels);Draws a rectangle with a top-left corner at coordinate (x1,y1) and a bottom-right hand corner of (x2,y2). Pixels determines how curved the corners will be.

x1, y1

x2, y2

pixels

Page 71: Introduction

The drawSpiral MethodExpo.drawSpiral(g, centerX, centerY, radius);

The parameters for Expo.drawSpiral are identical to Expo.drawCircle.

centerX, centerY

radius

Page 72: Introduction

The drawBurst MethodExpo.drawBurst(g, centerX, centerY, radius, numLines);The first 4 parameters for Expo.drawBurst are the same as Expo.drawCircle.The last parameter indicates the number of lines.

centerX, centerY

radius

Page 73: Introduction

The drawRandomBurst MethodExpo.drawRandomBurst(g, centerX, centerY, radius, numLines);The first 4 parameters for Expo.drawRandomBurst are the same as Expo.drawCircle.The last parameter indicates the number of random lines.

centerX, centerY

radius

Page 74: Introduction

// Java1515.java// This program demonstrates the drawSpiral, drawBurst and drawRandomBurst // methods of the Deluxe Expo class.// All three of these methods share the same parameters as drawCircle.// The two "Burst" methods have an additional parameter for the number of lines.// It also demonstrates the drawRoundedRectangle method of the Deluxe Expo class.// It shares the same parameters as drawRectangle, except there is an extra parameter// to control how "rounded" the corners will be.

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

public class Java1515 extends Applet{

public void paint(Graphics g){

Expo.drawRoundedRectangle(g,10,20,990,300,25);Expo.drawSpiral(g,180,160,100);Expo.drawBurst(g,500,160,100,8);Expo.drawRandomBurst(g,820,160,100,300);

Expo.drawRoundedRectangle(g,10,320,990,640,100);Expo.drawSpiral(g,180,480,150);Expo.drawBurst(g,500,480,150,40);Expo.drawRandomBurst(g,820,480,150,5000);

}}

Page 75: Introduction
Page 76: Introduction

// Java1516.java// This program demonstrates the "drawThick" methods of the Deluxe Expo class.// All "draw" methods in the Deluxe Expo class have a corresponding "drawThick"// method with the same parameters, except that the "drawThick" methods have // an extra parameter to control the "thickness".

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

public class Java1516 extends Applet{

public void paint(Graphics g){

int t = 12; // thicknessExpo.setColor(g,Expo.blue);Expo.drawThickLine(g,100,80,900,80,t);Expo.drawThickRectangle(g,50,30,950,620,t);Expo.drawThickCircle(g,500,310,200,t);Expo.drawThickOval(g,400,300,30,40,t);Expo.drawThickStar(g,400,300,100,10,t);Expo.drawThickOval(g,600,300,30,40,t);Expo.drawThickStar(g,600,300,90,6,t);Expo.drawThickArc(g,500,415,105,65,90,270,t);

Page 77: Introduction

Expo.drawThickRoundedRectangle(g,100,100,200,600,25,t);Expo.drawThickRoundedRectangle(g,800,100,900,500,50,t);Expo.drawThickSpiral(g,280,115,100,t);Expo.drawThickSpiral(g,700,115,100,t);Expo.setColor(g,Expo.red);Expo.drawThickBurst(g,150,350,150,20,t);Expo.drawThickRandomBurst(g,850,350,150,300,t);Expo.setColor(g,Expo.darkGreen);Expo.drawThickRegularPolygon(g,150,555,40,3,t);Expo.drawThickRegularPolygon(g,250,555,40,4,t);Expo.drawThickRegularPolygon(g,350,555,40,5,t);Expo.drawThickRegularPolygon(g,450,555,40,6,t);Expo.drawThickRegularPolygon(g,550,555,40,7,t);Expo.drawThickRegularPolygon(g,650,555,40,8,t);Expo.drawThickRegularPolygon(g,750,555,40,9,t);Expo.drawThickRegularPolygon(g,850,555,40,10,t);

}}

Page 78: Introduction
Page 79: Introduction

// Java1517.java// This program shows how to have user input in a graphics program with the// enterIntGUI and enterStringGUI methods of the Deluxe Expo class.// Methods enterDoubleGUI and enterCharGUI are also available.import java.awt.*;public class Java1517 extends java.applet.Applet{

int amount;String imageFile;Image picture;public void init(){

String imageFile = Expo.enterStringGUI("What image do you wish to display?");picture = getImage(getDocumentBase(), imageFile);amount =

Expo.enterIntGUI("How many times do you wish to display this image?");}public void paint(Graphics g){

for (int j = 1; j <=amount; j++){

int x = Expo.random(0,900);int y = Expo.random(0,550);g.drawImage(picture,x,y,this);

}}

}

Page 80: Introduction

Java1517.java Input

Page 81: Introduction
Page 82: Introduction

As before, you are provided with the file Expo.html which documents all of the methods in the Deluxe Expo class (both old and new).

Page 83: Introduction
Page 84: Introduction
Page 85: Introduction
Page 86: Introduction
Page 87: Introduction
Page 88: Introduction
Page 89: Introduction
Page 90: Introduction
Page 91: Introduction
Page 92: Introduction
Page 93: Introduction
Page 94: Introduction
Page 95: Introduction
Page 96: Introduction
Page 97: Introduction
Page 98: Introduction
Page 99: Introduction
Page 100: Introduction
Page 101: Introduction
Page 102: Introduction

// Java1518.java This program shows how to display multiple pages of graphics output.import java.awt.*;public class Java1518 extends java.applet.Applet{

int numClicks;Image picture1, picture2, picture3, picture4, picture5;

public void init(){

numClicks = 0;picture1 = getImage(getDocumentBase(),"LSchram.gif");picture2 = getImage(getDocumentBase(),"JSchram.jpg");picture3 = getImage(getDocumentBase(),"nederlands.gif");picture4 = getImage(getDocumentBase(),"leon&isolde.jpg");picture5 = getImage(getDocumentBase(),"JPIILogo.jpg");

// The following MediaTracker/try/catch code ensures that// all images are loaded before the program continues.MediaTracker tracker = new MediaTracker(this);tracker.addImage(picture1,1); tracker.addImage(picture2,1);tracker.addImage(picture3,1); tracker.addImage(picture4,1);tracker.addImage(picture5,1);try{

tracker.waitForAll();}catch(InterruptedException e){

System.out.println(e);}

}

Page 103: Introduction

public void paint(Graphics g){

switch (numClicks){

case 0 : page1(g); break;case 1 : page2(g); break;case 2 : page3(g); break;

}}

public boolean mouseDown(Event e, int x, int y){

numClicks++;repaint();return true;

}

Page 104: Introduction

public void page1(Graphics g){

Expo.setFont(g,"Arial",Font.BOLD,100);Expo.drawString(g,"TITLE PAGE",218,100);Expo.setColor(g,Expo.yellow);Expo.drawThickPreciseSpiral(g,510,370,275,50,15);Expo.setColor(g,Expo.red);Expo.fillStar(g,510,370,175,8);Expo.setColor(g,Expo.green);Expo.drawThickStar(g,510,370,225,8,25);Expo.setColor(g,Expo.blue);Expo.drawThickStar(g,510,370,275,8,25);g.drawImage(picture1,425,285,this);Expo.setFont(g,"Times Roman",Font.PLAIN,20);Expo.drawString(g,"My name is Leon Schram.",30,300);Expo.drawString(g,"Click once to continue.",760,550);

}

Page 105: Introduction

public void page2(Graphics g){

Expo.setFont(g,"Algerian",Font.BOLD,100);Expo.drawString(g,"PAGE 2",200,100);Expo.setColor(g,Expo.blue);Expo.fillRectangle(g,100,250,900,500);Expo.setColor(g,Expo.chartreuse);g.drawImage(picture3,200,300,this);g.drawImage(picture4,500,280,this);Expo.setColor(g,Expo.blue);Expo.setFont(g,"Times Roman",Font.PLAIN,20);Expo.drawString(g,"I was born in the Netherlands in

1945 and married my sweet wife Isolde in 1967.",100,200);Expo.drawString(g,"Click once to continue.",750,550);

Page 106: Introduction

 public void page3(Graphics g){

Expo.setFont(g,"Impact",Font.BOLD,100);Expo.drawString(g,"PAGE 3",200,100);Expo.setColor(g,Expo.darkGreen);Expo.fillRoundedRectangle(g,460,170,960,515,50);g.drawImage(picture5,150,225,this);g.drawImage(picture2,500,193,this);Expo.setFont(g,"Times Roman",Font.PLAIN,20);Expo.drawString(g,"I teach Computer Science at John

Paul II High School with my son John.",100,150);Expo.drawString(g,"Click once to continue.",750,550);

}}

Page 107: Introduction
Page 108: Introduction
Page 109: Introduction
Page 110: Introduction

End of Part 1Program Java1518.java concludes the Part 1 section which prepares you for the Personal Slide Show project.

See the GraphicsLab09.docx file for assignment details.

Page 111: Introduction
Page 112: Introduction

Beginning of Part 2The remaining programs are to help prepare you for the Final Graphics Project. This will be the biggest assignment of the year.

Page 113: Introduction

// Java1519.java// This program draws a small square at every mouse click position.// There are 2 problems with the program:// First, it draws a square in the upper left hand corner automatically before we have a// chance to do anything. Second, it will only draws one square at a time.

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

public class Java1519 extends Applet{

int xCoord;int yCoord;

public void paint(Graphics g){

Expo.setColor(g,Expo.red);Expo.fillRectangle(g,xCoord,yCoord,xCoord+15,yCoord+15);

}

public boolean mouseDown(Event e, int x, int y){

xCoord = x; yCoord = y;repaint();return true;

}}

Page 114: Introduction

// Java1520.java// This program cures the problem of the initial red square by adding a "firstPaint" boolean variable.import java.applet.Applet;import java.awt.*;public class Java1520 extends Applet{

int xCoord;int yCoord;boolean firstPaint;public void init() { firstPaint = true; }public void paint(Graphics g){

if (firstPaint)firstPaint = false;

else{

Expo.setColor(g,Expo.red);Expo.fillRectangle(g,xCoord,yCoord,xCoord+15,yCoord+15);

}}public boolean mouseDown(Event e, int x, int y){

xCoord = x;yCoord = y;repaint();return true;

}}

Page 115: Introduction
Page 116: Introduction

// Java1521.java// This program adds the clever "update" method which prevents the computer from clearing the // paint window every time it is refreshed. This allows you to draw more than one square at a time.import java.applet.Applet;import java.awt.*;public class Java1521 extends Applet{

int xCoord;int yCoord;boolean firstPaint;public void init() { firstPaint = true; }public void paint(Graphics g){

if (firstPaint)firstPaint = false;

else{

Expo.setColor(g,Expo.red);Expo.fillRectangle(g,xCoord,yCoord,xCoord+15,yCoord+15);

}}public boolean mouseDown(Event e, int x, int y){

xCoord = x; yCoord = y;repaint();return true;

}

public void update(Graphics g) { paint(g); }}

Page 117: Introduction

// Java1522.java// This program changes "mouseDown" to "mouseDrag" and makes the square smaller.// This should create the effect of having a freehand pencil.

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

public class Java1522 extends Applet{

int xCoord,yCoord;

public void init(){

xCoord = 0;yCoord = 0;

}

public void paint(Graphics g){

Expo.setColor(g,Expo.red);Expo.drawPoint(g,xCoord,yCoord);

}

public boolean mouseDrag(Event e, int x, int y){

xCoord = x;yCoord = y;repaint();return true;

}

public void update(Graphics g) { paint(g); }}

Page 118: Introduction
Page 119: Introduction

// Java1523.java// This program draws a straight line from the point where the mouse is clicked// to the point where the mouse is released.

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

public class Java1523 extends Applet{

int startX,startY,endX,endY;

public void paint(Graphics g){

Expo.drawLine(g,startX,startY,endX,endY);}

public boolean mouseDown(Event e, int x, int y){

startX = x;startY = y;return true;

}

public boolean mouseUp(Event e, int x, int y){

endX = x;endY = y;repaint();return true;

}}

Page 120: Introduction

// Java1524.java// This program draws a straight line from the point where the mouse is clicked// to the point where the mouse is released. In this example the line is // constantly visible. This is sometimes called the "rubber band" effect.import java.applet.Applet;import java.awt.*;public class Java1524 extends Applet{

int startX,startY,endX,endY;

public void paint(Graphics g){

Expo.drawLine(g,startX,startY,endX,endY);}

public boolean mouseDown(Event e, int x, int y){

startX = x;startY = y;return true;

}

public boolean mouseDrag(Event e, int x, int y){

endX = x;endY = y;repaint();return true;

}}

Page 121: Introduction

// Java1525.java// This program shows that method "update" can causes a peculiar "side effect" in some cases.// Watch what happens when you try to draw a line.

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

public class Java1525 extends Applet{

int startX,startY,endX,endY;

public void paint(Graphics g){

Expo.drawLine(g,startX,startY,endX,endY);}

public boolean mouseDown(Event e, int x, int y){

startX = x; startY = y;return true;

}

public boolean mouseDrag(Event e, int x, int y){

endX = x; endY = y;repaint();return true;

}

public void update(Graphics g) { paint(g); }}

Page 122: Introduction
Page 123: Introduction

// Java1526.java// This program is almost identical to Java1524.java.// The only difference is that "drawLine" has been changed to "drawRectangle".// Note how the rectangles are draw with the same "rubber band effect" as the lines.

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

public class Java1526 extends Applet{

int startX,startY,endX,endY;

public void paint(Graphics g){

Expo.drawRectangle(g,startX,startY,endX,endY);}

public boolean mouseDown(Event e, int x, int y){

startX = x;startY = y;return true;

}

public boolean mouseDrag(Event e, int x, int y){

endX = x;endY = y;repaint();return true;

}}

Page 124: Introduction
Page 125: Introduction

Drawing CirclesDrawing circles is more complicated than drawing lines or rectangles.

A circles has a center and a radius.

mouseDown determines the center.

mouseUp determines a point on the rim of the circle.

To find the radius we need to find the distance between these 2 points using the distance formula which is based on the Pythagorean Theorem.

Page 126: Introduction

Pythagorean TheoremThis Theorem states that in a right triangle, the square of the hypotenuse (longest side) is equal to thesum of the squares of the a c2 shorter sides.

So c2 = a2 + b2 b

The actual formula for finding the hypotenuse is:

Page 127: Introduction

Distance FormulaThe distance between 2 points in a coordinate plane is essentially the hypotenuse of the right triangle they form.

(x2,y2)

(x1, y1)

(x2, y2)

Page 128: Introduction

// Java1527.java// This program will draw circles using a special "getRadius" method// which is based on the Pythagorean Formula.import java.applet.Applet;import java.awt.*;public class Java1527 extends Applet{

int centerX,centerY,rimX,rimY;public void paint(Graphics g){

int radius = getRadius(centerX,centerY,rimX,rimY);Expo.drawCircle(g,centerX,centerY,radius);

}public boolean mouseDown(Event e, int x, int y){

centerX = x; centerY = y;return true;

}public boolean mouseDrag(Event e, int x, int y){

rimX = x; rimY = y;repaint();return true;

}public int getRadius(int x1, int y1, int x2, int y2){

double radius = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));return (int) radius;

}}

Page 129: Introduction
Page 130: Introduction

// Java1528.java// This uses the <Rectangle> class with the // <inside> method to determine if a certain // square has been clicked.import java.applet.Applet;import java.awt.*;public class Java1528 extends Applet{

Rectangle red, green, blue;int numColor;

public void init(){

red = new Rectangle(100,75,150,150);

green = new Rectangle(100,275,150,150);

blue = new Rectangle(100,475,150,150);

numColor = 0;}

public boolean mouseDown(Event e, int x, int y)

{if(red.inside(x,y))

numColor = 1;else if(green.inside(x,y))

numColor = 2;else if(blue.inside(x,y))

numColor = 3;else

numColor = 4;repaint();return true;

}

public void paint(Graphics g){

Expo.setColor(g,Expo.red);Expo.fillRectangle(g,100,75,250,225);Expo.setColor(g,Expo.green);Expo.fillRectangle(g,100,275,250,425);Expo.setColor(g,Expo.blue);Expo.fillRectangle(g,100,475,250,625);Expo.setFont(g,"Arial",Font.BOLD,28);switch (numColor){

case 1:Expo.setColor(g,Expo.red);g.drawString("Mouse

clicked inside red",300,150);break;

case 2:

Expo.setColor(g,Expo.green);g.drawString("Mouse

clicked inside green",300,350);break;

case 3:

Expo.setColor(g,Expo.blue);g.drawString("Mouse

clicked inside blue",300,550);break;

case 4:

Expo.setColor(g,Expo.black);g.drawString( "Mouse clicked outside

the colored squares",300,40);break;

}} }

Page 131: Introduction
Page 132: Introduction
Page 133: Introduction
Page 134: Introduction
Page 135: Introduction
Page 136: Introduction

// Java1529.java// This program proves that objects of the <Rectangle> class are abstract to the viewer and// not visible. The three square are intentionally not displayed. The program still works// like the previous program, but you must guess at the location of the squares.

import java.applet.Applet;import java.awt.*;public class Java1529 extends Applet{

Rectangle red, green, blue;int numColor;public void init(){

red = new Rectangle(100,75,150,150);

green = new Rectangle(100,275,150,150);

blue = new Rectangle(100,475,150,150);

numColor = 0;}public boolean mouseDown(Event e,

int x, int y){

if(red.inside(x,y))numColor = 1;

else if(green.inside(x,y))numColor = 2;

else if(blue.inside(x,y))numColor = 3;

elsenumColor = 4;

repaint();return true;

}

public void paint(Graphics g){

// Note: The solid colored squares are not drawn this time.

Expo.setFont(g,"Arial",Font.BOLD,28);switch (numColor){

case 1:Expo.setColor(g,Expo.red);g.drawString("Mouse

clicked inside red",300,150);break;

case 2:

Expo.setColor(g,Expo.green);g.drawString("Mouse

clicked inside green",300,350);break;

case 3:

Expo.setColor(g,Expo.blue);g.drawString("Mouse

clicked inside blue",300,550);break;

case 4:

Expo.setColor(g,Expo.black);g.drawString( "Mouse clicked outside

the colored squares",300,40);break;

}}}

Page 137: Introduction
Page 138: Introduction
Page 139: Introduction
Page 140: Introduction
Page 141: Introduction
Page 142: Introduction

Difference BetweenDrawing Rectangles and

Creating Rectangle ObjectsExpo.drawRectangle(g,x1,y1,x2,y2); Rectangle area = new Rectangle(x,y,width,height);

Page 143: Introduction
Page 144: Introduction

// Java1530.java Animation Part 1// This program simply draws a solid black circle.// This will eventually be "animated" as we go through // the next few programs. import java.awt.*; public class Java1530 extends java.applet.Applet{

public void paint(Graphics g){

Expo.fillCircle(g,50,325,25);}

}

Page 145: Introduction

// Java1531.java Animation Part 2// This program simply draws the solid black circle at 10 different locations.// This is not yet animation because we see all 10 circles at the same time. import java.awt.*; public class Java1531 extends java.applet.Applet{

public void paint(Graphics g){

Expo.fillCircle(g,50,325,25);Expo.fillCircle(g,150,325,25);Expo.fillCircle(g,250,325,25);Expo.fillCircle(g,350,325,25);Expo.fillCircle(g,450,325,25);Expo.fillCircle(g,550,325,25);Expo.fillCircle(g,650,325,25);Expo.fillCircle(g,750,325,25);Expo.fillCircle(g,850,325,25);Expo.fillCircle(g,950,325,25);

}}

Page 146: Introduction

// Java1532.java Animation Part 3// This program erases every circle before it draws the next one.// This is necessary for animation; however, the process happens // so fast you cannot see the circle move. import java.awt.*; public class Java1532 extends java.applet.Applet{

public void paint(Graphics g){

Expo.setColor(g,Expo.black);Expo.fillCircle(g,50,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,50,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,150,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,150,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,250,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,250,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,350,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,350,325,25);

 

Expo.setColor(g,Expo.black);Expo.fillCircle(g,450,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,450,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,550,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,550,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,650,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,650,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,750,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,750,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,850,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,850,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,950,325,25);Expo.setColor(g,Expo.white);Expo.fillCircle(g,950,325,25);

}}

Page 147: Introduction
Page 148: Introduction

// Java1533.java Animation Part 4// This program adds a short 1 second delay after each circle is drawn// to give you a chance to see it before it is erased. import java.awt.*; public class Java1533 extends java.applet.Applet{

public void paint(Graphics g){

Expo.setColor(g,Expo.black);Expo.fillCircle(g,50,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,50,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,150,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,150,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,250,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,250,325,25);

Expo.setColor(g,Expo.black);Expo.fillCircle(g,350,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,350,325,25);

Expo.setColor(g,Expo.black);Expo.fillCircle(g,450,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,450,325,25); Expo.setColor(g,Expo.black);Expo.fillCircle(g,550,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,550,325,25);

Expo.setColor(g,Expo.black);Expo.fillCircle(g,650,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,650,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,750,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,750,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,850,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,850,325,25);

 Expo.setColor(g,Expo.black);Expo.fillCircle(g,950,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,950,325,25);

}}

Page 149: Introduction
Page 150: Introduction
Page 151: Introduction
Page 152: Introduction
Page 153: Introduction
Page 154: Introduction
Page 155: Introduction
Page 156: Introduction
Page 157: Introduction
Page 158: Introduction
Page 159: Introduction

Program NoteThe output of program Java1533.java was simulated with PowerPoint.

It will not be possible to simulate the output of the remaining programs.

You need to execute them on your own computer to truly see what they are doing.

Page 160: Introduction

// Java1534.java Animation Part 5// This program has the same output as the previous program.// The program is now much shorter since a for loop is used. import java.awt.*; public class Java1534 extends java.applet.Applet{

public void paint(Graphics g){

for (int x = 50; x <= 950; x+=100){

Expo.setColor(g,Expo.black);Expo.fillCircle(g,x,325,25);Expo.delay(1000);Expo.setColor(g,Expo.white);Expo.fillCircle(g,x,325,25);

}}

}

Page 161: Introduction

// Java1535.java Animation Part 6// This program makes the animation smoother by using a smaller delay// and a smaller increment in the for loop. import java.awt.*; public class Java1535 extends java.applet.Applet{

public void paint(Graphics g){

for (int x = 50; x <= 950; x+=10){

Expo.setColor(g,Expo.black);Expo.fillCircle(g,x,325,25);Expo.delay(100);Expo.setColor(g,Expo.white);Expo.fillCircle(g,x,325,25);

}}

}

Page 162: Introduction

// Java1536.java Animation Part 7// This program makes the animation as smooth as possible by having an // increment of just 1 pixel in the for loop. The delay is also made smaller. import java.awt.*; public class Java1536 extends java.applet.Applet{

public void paint(Graphics g){

for (int x = 50; x <= 950; x+=1){

Expo.setColor(g,Expo.black);Expo.fillCircle(g,x,325,25);Expo.delay(10);Expo.setColor(g,Expo.white);Expo.fillCircle(g,x,325,25);

}}

}

Page 163: Introduction

Animation ReviewTo achieve animation, you need to dothe following:

1) Draw something

2) Delay for a fraction of a second

3) Erase it by drawing over it in the background color.

4) Redraw it in a slightly different location and repeat.

Page 164: Introduction

// Java1537.java Animation Part 8// This program draws and erases 3 circles instead of one to animate a snowman across the screen.// The flickering that you will see is normal for this type of animation.// In AP Computer Science you will learn a more advanced type of animation that eliminates the flicker. import java.awt.*; public class Java1537 extends java.applet.Applet{

public void paint(Graphics g){

for (int x = 50; x <= 950; x+=1){

Expo.setColor(g,Expo.black);Expo.fillCircle(g,x,290,15);Expo.fillCircle(g,x,325,25);Expo.fillCircle(g,x,380,40);Expo.delay(10);Expo.setColor(g,Expo.white);Expo.fillCircle(g,x,290,15);Expo.fillCircle(g,x,325,25);Expo.fillCircle(g,x,380,40);

}}

}

Page 165: Introduction

// Java1538.java Animation Part 9// This program shows that an image from a file can also be made to move across the screen.// As with the snowman, there can be considerable flickering.

import java.awt.*;

public class Java1538 extends java.applet.Applet{

Image picture;

public void init(){

picture = getImage(getDocumentBase(),"bunny.png");}

public void paint(Graphics g){

for (int x = -100; x <= 1100; x+=3){

g.drawImage(picture,x,200,this);Expo.delay(30);Expo.setColor(g,Expo.white);Expo.fillRectangle(g,x,200,x+100,300);

}}

}

Page 166: Introduction

// Java1539.java Animation Part 10// This program demonstrates the problem that occurs with a multi-colored background.import java.awt.*;public class Java1539 extends java.applet.Applet{

public void paint(Graphics g){

for (int c = 1; c <= 9; c++){

Expo.setColor(g,c);Expo.fillRectangle(g, (c-1)*111, 0, c*111, 650);

}

for (int x = 50; x <= 950; x+=3){

Expo.setColor(g,Expo.black);Expo.fillCircle(g,x,290,15);Expo.fillCircle(g,x,325,25);Expo.fillCircle(g,x,380,40);Expo.delay(30);Expo.setColor(g,Expo.red);Expo.fillCircle(g,x,290,15);Expo.fillCircle(g,x,325,25);Expo.fillCircle(g,x,380,40);

}}

}

Page 167: Introduction

// Java1540.java Animation Part 11// This program shows how to handle a multi-colored background.// Instead of "draw-and-erase", it is "draw-and-redraw".// The entire background is redrawn every time the object moves.// NOTE: This does have the potential for serious flickering.

import java.awt.*;

public class Java1540 extends java.applet.Applet{

public void paint(Graphics g){

for (int x = 50; x <= 950; x+=3){

for (int c = 1; c <= 9; c++){

Expo.setColor(g,c);Expo.fillRectangle(g, (c-1)*111, 0, c*111, 650);

}Expo.setColor(g,Expo.black);Expo.fillCircle(g,x,290,15);Expo.fillCircle(g,x,325,25);Expo.fillCircle(g,x,380,40);Expo.delay(30);

}}

}

Page 168: Introduction
Page 169: Introduction

Programming KnowledgeNever Becomes Obsolete

Some people may be concerned that with technology changing so rapidly, the programming knowledge that they have acquired in this course will soon be out of date. While new programming languages pop up from time to time, the logic of programming has never changed.

The next three programs all do the exact same thing. The first is written in Java. The second in C++ and the third in Pascal. Even though you may have never seen C++ or Pascal, you may be surprised how familiar these programs will look to you now that you know Java.

Page 170: Introduction

// Java1541.java// This program will count backwards from 20 to 0 and displays which numbers// are even and which are odd.// Compare this Java code to the code from other languages.

public class Java1541{

public static void main (String args[]){

System.out.println("\nJAVA1541.JAVA\n\n");for (int j = 20; j >= 0; j--){

if (j % 2 == 0){

System.out.println(j + " is an even number.");}else{

System.out.println(j + " is an odd number.");}

}System.out.println();

}}

Page 171: Introduction

// CPlusPlus.CPP// This program will count backwards from 20 to 0 and displays which numbers// are even and which are odd.// Compare this C++ code to the code from other languages.

#include <iostream.h>#include <conio.h>

void main(){ cout << endl << "CPlusPlus.CPP" << endl << endl; for (int j = 20; j >= 0; j--) {

if (j % 2 == 0){

cout << j << " is an even number." << endl;}else{

cout << j << " is an odd number." << endl;}

} cout << endl;

getch(); // makes the computer wait until a character is pressed}

Page 172: Introduction

{ Pascal.PAS This program will count backwards from 20 to 0 and displays which numbers are even and which are odd. Compare this Pascal code to the code from other languages. }

PROGRAM My_Pascal_Program;

VAR J : INTEGER; (* Loop Counter *)

BEGIN WRITELN; WRITELN('Pascal.PAS'); WRITELN; FOR J := 20 DOWNTO 0 DO BEGIN IF J MOD 2 = 0 THEN BEGIN WRITELN(J,' is an even number.') END ELSE BEGIN WRITELN(J,' is an odd number.') END END END.