Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda For Feb 26 2. PowerPoint Presentation on Java Methods. 3. Finish Happy Face Assignment (Due by the.

Similar presentations


Presentation on theme: "Agenda For Feb 26 2. PowerPoint Presentation on Java Methods. 3. Finish Happy Face Assignment (Due by the."— Presentation transcript:

1 Agenda For Feb 26 2. PowerPoint Presentation on Java Methods. http://members.shaw.ca/rzamar/IT11/Methods.ppt 3. Finish Happy Face Assignment (Due by the end of class). 4.Create a new Applet that displays a drawing of a house. Use at least 3 methods to draw this house. The structure of your code will be just as important as the final product. Your house must have:  At least two windows  A door (with a handle)  A chimney  A proof 1. Fire Drill Protocol.

2 General Structure Of A Method // Method: drawHouse (name of your method goes here). // Purpose: Uses the Graphics variable to draw a house public void drawHouse (Graphics screen) { // set the drawing color to blue screen.setColor(Color.blue); // draw the frame of the house screen.drawRect (50, 150, 100, 70); // and so on … } Place your Java methods underneath the paint method.

3 Methods Methods allow you to group a set of instructions together (when it makes sense to do so). Methods also help you organize your programs so that they are easier to read and debug. By giving my Java methods good descriptive names, anyone reading my program would be able to immediately understand what my program was doing.

4 Example of User Defined Java Methods public void drawWindows (Graphics screen) { // drawing instruction go here } public void drawFrame (Graphics screen) { // drawing instruction go here } public void drawChimney (Graphics screen) { // drawing instruction go here }

5 This Is How We Can Use Our Java Methods When we want the computer to execute the instructions inside one of our methods we simple call our method from within the Java paint function. Note we have to pass our method the screen variable (Remember our method takes the Graphics variable as an input). // paint method public void paint(Graphics screen) { // call the method that draws the frame of the house drawFrame(screen); // call the method that draws the windows drawWindow(screen); // call the method that draws the chimney drawChimney(screen); }

6 public class myApplet extends Applet { public void init() { } public void paint (Graphics screen) { drawFrame (screen);// executes method drawFrame drawWindow (screen);// executes method drawWindow drawChimney (screen);// executes method drawChimney } public void drawFrame (Graphics screen) { screen.setColor(Color.blue); screen.drawRect (50, 150, 100, 70); // and so on … } public void drawWindow (Graphics screen) { // drawing instructions go here … } }

7 Voila !


Download ppt "Agenda For Feb 26 2. PowerPoint Presentation on Java Methods. 3. Finish Happy Face Assignment (Due by the."

Similar presentations


Ads by Google