Review of Math Class Methods abssqrt minmax powround ceilfloor.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Review of Math Class Methods abssqrt minmax powround ceilfloor.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
PreAP Computer Science Quiz
Visual Classes 1 Class: Bug 5 Objects: All Bug Objects.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
OOP&M - laboratory lectures1 OOP&M – LAB3 LABtres: drawing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
This is an optional chapter in the AP Computer Science curriculum. The graphics concepts in this chapter and future input/output chapters will not be.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
PSAT Club Math – Multiple Choice.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
CS1101: Programming Methodology Aaron Tan.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original (non-OOP)
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Review of Math Class Methods abssqrt minmax powround ceilfloor.
// Java0601.java // This program demonstrates that the methods of a class are not always // accessible, like they were with the class. In this case an.
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
Exposure Java-A 2006 Chapter 4 Slides Using Methods and Parameters
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Chapter 4: Control Structures II
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Review of Math Class Methods abssqrt minmax powround ceilfloor.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Visual Classes 1 Class: Bug 5 Objects: All Bug Objects.
The Math Class Methods Utilizing the Important Math Operations of Java!
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
Creating a Java Application and Applet
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Java Keywords Java has special keywords that have meaning in Java. You have already seen a fair amount of keywords. Examples are: public, main, System,
Output Programs These slides will present a variety of small programs. Most of the programs deal with DecimalFormat objects or Polygon objects. Our concern.
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
PowerPoint Presentation From Materials Created
Section 6.2 Classes & Objects.
Section 8.7 The Consequences of Scope.
Graphics -- Introduction
PowerPoint Presentation Authors of Exposure Java
Section 9.1 Introduction.
Section 6.2 Classes & Objects.
PowerPoint Presentation Authors of Exposure Java
Presentation transcript:

Review of Math Class Methods abssqrt minmax powround ceilfloor

Review of Math Class Attributes PIE Circumference of a Circle / its Diameter About Base of the Natural Log About

Visual Classes 1 Class: Bug 5 Objects: All Bug Objects

Visual Classes 2 Classes: Bug Rock 8 Objects: 5 Bug Objects 3 Rock Objects

Visual Classes 4 Classes: Bug Flower Rock Actor 4 Objects: 1 Bug Object 1 Flower Object 1 Rock Object 1 Actor Object

Visual Classes 4 Classes: Bug Flower Rock Actor 12 Objects: 3 Bug Objects 3 Flower Objects 3 Rock Objects 3 Actor Objects

// Java0601.java // This program demonstrates that the methods of a class are not always // accessible, like they were with the class. In this case an // attempt is made to use methods of the class without success. public class Java0601 { public static void main (String args[]) { System.out.println("\nJAVA0601.JAVA\n"); Bank.checkingDeposit(1000.0); Bank.savingsDeposit(5000.0); System.out.println("Checking balance: " + Bank.getChecking() ); System.out.println("Savings balance: " + Bank.getSavings() ); System.out.println(); }

// Java0602.java // This program creates two Bank objects, called tom and sue. // Each object stores its own Bank information. public class Java0602 { public static void main (String args[]) { System.out.println("\nJAVA0602.JAVA\n"); Bank tom; tom = new Bank(); Bank sue; sue = new Bank(); tom.checkingDeposit(1000.0); tom.savingsDeposit(5000.0); sue.checkingDeposit(1500.0); sue.savingsDeposit (4000.0); System.out.println("Tom's checking balance: " + tom.getChecking()); System.out.println("Tom's savings balance: " + tom.getSavings()); System.out.println("Sue's checking balance: " + sue.getChecking()); System.out.println("Sue's savings balance: " + sue.getSavings()); System.out.println(); } JAVA0602.JAVA Tom's checking balance: Tom's savings balance: Sue's checking balance: Sue's savings balance:

Data Types & Variables vs. Classes & Objects You have seen program statements like: In this case, int is the data type and x is the variable. The previous program had this statement: In this case, Bank is the data type and tom is the variable ; however, Bank is a special data type called a class, and tom is a special variable called an object. int x; Bank tom;

