As you know, a class provides the blueprint for objects ; you create an object from a class. Each of the following statements taken from the CreateObjectDemo program creates an object and assigns it to a variable:Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100);The first line creates an object from the Point class and the second and third lines each create an object from the Rectangle class. From the Variables section in the previous lesson, you learned that to declare a variable, you write:type nameThis notifies the compiler that you will use name to refer to data whose type is type. The Java programming language divides variable types into two main categories: primitive types, and reference types. The new operator instantiates a class by allocating memory for a new object. Here's the code for the Point class:public class Point { public int x = 0; public int y = 0; //A constructor! public Point(int x, int y) { this.x = x; this.y = y; } }This class contains a single constructor. You can recognize a constructor because it has the same name as the class and has no return type. The constructor in the Point class takes two integer arguments, as declared by the code (int x, int y). The following statement provides 23 and 94 as values for those arguments:Point originOne = new Point(23, 94);The effect of the previous line of code can be illustrated in the next figure: Here's the code for the Rectangle class, which contains four constructors:public class Rectangle { public int width = 0; public int height = 0; public Point origin; //Four constructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; } public Rectangle(int w, int h) { this(new Point(0, 0), w, h); } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } //A method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } //A method for computing the area of the rectangle public int area() { return width * height; } }Each constructor lets you provide initial values for different aspects of the rectangle: the origin; the width, and the height; all three; or none. If a class has multiple constructors, they all have the same name but a different number of arguments or different typed arguments. The Java platform differentiates the constructors based on the number and the type of the arguments. When the Java platform encounters the following code, it knows to call the constructor in the Rectangle class that requires a Point argument followed by two integer arguments:Rectangle rectOne = new Rectangle(originOne, 100, 200);This call initializes the rectangle's origin variable to the Point object referred to by originOne. The code also sets width to 100 and height to 200. Now there are two references to the same Point object; an object can have multiple references to it, as shown in the next figure: Page 2A typical Java program creates many objects, which as you know, interact by sending messages . Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects. Page 3Next, this chapter explains how to use the core classes and built-in support for character data, numeric data, and arrays. It covers the following topics:
Page 4This trail covers the fundamentals of programming in the Java programming language. Page 5 Page 6The JavaTM Tutorial
This trail introduces you to the Java 2D API and shows you how to display and print 2D graphics in your Java programs. The Java 2D API enables you to easily
The Java 2D API also enables you to store and to manipulate image data--for example, you can easily perform image-filter operations, such as blur and sharpen, as shown in the following figure. This trail covers the most common uses of the Java 2D APIs and briefly describes some of the more advanced features. For additional information about using the Java 2D APIs, see the Java 2D Programmer's Guide . Additional sample programs illustrating the Java 2D API features are also available online.Note: The sample applets in this trail can be run with the JDK 1.2 Applet Viewer, a browser with Java Plug-in 1.2 installed, or a JDK 1.2 compatible browser.The Java 2D APIs are closely integrated with the Abstract Windowing Toolkit (AWT). If you are not familiar with AWT, you might find it useful to review the AWT documentation, which you can download. Overview of the Java 2D API introduces the key Java 2D concepts and describes the Java 2D rendering model. Displaying Graphics with Graphics2D teaches you how to set up the Graphics2D rendering context to use fancy stroke and fill styles, perform transformations, clip the drawing region, composite overlapping graphics, and specify rendering preferences. Working with Text and Fonts shows you how to use a Font object to create a font with desired attributes, and to derive a new font by changing the attributes, determine the names of the fonts that are available on your system and position text within a component. Manipulating and Displaying Images his lesson explains how to implement double buffering and how to perform image-filter operations with BufferedImage objects. Printing teaches you how to render 2D graphics to a printer and how to print complex documents. Solving Common 2D Graphics Problems gives the solutions to some problems you might encounter when writing 2D applets and applications.
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved. |