The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating...
Transcript of The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating...
![Page 1: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/1.jpg)
The ACM Java Libraries
Andrew MertzWilliam Slough
Nancy Van Cleave
Mathematics and Computer Science DepartmentEastern Illinois University
October 10, 2009
CCSC:MW 2009, Saint Xavier University
![Page 2: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/2.jpg)
Introduction
NetBeans and Java
The ACM Java Task Force (JTF) Libraries
Placing applets on web pages
How to learn about the ACM libraries:
This workshop
Textbooks
ACM Java Task Force Tutorial
Javadoc documentation
CCSC:MW 2009, Saint Xavier University
![Page 3: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/3.jpg)
Textbook support for ACM libraries
CCSC:MW 2009, Saint Xavier University
![Page 4: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/4.jpg)
ACM library galleries
EIU student work:http://www.eiu.edu/~mathcs/http
JTF demo gallery:http://jtf.acm.org/demos/index.html
CCSC:MW 2009, Saint Xavier University
![Page 5: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/5.jpg)
Installing the necessary software: NetBeans
www.netbeans.org/downloads/
Click on the Download NetBeans IDE Button
Run install
CCSC:MW 2009, Saint Xavier University
![Page 6: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/6.jpg)
Installating the necessary software: ACM Java libraries
jtf.acm.org — obtain acm.zip
Create acmLibrary project in NetBeansFile → New Project
Within the Categories panel, select Java
Within the Projects, select Java Class Library
Click Next to proceed
For the project name, enter acmLibrary
Click on the Finish button
Move the acm.zip file to the acmLibrary/src directory
Unpack the zip file (into the acmLibrary/src directory)
Right–click on the acmLibrary project in NetBeans, andselect close (don’t shut down NetBeans)
CCSC:MW 2009, Saint Xavier University
![Page 7: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/7.jpg)
The Program class diagram
JApplet
Program
GraphicsProgram ConsoleProgram DialogProgram
CCSC:MW 2009, Saint Xavier University
![Page 8: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/8.jpg)
A “Hello, world!” program
Create HelloProgram project
File → New Project
Within the Categories panel, select Java
Within the Projects, select Java Class Library
Click Next to proceed
For the project name, enter HelloProgram
Click on the Finish button
Make the acmLibrary available to this project
Right–click on Libraries, select Add Project
In the Add Project window choose acmLibrary
Click on Add Project / Jar files
CCSC:MW 2009, Saint Xavier University
![Page 9: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/9.jpg)
Create a new Java program
Right–click on Source Packages, then select New → Other
Select Java from the categories
The file type to select is Empty Java File
Click Next, then replace the suggested class name NewEmptywith HelloProgram
Click the Finish button
CCSC:MW 2009, Saint Xavier University
![Page 10: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/10.jpg)
A “Hello, world!” program: in a console window
import acm.graphics.*;import acm.program.*;
public class HelloProgram extends ConsoleProgram {public void run() {println("Hello, world!");
}}
Modifications:
Add a second line below the first with your name
Add a third line with your organization
CCSC:MW 2009, Saint Xavier University
![Page 11: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/11.jpg)
A “Hello, world!” program: with dialog boxes
import acm.graphics.*;import acm.program.*;
public class HelloProgram extends DialogProgram {public void run() {println("Hello, world!");println("Name here");println("Organization here");
}}
Modifications:
Change ConsoleProgram to DialogProgram
Change first two println statements to print statementswith \n to produce a single dialog window
CCSC:MW 2009, Saint Xavier University
![Page 12: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/12.jpg)
A “Hello, world!” program: in a graphics window
import acm.graphics.*;import acm.program.*;
public class HelloProgram extends GraphicsProgram {public void run() {add(new GLabel("Hello, world!", 100, 75));
}}
Modifications:
Move the output line down
Add lines for your name and organization
CCSC:MW 2009, Saint Xavier University
![Page 13: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/13.jpg)
Placing applets on a web site
index.html
HelloProgram.html
CCSC:MW 2009, Saint Xavier University
![Page 14: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/14.jpg)
Add2Quantities
import acm.program.*;
public class Add2Quantities extends ConsoleProgram {public void run() {
println("This program adds two values:");int a = readInt("Enter a: ");int b = readInt("Enter b: ");int sum = a + b;println("The sum is " + sum + ".");
}}
Modifications:
Convert to a DialogProgram
Different data types:int → double, readInt → readDoubleint → String, readInt → readLine
CCSC:MW 2009, Saint Xavier University
![Page 15: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/15.jpg)
The acm.graphics model
The acm.graphics package uses a collage model in which youcreate an image by adding various objects to a canvas.
This is similar to a felt board that serves as a backdrop for coloredshapes that stick to the felt surface.
Note that newer objects can obscure those added earlier. Thislayering arrangement is called the stacking order or z-order.
CCSC:MW 2009, Saint Xavier University
![Page 16: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/16.jpg)
The Java coordinate system
All distances and coordinates in the graphics library are measuredin pixels.
Coordinates in the graphics model are specified relative to theorigin in the upper left corner of the screen.
Coordinate values are specified as a pair of floating-point values(x, y) where the values for x increase as you move rightward acrossthe screen and the values for y increase as you move downward.
CCSC:MW 2009, Saint Xavier University
![Page 17: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/17.jpg)
Partial class diagram for acm.graphics
java.awt.Container
GCanvas
GObject GCompound
GLabel
GRectGOval
GLine
GPolygon
GImageGArc
GTurtle
CCSC:MW 2009, Saint Xavier University
![Page 18: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/18.jpg)
The GCanvas class
The GCanvas class represents the background canvas, a virtual feltboard.
When you use the acm.graphics package, you create pictures byadding various GObjects to a GCanvas.
For simple applications, you will not need to work with an explicitGCanvas object.
Programs that extend GraphicsProgram, automatically creates aGCanvas and resizes it so that it fills the program window.
Most of the methods defined for the GCanvas class are alsoavailable in a GraphicsProgram, thanks to delegation.
CCSC:MW 2009, Saint Xavier University
![Page 19: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/19.jpg)
Methods in the GCanvas class
add(object) Adds the object to the canvas at the frontof the stack
add(object, x, y) Moves the object to (x, y) and then adds itto the canvas
remove(object) Removes the object from the canvas
removeAll() Removes all objects from the canvas
getElementAt(x, y) Returns the front most object at (x, y), ornull if none
getWidth() Returns the width in pixels of the canvas
getHeight() Returns the height in pixels of the canvas
setBackground(c) Sets the background color of the canvas
CCSC:MW 2009, Saint Xavier University
![Page 20: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/20.jpg)
Methods in the GCanvas class
The following methods are available in GraphicsProgram only:
pause(milliseconds) Pauses the program for the specified time
waitForClick() Suspends the program until the user clicksthe mouse
CCSC:MW 2009, Saint Xavier University
![Page 21: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/21.jpg)
Two forms of the add method
The add method comes in two forms.
add(object);
adds the object at the location currently stored in its internalstructure. You use this form when you have already set thecoordinates of the object, usually done when it was created.
The second form:
add(object, x, y);
which moves the object to the point (x, y) and then adds it there.This form is useful when you need to determine some property ofthe object before you know where to put it. If, for example, youwant to center a GLabel, you must first create it and then use itssize to determine its location.
CCSC:MW 2009, Saint Xavier University
![Page 22: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/22.jpg)
Centering text
import acm.graphics.*;import acm.program.*;
public class CenteredLabel extends GraphicsProgram {
public void run() {GLabel label = new GLabel("I am centered");add(label,
(getWidth() - label.getWidth()) / 2,(getHeight() - label.getHeight()) / 2);
}}
CCSC:MW 2009, Saint Xavier University
![Page 23: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/23.jpg)
Methods common to GObjects
setLocation(x, y) Resets the location of the object to the specifiedpoint
move(dx, dy) Moves the object dx and dy pixels
movePolar(r, theta) Moves the object r pixels in the direction theta
getX() Returns the x coordinate of the object
getY() Returns the y coordinate of the object
getWidth() Returns the horizontal width of the object in pixels
getHeight() Returns the vertical height of the object in pixels
contains(x, y) Returns true if the object contains the specifiedpoint
CCSC:MW 2009, Saint Xavier University
![Page 24: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/24.jpg)
Methods common to GObjects (continued)
setColor(c) Sets the color of the object
getColor() Returns the color currently assigned to the object
setVisible(flag) Sets visibility (false = invisible, true = visible)
isVisible() Returns true if the object is visible
sendToFront() Sends the object to the front of the stacking order
sendToBack() Sends the object to the back of the stacking order
sendForward() Sends the object forward one position in the stack-ing order
sendBackward() Sends the object backward one position in the stack-ing order
CCSC:MW 2009, Saint Xavier University
![Page 25: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/25.jpg)
Sharing behavior through interfaces
There are some methods that apply to some GObject subclasses,but not others.
For example, you can call setFilled on GOvals or GRects.However, it does not make sense to call setFilled on a GLine.
In the acm.graphics package, there are three interfaces thatdefine methods for certain GObject subclasses:
GFillable
GResizable
GScalable
CCSC:MW 2009, Saint Xavier University
![Page 26: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/26.jpg)
acm.graphics interfaces
GFillable (GArc, GOval, GPolygon, GRect)
setFilled(flag) Sets the fill state (false = outlined, true = filled)
isFilled() Returns the fill state
setFillColor(c) Sets the color used to fill the interior
getFillColor() Returns the fill color
GResizable (GImage, GOval, GRect)
setSize(width, height) Sets the dimensions of the object
setBounds(x, y, width, height) Sets the location and dimensions
GScalable (GArc, GCompound, GLine, GImage, GOval, GPolygon, GRect)
scale(sf) Scales both dimensions of the object by sf
scale(sx, sy) Scales the object by sx horizontally and sy vertically
CCSC:MW 2009, Saint Xavier University
![Page 27: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/27.jpg)
Making a face
CCSC:MW 2009, Saint Xavier University
![Page 28: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/28.jpg)
Exercise: Drawing a house
CCSC:MW 2009, Saint Xavier University
![Page 29: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/29.jpg)
Encapsulated coordinates
The acm.graphics package defines three classes:
GPoint
GDimension
GRectangle
These combine geometric information about coordinates and sizesinto a single object.
GPoint encapsulates the x and y coordinates of a point
GDimension encapsulates width and height values that specifyan object’s size
GRectangle encapsulates all four of these values by specifyingboth the origin and size of a rectangle
CCSC:MW 2009, Saint Xavier University
![Page 30: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/30.jpg)
Gathering input
Event driven model
Most of the time the program is waiting for something(an event) to occur
When an event occurs, the program responds appropriately
eventRespond to
an event
Event occursResponsecomplete
Wait for
CCSC:MW 2009, Saint Xavier University
![Page 31: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/31.jpg)
The Java event model
In Java, you indicate what events you wish to respond to bydesignating some object as a listener for that event.
When the event occurs, a message is sent to the listener, whichtriggers the appropriate response.
CCSC:MW 2009, Saint Xavier University
![Page 32: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/32.jpg)
Events the ACM library facilitates
Keyboard events: key presses and key releases. Identifies the keyand whether shift, control and/or alt are also pressed.
Mouse events: motion, button presses and button releases.Motion events tell where the position of the mouse is now, how farit moved and if any buttons are pressed.
Action events: GUI buttons pressed and other user-interfaceactions.
CCSC:MW 2009, Saint Xavier University
![Page 33: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/33.jpg)
A simple event-driven program
import acm.graphics.*;import acm.program.*;import java.awt.event.*;
public class Dots extends GraphicsProgram {public void init() {addMouseListeners();
}
public void mouseClicked(MouseEvent e) {GOval dot = new GOval(DOT_SIZE, DOT_SIZE);dot.setFilled(true);add(dot,
e.getX() - DOT_SIZE/2,e.getY() - DOT_SIZE/2);
}private static final double DOT_SIZE = 20;
}
CCSC:MW 2009, Saint Xavier University
![Page 34: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/34.jpg)
How it works
The addMouseListeners method of acm.program.Programenables mouse-event reporting.
Clicking the mouse generates a MouseEvent.
That event triggers a call to the mouseClicked method.
The program responds by adding a new GOval to the canvas.
Subsequent mouse clicks are treated in exactly the same way.
CCSC:MW 2009, Saint Xavier University
![Page 35: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/35.jpg)
acm.util.RandomGenerator
The RandomGenerator class can generate pseudorandom integers,doubles, booleans, and colors.
Methods provided include:
nextBoolean(double p) Returns a random boolean valuewith the specified probability
nextColor() Returns a random opaque Color
nextDouble(double low, double high) Returns a random real numberin the specified range
nextInt(int low, int high) Returns a random integer in thespecified range
CCSC:MW 2009, Saint Xavier University
![Page 36: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/36.jpg)
The GMath class
The GMath class has the following static methods:
sinDegrees(theta) Returns the sine of theta, measured in degrees
cosDegrees(theta) Returns the cosine of theta
tanDegrees(theta) Returns the tangent of theta
toRadians(degrees) Converts an angle from degrees to radians
toDegrees(radians) Converts an angle from radians to degrees
distance(x, y) Returns the distance from the origin to (x, y)
distance(x0, y0, x1, y1) Returns the distance from (x0, y0) to (x1, y1)
round(x) Returns the closest int to x
angle(x, y) Returns the angle in degrees formed by the lineconnecting the origin to the point (x, y)
angle(x0, y0, x1, y1) Returns the angle in degrees formed by the lineconnecting the points (x0, y0) and (x1, y1)
CCSC:MW 2009, Saint Xavier University
![Page 37: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/37.jpg)
Adding a touch of color
public class Dots extends GraphicsProgram {public void init() {addMouseListeners();
}
public void mouseClicked(MouseEvent e) {GOval dot = new GOval(DOT_SIZE, DOT_SIZE);dot.setFilled(true);dot.setColor(rand.nextColor());add(dot,
e.getX() - DOT_SIZE/2,e.getY() - DOT_SIZE/2);
}
private static final double DOT_SIZE = 20;private static RandomGenerator rand =
new RandomGenerator();}
CCSC:MW 2009, Saint Xavier University
![Page 38: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/38.jpg)
Responding to mouse events
1. Define an init method that calls addMouseListeners.
2. Write new definitions of any listener methods you need.
The most common mouse events and associated listener methods:
Method When invoked
mouseClicked(e) when the user clicks the mouse
mousePressed(e) when the mouse button is pressed
mouseReleased(e) when the mouse button is released
mouseMoved(e) when the user moves the mouse
mouseDragged(e) when the mouse is dragged with thebutton down
The parameter e is a MouseEvent object, which provides moredata about the event, such as the location of the mouse.
CCSC:MW 2009, Saint Xavier University
![Page 39: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/39.jpg)
Lines
public class Lines extends GraphicsProgram {private GPoint lastPoint;
public void init() {lastPoint = new GPoint(getWidth()/2,getHeight()/2);addMouseListeners();
}
public void mouseClicked(MouseEvent e){add(new GLine(lastPoint.getX(), lastPoint.getY(),
e.getX(), e.getY()));lastPoint.setLocation(e.getX(), e.getY());
}}
CCSC:MW 2009, Saint Xavier University
![Page 40: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/40.jpg)
DragLines
public class DragLines extends GraphicsProgram {public void init() {addMouseListeners();
}
public void mousePressed(MouseEvent e) {line = new GLine(e.getX(), e.getY(),
e.getX(), e.getY());add(line);
}
public void mouseDragged(MouseEvent e) {line.setEndPoint(e.getX(), e.getY());
}
private GLine line;}
CCSC:MW 2009, Saint Xavier University
![Page 41: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/41.jpg)
Exercise: Dragging dots
Write a program that displays randomly colored dots whose size isbased on the amount the mouse is dragged.
CCSC:MW 2009, Saint Xavier University
![Page 42: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/42.jpg)
DragObjects
public class DragObjects extends GraphicsProgram {public void init() {GRect rect = new GRect(100, 100, 150, 100);rect.setFilled(true);rect.setColor(Color.RED);add(rect);GOval oval = new GOval(300, 115, 100, 70);oval.setFilled(true);oval.setColor(Color.GREEN);add(oval);addMouseListeners();
}
public void mousePressed(MouseEvent e) {last = new GPoint(e.getPoint());gobj = getElementAt(last);
}
. . . continues on next slideCCSC:MW 2009, Saint Xavier University
![Page 43: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/43.jpg)
DragObjects
public void mouseDragged(MouseEvent e) {if(gobj != null){gobj.move(e.getX() - last.getX(),
e.getY() - last.getY());last = new GPoint(e.getPoint());
}}
public void mouseClicked(MouseEvent e) {if (gobj != null) gobj.sendToFront();
}
private GObject gobj; /* The object being dragged */private GPoint last; /* The last mouse position */
}
CCSC:MW 2009, Saint Xavier University
![Page 44: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/44.jpg)
Mouse listeners
The ACM Java Libraries simplifies the JFC approach to mouselisteners.
To maximize efficiency, Java defines two distinct listener interfaces.
The MouseListener interface responds to mouse events thathappen relatively infrequently, such as clicking the mouse button.
The MouseMotionListener interface responds to the much morerapid-fire events that occur when you move or drag the mouse.
CCSC:MW 2009, Saint Xavier University
![Page 45: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/45.jpg)
GImage
The ACM Library makes displaying images very easy and you canstill use all of the other features of Swing.
import acm.graphics.*;import acm.program.*;import javax.swing.*;
public class ShowPicture extends GraphicsProgram {
public void run() {JFileChooser chooser = new JFileChooser();chooser.showOpenDialog(null);String filename = chooser.getSelectedFile().getPath();GImage image = new GImage(filename);setSize((int) image.getWidth(),
(int) image.getHeight());add(image);
}}
CCSC:MW 2009, Saint Xavier University
![Page 46: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/46.jpg)
Animation
The pause method allows for simple animations.
double windowWidth = getWidth();double windowHeight = getHeight();
// calculate ball size and stepsdouble size = 0.06 * windowWidth;double dx = size / 8.0;double dy = size / 8.0;
. . . continues on next slide
CCSC:MW 2009, Saint Xavier University
![Page 47: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/47.jpg)
Animation
GOval ball = new GOval(windowWidth/2.0,
windowHeight/2.0, size, size);
ball.setColor(Color.RED);
ball.setFilled(true); ball.setFillColor(Color.ORANGE);
add(ball);
int count = 0;
while (count < BOUNCES) {
if ((ball.getX() + size > windowWidth) ||
(ball.getX() <= 0.0)) {
dx = -dx;
count++;
}
if ((ball.getY() + size > windowHeight) ||
(ball.getY() <= 0.0)) {
dy = -dy;
count++;
}
ball.move(dx, dy);
pause(PAUSE_TIME);
}
CCSC:MW 2009, Saint Xavier University
![Page 48: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/48.jpg)
Animation
Modify the bouncing ball demo to bounce an image around thescreen.
CCSC:MW 2009, Saint Xavier University
![Page 49: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/49.jpg)
GPolygon
The GPolygon class can be used to represent closed polygonalshapes.
Initially, a GPolygon has no vertices. Vertices are added via thefollowing methods:
addArc(arcWidth, arcHeight,
start, sweep)
Adds a series of edges to simulatean arc
addEdge(dx, dy) Adds an edge using the given dis-placement from the last vertex
addPolarEdge(r, theta) Adds an edge using the given polarcoordinates
addVertex(x, y) Adds a vertex relative to the polygonorigin
The coordinates of vertices are expressed with respect to the originof the polygon (its location on a canvas).
GPolygons are resizable.
CCSC:MW 2009, Saint Xavier University
![Page 50: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/50.jpg)
Drawing a diamond
public class Diamond extends GraphicsProgram {
public void run() {GPolygon diamond = new GPolygon();diamond.addVertex(-30, 0);diamond.addVertex(0, 40);diamond.addVertex(30, 0);diamond.addVertex(0, -40);diamond.setFilled(true);diamond.setFillColor(Color.BLUE);add(diamond, getWidth()/2, getHeight()/2);
}}
CCSC:MW 2009, Saint Xavier University
![Page 51: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/51.jpg)
Exercise: Diamonds
Create a program that draws randomly sized and colored diamondswhere clicked.
CCSC:MW 2009, Saint Xavier University
![Page 52: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/52.jpg)
GCompound
GCompound objects are like canvases, you can add other GObjectsto them.
Some of GCompound’s methods:
add(gobj) Adds an object to this GCompound
add(gobj, x, y) Adds an object at the point (x, y)
markAsComplete() Makes it illegal to add or remove elements
remove(gobj) Removes an object
removeAll() Removes all objects
The coordinates of vertices are expressed with respect to the originof the compound object (its location on a canvas).
GCompound objects are resizable.
CCSC:MW 2009, Saint Xavier University
![Page 53: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/53.jpg)
Make a face as a compound object
CCSC:MW 2009, Saint Xavier University
![Page 54: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/54.jpg)
Exercise: Draw a house as a compound object
CCSC:MW 2009, Saint Xavier University
![Page 55: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/55.jpg)
Turtles
The GTurtle class enables graphics using a LOGO-like model of aturtle moving across the screen leaving a trail.
forward(distance) Moves the turtle forward
hideTurtle() Hides the turtle
left(angle) Turns the specified number of degrees to the left
penDown() Lowers the pen
penUp() Raises the pen
right(angle) Turns the specified number of degrees to the left
setDirection(dir) Sets the direction in which the turtle is moving
setLocation(x, y) Moves to the point (x, y) without drawing a line
setSpeed(speed) Sets the speed of the turtle, 0 (slowest) and 1(fastest)
showTurtle() Makes the turtle visible
CCSC:MW 2009, Saint Xavier University
![Page 56: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/56.jpg)
Fractals
GTurtle can be very useful for many types of graphics, especiallyfractals.
The order 0 Koch snowflake is an equilateral triangle. The order nKoch snowflake replaces each line of the order n− 1 snowflakewith the following:
CCSC:MW 2009, Saint Xavier University
![Page 57: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/57.jpg)
Fractals
CCSC:MW 2009, Saint Xavier University
![Page 58: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/58.jpg)
Snowflakes
public class Koch_1 extends GraphicsProgram {
private GTurtle turtle = new GTurtle();private int order = 4;private double length = 300;
public void init() {setSize(600, 600);add(turtle, (getWidth() - length) / 2,
(getHeight() - length) / 2);turtle.setSpeed(0.8);turtle.penDown();
}
. . . continues on next slide
CCSC:MW 2009, Saint Xavier University
![Page 59: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/59.jpg)
Snowflakes
private void zig(int order, double length) {if (order == 0) turtle.forward(length);else {zig(order - 1, length / 3.0);turtle.left(60);zig(order - 1, length / 3.0);turtle.right(120);zig(order - 1, length / 3.0);turtle.left(60);zig(order - 1, length / 3.0);
}}
. . . continues on next slide
CCSC:MW 2009, Saint Xavier University
![Page 60: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/60.jpg)
Snowflakes
public void run() {zig(order, length);turtle.right(120);zig(order, length);turtle.right(120);zig(order, length);turtle.right(120);
}}
CCSC:MW 2009, Saint Xavier University
![Page 61: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/61.jpg)
Exercise: Fractal
Make another fractal where the order 0 case is a square and theorder n case replaces each line of the order n− 1 case with thefollowing:
Note that there are 8 segments in the production rule.
CCSC:MW 2009, Saint Xavier University
![Page 62: The ACM Java Libraries - appspot.comohse28.appspot.com/files/acmJavaLibraries.pdf · Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary](https://reader035.fdocuments.in/reader035/viewer/2022062402/5ec9c276178b1d3a601cf480/html5/thumbnails/62.jpg)
Fractals
CCSC:MW 2009, Saint Xavier University