// Java0603.java This program demonstrates how an object can be constructed with a specified // initial balance in checking and savings. Most Java classes have multiple // constructors to create objects for multiple situations. public class Java0603 { public static void main (String args[]) { System.out.println("\nJAVA0603.JAVA\n"); Bank tom;tom = new Bank(5000.0, ); Bank sue;sue = new Bank(3000.0, ); System.out.println("Tom's checking balance: " + tom.getChecking()); System.out.println("Tom's savings balance: " + tom.getSavings()); System.out.println("Sue's checking balance: " + sue.getChecking()); System.out.println("Sue's savings balance: " + sue.getSavings()); System.out.println(); System.out.println("Tom makes a $ checking withdrawal"); tom.checkingDeposit(1000.0); System.out.println("Tom makes a $ savings withdrawal"); tom.savingsDeposit(2000.0); System.out.println("Sue makes a $ checking deposit"); sue.checkingDeposit(1500.0); System.out.println("Sue makes a $ savings deposit"); sue.savingsDeposit(3000.0); System.out.println(); System.out.println("Tom's checking balance: " + tom.getChecking()); System.out.println("Tom's savings balance: " + tom.getSavings()); System.out.println("Sue's checking balance: " + sue.getChecking()); System.out.println("Sue's savings balance: " + sue.getSavings()); System.out.println(); } JAVA0603.JAVA Tom's checking balance: Tom's savings balance: Sue's checking balance: Sue's savings balance: Tom makes a $ checking deposit Tom makes a $ savings deposit Sue makes a $ checking deposit Sue makes a $ savings deposit Tom's checking balance: Tom's savings balance: Sue's checking balance: Sue's savings balance:

The Preferred Way Let us think back to Chapter 3. You were shown statements that define and initialize variables like: But then, you were shown that it is better to define and initialize your variables in one statement like this: The same thing applies to creating objects. The previous program first declared an object, and then constructed it, with 2 separate statements: As before, it is better to combine these 2 statements into one. We can declare and construct the object in one statement like this: int x; x = 5; int x = 5; Bank tom; tom = new Bank(5000.0, ); Bank tom = new Bank(5000.0, );

// Java0604.java // This program demonstrates how an object can be declared and defined all in one statement. // It also shows the checkingWithdrawal and savingsWithdrawal method. public class Java0604 { public static void main (String args[]) { System.out.println("\nJAVA0604.JAVA\n"); Bank tom = new Bank(5000.0, ); Bank sue = new Bank(3000.0, ); System.out.println("Tom's checking balance: " + tom.getChecking()); System.out.println("Tom's savings balance: " + tom.getSavings()); System.out.println("Sue's checking balance: " + sue.getChecking()); System.out.println("Sue's savings balance: " + sue.getSavings()); System.out.println(); System.out.println("Tom makes a $ checking withdrawal"); tom.checkingWithdrawal(1000.0); System.out.println("Tom makes a $ savings withdrawal"); tom.savingsWithdrawal(2000.0); System.out.println("Sue makes a $ checking withdrawal"); sue.checkingWithdrawal(1500.0); System.out.println("Sue makes a $ savings withdrawal"); sue.savingsWithdrawal(3000.0); System.out.println(); System.out.println("Tom's checking balance: " + tom.getChecking()); System.out.println("Tom's savings balance: " + tom.getSavings()); System.out.println("Sue's checking balance: " + sue.getChecking()); System.out.println("Sue's savings balance: " + sue.getSavings()); System.out.println(); } JAVA0604.JAVA Tom's checking balance: Tom's savings balance: Sue's checking balance: Sue's savings balance: Tom makes a $ checking withdrawal Tom makes a $ savings withdrawal Sue makes a $ checking withdrawal Sue makes a $ savings withdrawal Tom's checking balance: Tom's savings balance: Sue's checking balance: Sue's savings balance:

// Java0605.java // This program demonstrates how to "right justify" integers with an object // of the class and the method. import java.text.DecimalFormat; // necessary to use DecimalFormat class public class Java0605 { public static void main (String args[]) { System.out.println("\nJAVA0605.JAVA\n"); DecimalFormat output = new DecimalFormat("00000"); System.out.println(output.format(1)); System.out.println(output.format(12)); System.out.println(output.format(123)); System.out.println(output.format(1234)); System.out.println(output.format(12345)); System.out.println(output.format(123456)); System.out.println(output.format( )); System.out.println(); } JAVA0605.JAVA

