Lab 3: Circle Applet CMPT 183 Dr. Gutierrez Topics for today … • Simple syntax & math operations • Random • Interactivity (mouse) • Basic structure of class (again) • Hands-on Circle Applet Created by Jerry Alan Fails Simple Syntax: Assignment & Math Ops • Assignment – Used to store information in a variable – Example: x = 0; // the variable x gets the value zero • Math Operations – Simple math operations: • + : addition • - : subtraction • * : multiplication • / : division – Example: : x = 53 + 4 – 3 * (12 / 2); // x gets ??? • For today you’ll also need Math.PI (π) Created by Jerry Alan Fails 39 The Random Class • The Random class is part of the java.util package • It provides methods that generate pseudorandom numbers • A Random object performs complicated calculations based on a seed value to produce a stream of seemingly random values • See RandomNumbers.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // RandomNumbers.java Author: Lewis/Loftus // // Demonstrates the creation of pseudo-random numbers using the // Random class. //******************************************************************** import java.util.Random; public class RandomNumbers { //----------------------------------------------------------------- // Generates random numbers in various ranges. //----------------------------------------------------------------- public static void main (String[] args) { Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = generator.nextInt(10); System.out.println ("From 0 to 9: " + num1); continued Copyright © 2012 Pearson Education, Inc. continued num1 = generator.nextInt(10) + 1; System.out.println ("From 1 to 10: " + num1); num1 = generator.nextInt(15) + 20; System.out.println ("From 20 to 34: " + num1); num1 = generator.nextInt(20) - 10; System.out.println ("From -10 to 9: " + num1); num2 = generator.nextFloat(); System.out.println ("A random float (between 0-1): " + num2); num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 num1 = (int)num2 + 1; System.out.println ("From 1 to 6: " + num1); } } Sample Run A random integer: 672981683 From 0 to 9: 0 From 1 to 10: 3 From 20 to 34: 30 From -10 to 9: -4 A random float (between 0-1): 0.18538326 From 1 to 6: 3 Examples – code to outcome Copyright © 2012 Pearson Education, Inc. Given a Random object named gen, what range of values are produced by the following expressions? gen.nextInt(25) gen.nextInt(6) + 1 gen.nextInt(100) + 10 gen.nextInt(50) + 100 gen.nextInt(10) – 5 gen.nextInt(22) + 12 Range? 0 to 24 1 to 6 10 to 109 100 to 149 -5 to 4 12 to 33 Examples – outcome to code Copyright © 2012 Pearson Education, Inc. Write an expression that produces a random integer in the following ranges: gen.nextInt(13) gen.nextInt(20) + 1 gen.nextInt(6) + 15 gen.nextInt(11) – 10 Range 0 to 12 1 to 20 15 to 20 -10 to 0 Mouse Events • Events related to the mouse are separated into mouse events and mouse motion events • Mouse Events: mouse pressed the mouse button is pressed down mouse released the mouse button is released mouse clicked the mouse button is pressed down and released without moving the mouse in between mouse entered the mouse pointer is moved onto (over) a component mouse exited the mouse pointer is moved off of a component Mouse Events • Mouse motion events: • Listeners for mouse events are created using the MouseListener and MouseMotionListener interfaces • A MouseEvent object is passed to the appropriate method when a mouse event occurs Copyright © 2012 Pearson Education, Inc. mouse moved the mouse is moved mouse dragged the mouse is moved while the mouse button is pressed down Making an Applet Interactive • We need to listen for mouse events & handle them • In addition to extending JApplet, implement MouseListener • In init() method, register event listener by calling addMouseListener( this ); • Create empty definitions for mouse event methods: public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { } public void mousePressed(MouseEvent evt) { } Created by Emily Hill Responding to a Mouse Event • One mouse event method should NOT be empty: public void mousePressed(MouseEvent evt) { Point p = evt.getPoint(); // Update the x/y coordinates // refresh the applet repaint(); } • Example: HelloWorldNoOverlayEvent.java Created by Emily Hill Created by Emily Hill Basic class structure Three major components of a class: – Fields – store data for the object to use – Constructors – allow the object to be set up properly when first created – Methods – implement the behavior of the object public class ClassName { Fields Constructors Methods } Fields • Fields store values for an object. • They are also known as instance variables. • Fields define the state of an object. public class Square { private int x; private int y; private int size; private Color fillColor; // Further details omitted. } private int size; visibility modifier type variable name Constructors • Constructors initialize an object. • They have the same name as their class. • They store initial values into the fields. • They often receive external parameter values for this. public Square(int x, int y) { this.x = x; this.y = y; size = 50; color = Color.blue; } /** * Gets the size of the square. */ Methods public int getSize() { return size; } return type method name parameter list (empty) start and end of method body (block) return statement visibility modifier method header/signature Constructor Practice • Import the starter project. Download if from Course Materials in Blackboard • Rename it to the same name plus your netID • Add a constructor to the Circle class that accepts as parameters the initial x and y positions, radius, and color • Alter your Picture class to use these constructors (instead of all the method calls you used before) • What do you think of this approach? Method Practice • Add to (or modify) the Circle class: – Add a getArea method that returns the area of the circle – Add a getPerimeter method that returns the length of the circunference – Modify paintComponent so it shows the area and perimeter of the circle (look at the first HelloWorld lab to remind yourself how to draw a string) – EXTRA: Use the DecimalFormat class to only show two significant digits (see book section 3.6) • Use your Picture applet to test and make sure the area and perimeter are showing up and are being calculated correctly Interactive Circle Applet • Modify the provided CircleApplet starter class – You need to store a circle and a random object – Initialize/create the object to be stored in your init method – Paint your circle in your paint method • Modify the mouseClicked method body so the circle: – Is moved to the point where the mouse was clicked – Gets a new random radius that is at least 30 pixels – Gets a new random color What we did today … • Concepts practiced – Constructors – Methods – Classes – Interactivity (mouse events) • Modified the Circle and Square classes: – Added a constructor that accepts as parameters the initial x and y positions, radius, and color (should already be done from previous slide) – Added a getArea method that returns the area of the circle/square – Added a getPerimeter method that returns the area of the circle/square – Modified paintComponent so it shows the area and perimeter of the circle/square • Defined a new applet class – CircleApplet – that – Stores a circle and a random object (fields) – Paints the circle – Listens for mouse clicking and when it is clicked • Change the location of the circle to click location • And gets random radius and color Homework • Finish lab exercise • CodingBat • Read Chapters 2 and 3