The ACM Java Libraries

The ACM Java Libraries

The ACM Java Libraries Andrew Mertz William Slough Nancy Van Cleave Mathematics and Computer Science Department Eastern Illinois University October 10, 2009 CCSC:MW 2009, Saint Xavier University 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 Textbook support for ACM libraries CCSC:MW 2009, Saint Xavier University 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 Installing the necessary software: NetBeans www.netbeans.org/downloads/ Click on the Download NetBeans IDE Button Run install CCSC:MW 2009, Saint Xavier University Installating the necessary software: ACM Java libraries jtf.acm.org | obtain acm.zip Create acmLibrary project in NetBeans 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 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, and select close (don't shut down NetBeans) CCSC:MW 2009, Saint Xavier University The Program class diagram JApplet Program GraphicsProgram ConsoleProgram DialogProgram CCSC:MW 2009, Saint Xavier University 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 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 NewEmpty with HelloProgram Click the Finish button CCSC:MW 2009, Saint Xavier University 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 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 statements with \n to produce a single dialog window CCSC:MW 2009, Saint Xavier University 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 Placing applets on a web site index.html HelloProgram.html CCSC:MW 2009, Saint Xavier University 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 ! readDouble int ! String, readInt ! readLine CCSC:MW 2009, Saint Xavier University The acm.graphics model The acm.graphics package uses a collage model in which you create an image by adding various objects to a canvas. This is similar to a felt board that serves as a backdrop for colored shapes that stick to the felt surface. Note that newer objects can obscure those added earlier. This layering arrangement is called the stacking order or z-order. CCSC:MW 2009, Saint Xavier University The Java coordinate system All distances and coordinates in the graphics library are measured in pixels. Coordinates in the graphics model are specified relative to the origin 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 across the screen and the values for y increase as you move downward. CCSC:MW 2009, Saint Xavier University Partial class diagram for acm.graphics GRect GOval GLabel GLine java.awt.Container GObject GCompound GPolygon GCanvas GTurtle GImage GArc CCSC:MW 2009, Saint Xavier University The GCanvas class The GCanvas class represents the background canvas, a virtual felt board. When you use the acm.graphics package, you create pictures by adding various GObjects to a GCanvas. For simple applications, you will not need to work with an explicit GCanvas object. Programs that extend GraphicsProgram, automatically creates a GCanvas and resizes it so that it fills the program window. Most of the methods defined for the GCanvas class are also available in a GraphicsProgram, thanks to delegation. CCSC:MW 2009, Saint Xavier University Methods in the GCanvas class add(object) Adds the object to the canvas at the front of the stack add(object, x, y) Moves the object to (x, y) and then adds it to 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), or null 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 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 clicks the mouse CCSC:MW 2009, Saint Xavier University 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 internal structure. You use this form when you have already set the coordinates 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 of the object before you know where to put it. If, for example, you want to center a GLabel, you must first create it and then use its size to determine its location. CCSC:MW 2009, Saint Xavier University 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 Methods common to GObjects setLocation(x, y) Resets the location of the object to the specified point 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 specified point CCSC:MW 2009, Saint Xavier University 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 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 that define methods for certain GObject subclasses: GFillable GResizable GScalable CCSC:MW 2009, Saint Xavier University 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 Making a face CCSC:MW 2009, Saint Xavier University Exercise: Drawing a house CCSC:MW 2009, Saint Xavier University Encapsulated coordinates The acm.graphics package defines three classes: GPoint

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    62 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us