// Java0606.java // This program brings back the Nested Loop Times Table from Java0530.java. // With the help of the output looks much better. import java.text.DecimalFormat; // necessary to use DecimalFormat class public class Java0606 { public static void main (String args[]) { System.out.println("\nJAVA0606.JAVA\n"); DecimalFormat output = new DecimalFormat("000"); for (int r = 1; r <= 10; r++) { for (int c = 1; c <= 15; c++) { int product = r * c; System.out.print( output.format(product ) + " "); } System.out.println(); } System.out.println(); } JAVA0606.JAVA

// Java0607.java // This program demonstrates how to insert commas in // numerical comma with a object. import java.text.DecimalFormat; public class Java0607 { public static void main (String args[]) { System.out.println("\nJAVA0607.JAVA\n"); DecimalFormat output = new DecimalFormat("0,000,000"); System.out.println(output.format(1)); System.out.println(output.format(12)); System.out.println(output.format(123)); System.out.println(output.format(1234)); System.out.println(output.format(12345)); System.out.println(output.format(123456)); System.out.println(output.format( )); System.out.println(); } JAVA0607.JAVA 0,000,001 0,000,012 0,000,123 0,001,234 0,012,345 0,123,456 1,234,567 Try this! Change the DecimalFormat to “0,000” and change the last number to

// Java0608.java // This program demonstrates how to display US currency amounts. // Additionally note how the methods rounds off to the nearest penny. // Also, note that the object does not have to be called "output". import java.text.DecimalFormat; public class Java0608 { public static void main (String args[]) { System.out.println("\nJAVA0608.JAVA\n"); DecimalFormat money = new DecimalFormat("$0.00"); System.out.println(money.format(1)); System.out.println(money.format(12.2)); System.out.println(money.format(123.32)); System.out.println(money.format( )); System.out.println(money.format( )); System.out.println(money.format( )); System.out.println(money.format( )); System.out.println(); } JAVA0608.JAVA $1.00 $12.20 $ $ $ $ $

// Java0609.java // This program demonstrates how to control rounding off to a specified // number of digits beyond the decimal point. // It also shows that multiple objects can be in the same program. import java.text.DecimalFormat; public class Java0609 { public static void main (String args[]) { System.out.println("\nJAVA0609.JAVA\n"); DecimalFormat output1 = new DecimalFormat("0.0"); DecimalFormat output2 = new DecimalFormat("0.00"); DecimalFormat output3 = new DecimalFormat("0.000"); DecimalFormat output4 = new DecimalFormat("0.0000"); DecimalFormat output5 = new DecimalFormat(" "); DecimalFormat output6 = new DecimalFormat(" "); DecimalFormat output7 = new DecimalFormat(" "); DecimalFormat output8 = new DecimalFormat(" "); System.out.println(Math.PI); System.out.println(output1.format(Math.PI)); System.out.println(output2.format(Math.PI)); System.out.println(output3.format(Math.PI)); System.out.println(output4.format(Math.PI)); System.out.println(output5.format(Math.PI)); System.out.println(output6.format(Math.PI)); System.out.println(output7.format(Math.PI)); System.out.println(output8.format(Math.PI)); System.out.println(); } JAVA0609.JAVA

// Java0610.java // This program demonstrates the method. // With, the string // Hello World will be displayed starting at the [x,y] pixel coordinate. import java.awt.*; import java.applet.*; public class Java0610 extends Applet { public void paint(Graphics g) { Expo.drawString(g,"Top-Left-Hand Corner",40,40); Expo.drawString(g,"Top-Right-Hand Corner",840,40); Expo.drawString(g,"Bottom-Left-Hand Corner",40,640); Expo.drawString(g,"Bottom-Right-Hand Corner",820,640); Expo.drawString(g,"Middle",480,330); }

The drawString Method Expo.drawString(g,"Hello There!", x, y); Draws any String starting at coordinate (x,y). Hello There! x, y

// Java0611.java // This program introduces the method. setFont requires a Name, Style and Size. // Name is either "Courier", "TimesRoman", "Arial", or any other available font. // Style is either Font.PLAIN, Font.BOLD, Font.ITALIC or Font.BOLD+Font.Italic. // Size is the point size value of the Font. import java.awt.*; public class Java0611 extends java.applet.Applet { public void paint(Graphics g) { Expo.drawString(g,"Default Appearance with drawString",20,20); Expo.setFont(g,"Courier",Font.PLAIN,20); Expo.drawString(g,"Courier 20-point plain font",20,60); Expo.setFont(g,"Courier",Font.BOLD,20); Expo.drawString(g,"Courier 20-point bold font",20,100); Expo.setFont(g,"TimesRoman",Font.PLAIN,36); Expo.drawString(g,"Times Roman 36-point plain font",20,180); Expo.setFont(g,"TimesRoman",Font.ITALIC,36); Expo.drawString(g,"Times Roman 36-point italic font",20,260); Expo.setFont(g,"Arial",Font.PLAIN,72); Expo.drawString(g,"Arial 72-point plain font",20,400); Expo.setFont(g,"Algerian", Font.BOLD+Font.ITALIC, 48); Expo.drawString(g,"Algerian 48-point bold/italic font",20,500); Expo.setFont(g,"Qwerty",Font.PLAIN,24); Expo.drawString(g," Arial 24-point plain font substituted for non-existent Qwerty font ",20,600); }

// Java0612.java // This program demonstrates the method. // can handle between 3 or more sets of coordinate // points to draw a triangle, quadrilateral, pentagon, hexagon, // octagon, or any other polygon. import java.awt.*; import java.applet.*; public class Java0612 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,500,100,800,200,600,400,400,400,200,200); } 500, , ,400400, ,200

// Java0612.java // This program demonstrates the method. // can handle between 3 or more sets of coordinate // points to draw a triangle, quadrilateral, pentagon, hexagon, // octagon, or any other polygon. import java.awt.*; import java.applet.*; public class Java0612extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,500,100,800,200,600,400,400,400,200,200); } 500, , ,400400, ,200

// Java0612.java // This program demonstrates the method. // can handle between 3 or more sets of coordinate // points to draw a triangle, quadrilateral, pentagon, hexagon, // octagon, or any other polygon. import java.awt.*; import java.applet.*; public class Java0612extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,500,100,800,200,600,400,400,400,200,200); } 500, , ,400400, ,200

// Java0612.java // This program demonstrates the method. // can handle between 3 or more sets of coordinate // points to draw a triangle, quadrilateral, pentagon, hexagon, // octagon, or any other polygon. import java.awt.*; import java.applet.*; public class Java0612extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,500,100,800,200,600,400,400,400,200,200); } 500, , ,400400, ,200

// Java0612.java // This program demonstrates the method. // can handle between 3 or more sets of coordinate // points to draw a triangle, quadrilateral, pentagon, hexagon, // octagon, or any other polygon. import java.awt.*; import java.applet.*; public class Java0612extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,500,100,800,200,600,400,400,400,200,200); } 500, , ,400400, ,200

// Java0612.java // This program demonstrates the method. // can handle between 3 or more sets of coordinate // points to draw a triangle, quadrilateral, pentagon, hexagon, // octagon, or any other polygon. import java.awt.*; import java.applet.*; public class Java0612extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,500,100,800,200,600,400,400,400,200,200); } 500, , ,400400, ,200

// Java0612.java // This program demonstrates the method. // can handle between 3 or more sets of coordinate // points to draw a triangle, quadrilateral, pentagon, hexagon, // octagon, or any other polygon. import java.awt.*; import java.applet.*; public class Java0612extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,500,100,800,200,600,400,400,400,200,200); } 500, , ,400400, ,200

// Java0613.java // This program demonstrates that the sequence of adding coordinate // points as parameters is significant. The same coordinates of the // previous program are used in a different sequence. // The display is very different. import java.awt.*; import java.applet.*; public class Java0613 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,400,400,500,100,800,200,200,200,600,400); } 500, , ,400400, ,200

// Java0613.java // This program demonstrates that the sequence of adding coordinate // points as parameters is significant. The same coordinates of the // previous program are used in a different sequence. // The display is very different. import java.awt.*; import java.applet.*; public class Java0613 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,400,400,500,100,800,200,200,200,600,400); } 500, , ,400400, ,200

// Java0613.java // This program demonstrates that the sequence of adding coordinate // points as parameters is significant. The same coordinates of the // previous program are used in a different sequence. // The display is very different. import java.awt.*; import java.applet.*; public class Java0613 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,400,400,500,100,800,200,200,200,600,400); } 500, , ,400400, ,200

// Java0613.java // This program demonstrates that the sequence of adding coordinate // points as parameters is significant. The same coordinates of the // previous program are used in a different sequence. // The display is very different. import java.awt.*; import java.applet.*; public class Java0613 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,400,400,500,100,800,200,200,200,600,400); } 500, , ,400400, ,200

// Java0613.java // This program demonstrates that the sequence of adding coordinate // points as parameters is significant. The same coordinates of the // previous program are used in a different sequence. // The display is very different. import java.awt.*; import java.applet.*; public class Java0613 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,400,400,500,100,800,200,200,200,600,400); } 500, , ,400400, ,200

// Java0613.java // This program demonstrates that the sequence of adding coordinate // points as parameters is significant. The same coordinates of the // previous program are used in a different sequence. // The display is very different. import java.awt.*; import java.applet.*; public class Java0613 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,400,400,500,100,800,200,200,200,600,400); } 500, , ,400400, ,200

// Java0613.java // This program demonstrates that the sequence of adding coordinate // points as parameters is significant. The same coordinates of the // previous program are used in a different sequence. // The display is very different. import java.awt.*; import java.applet.*; public class Java0613 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,400,400,500,100,800,200,200,200,600,400); } 500, , ,400400, ,200

// Java0614.java // This program demonstrates fillPolygon. import java.awt.*; import java.applet.*; public class Java0614 extends Applet { public void paint(Graphics g) { Expo.fillPolygon(g,500,100,800,200,600,400,400,400,200,200); }

// Java0615.java // This program fills 2 Polygons to draw a little boy with a red shirt and blue pants. import java.awt.*; import java.applet.*; public class Java0615 extends Applet { public void paint(Graphics g) { Expo.drawCircle(g,500,100,50); Expo.drawLine(g,500,150,500,400); Expo.drawLine(g,500,400,400,600); Expo.drawLine(g,500,400,600,600); Expo.drawLine(g,300,225,700,225); Expo.setColor(g,Expo.red); Expo.fillPolygon(g,350,200,650,200,650,250,575,250, 575,350,425,350,425,250,350,250); Expo.setColor(g,Expo.blue); Expo.fillPolygon(g,425,375,425,425,350,550,450,600, 500,450,550,600,650,550,575,425,575,375); }

1 // Java0616.java 2 // This program demonstrates what happens when / 3 // is called with an incorrect number of parameters. 4 // Since the parameters represent coordinate points, 5 // the number of parameters must always be even. 6 // Since a polygon must have at least 3 sides, 7 // the number of parameters must be at least 6. 8 // If either condition is not met, a special error message is displayed import java.awt.*; 12 import java.applet.*; public class Java0616 extends Applet 16 { 17 public void paint(Graphics g) 18 { 19 Expo.drawCircle(g,500,100,50); 20 Expo.drawLine(g,500,150,500,400); 21 Expo.drawLine(g,500,400,400,600); 22 Expo.drawLine(g,500,400,600,600); 23 Expo.drawLine(g,300,225,700,225);

24 25 Expo.setColor(g,Expo.red); 26 Expo.fillPolygon(g,350,200,650,200,650,250,575,250, 575,350,425,350,425,250,350); 27 // The last parameter, 250, was removed from the command above. 28 // Now the last x value does not have a corresponding y value Expo.setColor(g,Expo.blue); 31 Expo.fillPolygon(g,425,375,425,425,350,550,450,600,500,450,550,600,650,550,575,425,575,375); 32 } 33 } 34

NOTE: After you click “OK” the rest of the program does execute without the messed up red shirt.

// Java0617.java // This program reviews drawPolygon, setFont, and drawString. import java.awt.*; import java.applet.*; public class Java0617 extends Applet { public void paint(Graphics g) { Expo.drawPolygon(g,175,175,250,50,350,200); Expo.drawString(g,"Triangle",225,150); Expo.drawPolygon(g,400,200,525,200,600,50,475,50); Expo.setFont(g,"Arial",Font.BOLD,16); Expo.drawString(g,"Quadralateral",425,175); Expo.drawPolygon(g,650,100,750,50,850,100,850,200,650,200); Expo.setFont(g,"Arial",Font.ITALIC,24); Expo.drawString(g,"Pentagon",700,150); Expo.drawPolygon(g,25,250,225,250,175,325,225,400,25,400,75,325); Expo.setFont(g,"TimesRoman",Font.PLAIN,24); Expo.drawString(g,"Hexagon",75,375); Expo.drawPolygon(g,275,250,375,250,475,300,475,400,350,400,350,325,275,325); Expo.setFont(g,"TimesRoman",Font.BOLD,28); Expo.drawString(g,"Heptagon",300,300); Continued on the next slide…

…Java0617.java Continued Expo.drawPolygon(g,525,300,600,250,650,250,725,300,725,350,650,400,600,400,525,350); Expo.setFont(g,"TimesRoman",Font.BOLD+Font.ITALIC,44); Expo.drawString(g,"Octagon",550,340); Expo.drawPolygon(g,775,300,800,250,975,250,925,325,975,325,975,400,875,400,825,375, 850,325); Expo.setFont(g,"TimesRoman",Font.ITALIC,36); Expo.drawString(g,"Nonagon",800,300); Expo.drawPolygon(g,150,500,200,450,250,500,300,450,350,500,350,550,300,600,250,550, 200,600,150,550); Expo.setFont(g,"Algerian",Font.PLAIN,36); Expo.drawString(g,"Decagon",175,540); Expo.drawPolygon(g,400,450,600,450,575,525,600,525,600,600,550,575,500,600,450,575, 400,600,400,525,425,525); Expo.setFont(g,"Courier",Font.BOLD,28); Expo.drawString(g,"Undecagon",425,560); Expo.drawPolygon(g,650,525,700,450,700,525,725,525,725,450,775,450,775,525,800,525, 800,450,850,525,850,600,650,600); Expo.setFont(g,"Courier",Font.ITALIC,28); Expo.drawString(g,"Dodecagon",675,560); }

The drawRegularPolygon Method Expo.drawRegularPolygon(g, centerX, centerY, radius, sides); The first 4 parameters for Expo.drawRegularPolygon are the same as Expo.drawCircle. The last parameter indicates the number of sides. centerX, centerY radius

// Java0618.java // This program demonstrates the drawRegularPolygon of the Expo class. // Regular Polygons are drawn from their center (X,Y) with a certain // radius and a certain number of sides with // drawRegularPolygon(g,x,y,radius,numSides). import java.awt.*; import java.applet.*; public class Java0618 extends Applet { public void paint(Graphics g) { Expo.drawRegularPolygon(g,200,170,130,5); Expo.drawRegularPolygon(g,500,170,130,6); Expo.drawRegularPolygon(g,800,170,130,7); Expo.drawRegularPolygon(g,200,490,130,8); Expo.drawRegularPolygon(g,500,490,130,9); Expo.drawRegularPolygon(g,800,490,130,10); }

// Java0619.java // This program demonstrates fillRegularPolygon. import java.awt.*; import java.applet.*; public class Java0619 extends Applet { public void paint(Graphics g) { Expo.setColor(g,Expo.blue); Expo.fillRegularPolygon(g,200,170,130,5); Expo.setColor(g,Expo.gold); Expo.fillRegularPolygon(g,500,170,130,6); Expo.setColor(g,Expo.magenta); Expo.fillRegularPolygon(g,800,170,130,7); Expo.setColor(g,Expo.red); Expo.fillRegularPolygon(g,200,490,130,8); Expo.setColor(g,Expo.green); Expo.fillRegularPolygon(g,500,490,130,9); Expo.setColor(g,Expo.purple); Expo.fillRegularPolygon(g,800,490,130,10); }

The drawStar Method Expo.drawStar(g, centerX, centerY, radius, points); The first 3 parameters for Expo.drawStar are the same as Expo.drawCircle. The last parameter indicates the number of points. radius centerX, centerY

// Java0620.java // This program demonstrates the drawStar and fillStar methods. import java.awt.*; import java.applet.*; public class Java0620 extends Applet { public void paint(Graphics g) { Expo.setColor(g,Expo.blue); Expo.drawStar(g,200,170,130,5); Expo.setColor(g,Expo.gold); Expo.fillStar(g,500,170,130,6); Expo.setColor(g,Expo.magenta); Expo.drawStar(g,800,170,130,7); Expo.setColor(g,Expo.red); Expo.fillStar(g,200,490,130,8); Expo.setColor(g,Expo.green); Expo.drawStar(g,500,490,130,9); Expo.setColor(g,Expo.purple); Expo.fillStar(g,800,490,130,10); }

// Java0621.java // This program demonstrates setColor command being used to "create" // new colors. The program will draw the Official Texas Flag with the // EXACT shades of red and blue. import java.awt.*; import java.applet.*; public class Java0621 extends Applet { public void paint(Graphics g) { Expo.setColor(g,0,39,104); // Official shade of blue used in the Texas Flag Expo.fillRectangle(g,0,0,325,650); Expo.setColor(g,190,10,47); // Official shade of red used in the Texas Flag Expo.fillRectangle(g,325,325,1000,650); Expo.setColor(g,255,255,255); Expo.fillStar(g,162,325,130,5); } // Three 255s is the same as color Expo.white

Where do you get the 3 color #s I started by downloading an image of the Official Texas Flag. I then loaded it in Paint. Using the eye-dropper tool, I then clicked on the desired color. I then clicked [Edit Colors] and made note of the red, green & blue values. By using the same 3 numbers in my program, I get the exact same color in the output of my Java program: Expo.setColor(g,0,39,104);

// Java0622.java // This program shows all the shades of Red, Green and Blue using the method. import java.awt.*; import java.applet.*; public class Java0622 extends Applet { public void paint(Graphics g) { int x = 50; for (int red = 0; red <= 255; red++) { Expo.setColor(g,red,0,0); Expo.drawLine(g,x,0,x,650); x++; } x = 350; for (int green = 0; green <= 255; green++) { Expo.setColor(g,0,green,0); Expo.drawLine(g,x,0,x,650); x++; } x = 650; for (int blue = 0; blue <= 255;blue++) { Expo.setColor(g,0,0,blue); Expo.drawLine(g,x,0,x,650); x++; }

// Java0623.java // This program uses the method of the Expo class // 5 times to create 5 random numbers between 1 and 100. public class Java0623 { public static void main (String args[]) { System.out.println("\nJAVA0621.JAVA\n"); System.out.println(Expo.random(1,100)); } JAVA0623.JAVA JAVA0623.JAVA Since we are working with RANDOM #s, your outputs will be different than mine – and also different from the other students in the class.

// Java0624.java // This program allows the user to specify the range of random numbers. public class Java0624 { public static void main (String args[]) { System.out.println("\nJAVA0624.JAVA\n"); System.out.print("Enter the smallest number you want. --> "); int minimum = Expo.enterInt(); System.out.print("Enter the largest number you want. --> "); int maximum = Expo.enterInt(); System.out.println(); System.out.println(Expo.random(minimum,maximum)); System.out.println(); }

JAVA0624.JAVA Enter the smallest number you want. --> 10 Enter the largest number you want. --> JAVA0624.JAVA Enter the smallest number you want. --> 1000 Enter the largest number you want. -->

// Java0625.java // This program first displays two lines in a fixed location. // This is followed by generating four random values, which are // used to draw a third line in a random location. import java.awt.*; import java.applet.*; public class Java0625 extends Applet { public void paint(Graphics g) { Expo.drawLine(g,500,0,500,649); Expo.drawLine(g,0,325,999,325); int x1 = Expo.random(0,999); int y1 = Expo.random(0,649); int x2 = Expo.random(0,999); int y2 = Expo.random(0,649); Expo.setColor(g,Color.red); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0626.java // This program displays a circle in a fixed location with a fixed size. // Two random values are generated for the center location of the circle // and a third random value is used for the radius of the circle. import java.awt.*; import java.applet.*; public class Java0626 extends Applet { public void paint(Graphics g) { Expo.drawCircle(g,500,325,250); int x = Expo.random(150,850); int y = Expo.random(150,500); int radius = Expo.random(10,150); Expo.setColor(g,Expo.red); Expo.drawCircle(g,x,y,radius); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random(0,650); int x2 = Expo.random(0,1000); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

Changing Random Number Ranges to Affect the Graphics Program Output The next several slides will still show program Java0627.java, but the output of the program is different. You need to figure out how to change the each program to make it produce the output shown. NOTE: This skill is essential in doing Graphics Lab 03!

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random(0,650); int x2 = Expo.random(0,1000); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random( 0,500 ); int y1 = Expo.random(0,650); int x2 = Expo.random( 0,500 ); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random(0,650); int x2 = Expo.random(0,1000); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random( 500,1000 ); int y1 = Expo.random(0,650); int x2 = Expo.random( 500,1000 ); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random(0,650); int x2 = Expo.random(0,1000); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random( 0,325 ); int x2 = Expo.random(0,1000); int y2 = Expo.random( 0,325 ); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random(0,650); int x2 = Expo.random(0,1000); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random( 325,650 ); int x2 = Expo.random(0,1000); int y2 = Expo.random( 325,650 ); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random(0,650); int x2 = Expo.random(0,1000); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random( 0,500 ); int y1 = Expo.random( 0,325 ); int x2 = Expo.random( 0,500 ); int y2 = Expo.random( 0,325 ); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random(0,1000); int y1 = Expo.random(0,650); int x2 = Expo.random(0,1000); int y2 = Expo.random(0,650); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0627.java // This program displays 1000 random lines. import java.awt.*; import java.applet.*; public class Java0627 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x1 = Expo.random( 500,1000 ); int y1 = Expo.random( 325,650 ); int x2 = Expo.random( 500,1000 ); int y2 = Expo.random( 325,650 ); Expo.drawLine(g,x1,y1,x2,y2); }

// Java0628.java // This program displays 1000 random colored solid circles. // All circles have a radius of 50. import java.awt.*; import java.applet.*; public class Java0628 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x = Expo.random(0,1000); int y = Expo.random(0,650); 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.fillCircle(g,x,y,50); }

// Java0629.java // This program displays 1000 random colored solid circles, with random radii. import java.awt.*; import java.applet.*; public class Java0629 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x = Expo.random(0,1000); int y = Expo.random(0,650); int red = Expo.random(0,255); int green = Expo.random(0,255); int blue = Expo.random(0,255); int radius = Expo.random(1,100); Expo.setColor(g,red,green,blue); Expo.fillCircle(g,x,y,radius); }

// Java0630.java // This program displays 1000 randomly colored equilateral triangles. // Creating random colors is also simplified with the setRandomColor method. import java.awt.*; import java.applet.*; public class Java0630 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x = Expo.random(0,1000); int y = Expo.random(0,650); int radius = Expo.random(1,100); int sides = 3; Expo.setRandomColor(g); Expo.fillRegularPolygon(g,x,y,radius,sides); }

// Java0631.java // This program displays 1000 randomly colored polygons with a random # of sides. import java.awt.*; import java.applet.*; public class Java0631 extends Applet { public void paint(Graphics g) { for (int k = 1; k <= 1000; k++) { int x = Expo.random(0,1000); int y = Expo.random(0,650); int radius = Expo.random(1,100); int sides = Expo.random(3,10); Expo.setRandomColor(g); Expo.fillRegularPolygon(g,x,y,radius,sides); }