Legal Terms Examplets SM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan... }; } } Examplets TM provided by permission of the publisher, Addision-Wesle
Trang 1"Examplets" are code snippets that illustrate a particular task.
They're designed to show which classes and members areinvolved in performing the task, and how the classes andmembers interact with each other You will see blue italicizedtext in an examplet; this indicates code that should be replaced
if you use it in your program
An examplet can have enough detail for you to use it, as is Oryou might have to get more information about the involvedclasses A good place to find information about the Java 2Platform APIs is the Java 2 Platform API index
It's also good practice to add catch clauses, as appropriate, tothe examplets If the code of an examplet throws a checkedexception, the code is surrounded by a try/catch statement
However, to reduce space and distraction, the catch clause isleft blank Catch clauses should always handle the exceptionand should never be left blank So, at the very least, youshould include a call to e.printStackTrace() to display the exception
on the console
You'll find examplets in a variety of categories See the Table ofContents for a list of categories, and the examplets in eachcategory
Table of Contents
Trang 2CompressionJAR filesDatabase - JDBCData Structures and AlgorithmsFiles, Streams, I/O
ImagesInternationization
JNDIDirectoryEventLdapMathNetworkingPrintingReference ObjectsReflection
RMISecuritySoundSampledMidiStringsSwingEventsFile ChooserLayout
TreeTextThreadsTimejavax.rmijavax.swing.undo
(java.util.jar)(java.sql)(java.util)(java.io)(java.awt.image)(java.text)
(java.beans)(javax.naming)(javax.naming.directory)(javax.naming.event)(javax.naming.ldap)(java.math)
(java.net)(java.awt.print)(java.lang.ref)(java.lang.reflect)(java.rmi)
(java.security)
(javax.sound.sampled)(javax.sound.midi)(java.lang)
(javax.swing)(javax.swing.event)(javax.swing.filechooser)(java.awt and javax.swing)(javax.swing.tree)
(javax.swing.text)(java.lang)
(java.lang)(javax.naming)(javax.naming.directory)(javax.naming.event)(javax.naming.ldap)(javax.rmi)
(javax.swing.undo)
http://developer.java.sun.com/developer/codesamples/ (2 of 13) [8/1/2000 7:46:26 AM]
Trang 3Setting a Keyboard Accelerator for a Menu Item
Applets (java.applet)
The Quintessential AppletGetting an Applet ParameterMaking the Browser Visit a URLShowing a Message in the Browser's Status BarLoading and Playing Audio in an Applet
Loading and Playing Audio in an ApplicationLoading and Painting an Image in an AppletAnimating an Array of Images in an Applet
AWT (Abstract Windowing Toolkit) (java.awt)
The Quintessential Drawing ProgramDrawing Basic Shapes
Filling Basic ShapesLoading an ImageDrawing an ImageScaling, Shearing, Translating, and Rotating a DrawnImage
Drawing Simple TextDrawing Rotated TextGetting the Dimensions of TextDrawing Anti-Aliased Text and GraphicsDrawing with Alpha
Setting the Clipping Area with a ShapeChanging the Thickness of the Stroking Pen
Trang 4Fonts (java.awt.font)
Listing All Available FontsDrawing a Paragraph of TextGetting the Shape from the Outline of TextDrawing Text with Mixed Styles
Shapes (java.awt.geom)
Creating a Shape with Lines and CurvesCreating Basic Shapes
Combining ShapesScaling, Shearing, Translating, and Rotating a Shape
Compression (java.util.zip)
Compressing a FileDecompressing a FileListing the Contents of a ZIP FileCalculating the Checksum of a Byte Array
JAR files (java.util.jar)
Retrieving the Manifest of a JAR File
Database - JDBC (java.sql)
http://developer.java.sun.com/developer/codesamples/ (4 of 13) [8/1/2000 7:46:26 AM]
Trang 5Creating a ListCreating a Hash TableTruncating a List
Converting a Collection to an ArrayConverting an Array to a CollectionImplementing a Queue
Implementing a StackIterating the Elements of a CollectionSorting an Array
Finding an Element in a Sorted ArrayFinding an Element in a Sorted ListInserting an Element into a Sorted ListGenerating a Random Number
Breaking a String into WordsReading Properties from a Properties FileCreating Date Object From a Particular DateParsing a Date Using a Pattern
Reading and Writing a Properties FileGetting and Setting Properties
Scheduling a Timer Task to Run at a Certain TimeScheduling a Timer Task to Run Repeatedly
Files, Streams, I/O (java.io)
Trang 6Listing the File System RootsSetting File Modification TimeSerializing an Object
Deserializing an ObjectTraversing a DirectoryReading UTF-8 Encoded DataWriting UTF-8 Encoded DataReading ISO Latin-1 Encoded DataWriting ISO Latin-1 Encoded Data
Images (java.awt.image)
Converting an Image to a Buffered ImageGetting Pixels from a Buffered ImageGetting a Sub-Image of an ImageCreating and Drawing on a Buffered ImageScaling, Shearing, Translating, and Rotating an ImageConverting a Color Image to Gray
Blurring an ImageSharpening an ImageEmbossing an Image
Internationization (java.text)
Formatting and Parsing a Locale-specific NumberFormatting and Parsing Locale-specific CurrencyFormatting and Parsing a Locale-specific PercentageFormatting and Parsing a Locale-specific Date
Formatting and Parsing Locale-specific TimeFormatting and Parsing a Locale-specific Date and TimeDetermining the Type of a Character
Comparing Strings in a Locale-independent WayDetermining the Character Boundaries in a Unicode StringDetermining the Word Boundaries in a Unicode StringDetermining the Sentence Boundaries in a Unicode String
http://developer.java.sun.com/developer/codesamples/ (6 of 13) [8/1/2000 7:46:26 AM]
Trang 7Math (java.math)
Operating With Big Integer ValuesOperating With Big Decimal ValuesSetting the Decimal Place of a Big Decimal Value
Networking (java.net)
Creating a URLParsing a URLReading Text from a URLCalling a CGI Using POST MethodResolving a Hostname
Creating a Client SocketCreating a Server SocketReading Text from a SocketWriting Text to a SocketSending a DatagramReceiving a DatagramJoining a Multicast GroupReceiving from a Multicast GroupSending to a Multicast Group
Printing (java.awt.print)
The Quintessential Printing ProgramGetting the Dimensions of a Printed PageSetting the Orientation of a Printed PagePrinting Pages with Different FormatsDisplaying the Page Format Dialog
Trang 8Invoking a Method Using a Method ObjectGetting the Modifiers of a Member ObjectGetting the Name of a Class or Member ObjectDetermining if an Object Is an Array
Determining the Dimensions of an ArrayGetting the Component Type of an ArrayCreating an Array
Expanding an ArrayGetting and Setting the Value of an Element in an ArrayOverriding Default Access
Creating a Proxy Object
RMI (java.rmi)
Starting Up the RMI RegistryDefining and Exporting a Remote ObjectLooking Up a Remote Object and Invoking a MethodPassing Parameters to a Remote Method
Returning Values from a Remote MethodThrowing an Exception from a Remote Method
Sound Sampled (javax.sound.sampled)
Loading and Playing Sampled AudioDetermining the File Format of a Sampled Audio FileDetermining the Encoding of a Sampled Audio FileDetermining the Duration of a Sampled Audio FilePlaying Streaming Sampled Audio
Continuously Playing a Sampled Audio FileDetermining the Position of a Sampled Audio PlayerDetermining When a Sampled Audio Player hasFinished Playing
Controlling the Volume of Playing Sampled Audio
Midi
http://developer.java.sun.com/developer/codesamples/ (8 of 13) [8/1/2000 7:46:26 AM]
Trang 9Creating a New Key Pair and Self-signed CertificateCreating and Signing a JAR File
Creating a New Policy FileManaging Policy FilesCreating a Keyed Digest Using MD5Getting a Certificate from a KeystoreChecking Read/Write Permission for a DirectoryChecking if One Permission Implies AnotherCreating a Secure Random Number
Creating Image Radio ButtonsRetrieving the Selected Button in a GroupFiring Item Events
Creating a File Chooser DialogDisplaying Only Directories in a File Chooser DialogCreating an Internal Frame
Creating a List ComponentCreating a Scrolling List ComponentSetting the Selection Mode of a List Component
Trang 10Showing a Dialog BoxCreating Key Strokes and Binding Them to ActionsAdding an InputMap to a Component
Setting a Tool TipLaying Out Components in a Row or ColumnSeparating Components in a Row or ColumnLaying Out Components in a Flow (Left-to-Right,Top-to-Bottom)
Laying Out Components in a GridLaying Out Components Using Absolute CoordinatesSetting a Border
Adding a Title to a BorderCreating a Compound BorderDetermining the Available Look and FeelsSetting the Look and Feel
Painting the Background of a Container with an ImagePattern
Events (javax.swing.event)
Handling Hyperlink EventsHandling Changes to a List Component
File Chooser (javax.swing.filechooser)
Adding a Filter to a File Chooser Dialog
Tree (javax.swing.tree)
Creating a TreeHandling Selection Events in a Tree Component
Text (javax.swing.text)
Inserting Styled Text in a Text PaneInserting an Image into a Text PaneInserting a Component into a Text Pane
http://developer.java.sun.com/developer/codesamples/ (10 of 13) [8/1/2000 7:46:26 AM]
Trang 11Replacing Substrings in a StringConverting a String to Upper or Lower CaseConverting a String to a Number
Shifting Elements in an ArrayCopying Elements from One Array to AnotherRetrieving the SuperClass of an Object
Getting the Package of an ObjectImplementing a Work QueueExecutes a Command
Reading Output from a CommandSending Input to a CommandPausing
Getting the Current TimeGetting the Value of a System PropertySetting the Value of a System PropertyUsing a Thread-local Variable
Loading Native CodeConverting Unicode to UTF-8Converting UTF-8 to UnicodeDetermining a Character's Unicode Block
JNDI
Creating an Initial Context to the Naming ServiceLooking Up an Object From the Naming ServiceListing a Context in the Naming Service
Adding, Replacing, Removing, and Renaming a Binding inthe Naming Service
Trang 12Getting an Object's Schema from the DirectoryGetting an Attribute's Schema from the DirectoryAuthenticating to the Directory
Performing an LDAP ``Extended'' Operation
javax.rmi
Starting the Name ServerDefining and Exporting a Portable Remote ObjectLooking Up a Portable Remote Object and Invoking aMethod
javax.swing.undo
Adding Undo and Redo to a Text Component
These code examples and other materials are subject to Sun Microsystems, Inc Legal Terms
Examplets SM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
[ This page was updated: 31-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies
http://developer.java.sun.com/developer/codesamples/ (12 of 13) [8/1/2000 7:46:26 AM]
Trang 13The Quintessential Accessible Object
You should try to subclass from JComponent or one of itsdescendents since that will automatically make your objectaccessible If that is not possible, your object should implementAccessible
import javax.accessibility.*;
public class BasicAccessible implements Accessible { public AccessibleContext getAccessibleContext() { return new AccessibleContext() {
// Implement all the abstract methods in this // abstract class
};
} }
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
Trang 14Setting an Accessible Name for an Image Button
You should also set the accessible name for components that onlyshow an image The tool tip text, if set, serves as the accessiblename for a component However, if the tool tip text is being used forsomething else, set the component's accessible name
// If tool tip is being used for something else, // set the accessible name
button.getAccessibleContext().setAccessibleName(
"Button Name");
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/javax.accessibility/175.html [8/1/2000 7:46:39 AM]
Trang 15Setting a Description for Image Icons
Image icons can be inserted in a variety of places such as in a text
or tree component You should set a description for image icons tohelp blind users
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
Trang 16Setting a Mnemomic for Buttons
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/javax.accessibility/177.html [8/1/2000 7:46:41 AM]
Trang 17Setting a Mnemonic for a Menu
At least one menu in a menu bar should have a mnemonic Thismakes all the menus and menu items accessible
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
Trang 18Associating a Label with a Component
When a label is associated with a component, you should callsetLabelFor() to make the association explicit and then set amnemonic on the label The associated component will get thefocus when the mnemonic is activated
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/javax.accessibility/179.html [8/1/2000 7:46:43 AM]
Trang 19Setting a Keyboard Accelerator for a Menu Item
At least one menu in a menu bar should have a mnemonic Thismakes all the menus and menu items accessible
//The next two lines should be in one line
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
Trang 20The Quintessential Applet
Every applet must subclass Applet
import java.applet.*;
import java.awt.*;
public void init() { // Called once by the browser when // it starts the applet
} public void start() { // Called whenever the page containing // this applet is made visible
} public void stop() { // Called whenever the page containing this // applet is not visible
} public void destroyed() { // Called once when the browser destroys // this applet
} public void paint(Graphics g) { // Called whenever this applet needs to // repaint itself
} }
This is the applet's accompanying HTML file
Trang 21Getting an Applet Parameter
Here is a sample HTML file containing a value for the parameter
\meta{p}
<param name=p value="some text">
</applet>
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
Trang 22Making the Browser Visit a URL
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.applet/2.html [8/1/2000 7:46:47 AM]
Trang 23Showing a Message in the Browser's Status Bar
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
Trang 24Loading and Playing Audio in an Applet
// The following 2 lines of code must be // on the same line
AudioClip ac = getAudioClip(getDocumentBase(), "http://hostname/audio.au");
ac.play();
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.applet/4.html [8/1/2000 7:46:49 AM]
Trang 25Loading and Playing Audio in an Application
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
Trang 26Loading and Painting an Image in an Applet
}Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.applet/6.html [8/1/2000 7:46:52 AM]
Trang 27Animating an Array of Images in an Applet
This is the simplest applet to animate an array of images Inpractice, you should use double-buffering (which this example doesnot use) to eliminate ``flickering.''
images[1] = getImage(getDocumentBase(),
} public void start() { (thread = new Thread(this)).start();
} public void stop() { thread = null;
} public void paint(Graphics g) {
Trang 28[ This page was updated: 31-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.applet/7.html (2 of 2) [8/1/2000 7:46:56 AM]
Trang 29The Quintessential Drawing Program
This example creates a frame and draws an oval within the frame
import java.awt.*;
import javax.swing.*;
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D)g;
getSize().height-1);
} public static void main(String[] args) { JFrame frame = new JFrame();
Order this book from Amazon
Trang 30Drawing Basic Shapes
There are two ways to draw basic shapes like lines and rectangles.The first is to use specific drawing methods like
Graphics.drawOval() This example uses these methods Thesecond is to construct a shape and then use Graphics2D.draw() todraw the shape See the java.awt.geom package for examples thatcreate shapes
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.awt/9.html [8/1/2000 7:46:58 AM]
Trang 31Filling Basic Shapes
There are two ways to fill basic shapes like lines and rectangles.The first is to use specific drawing methods like Graphics.fillOval().This example uses these methods The second is to construct ashape and then use Graphics2D.fill() to fill the shape See thejava.awt.geom package for examples that create shapes
Polygon polygon = new Polygon();
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
Trang 32Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.awt/11.html [8/1/2000 7:47:00 AM]
Trang 33imageIcon.paintIcon(this, g, x, y);
}Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
Trang 34Scaling, Shearing, Translating, and Rotating a Drawn Image
AffineTransform tx = new AffineTransform();
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.awt/13.html [8/1/2000 7:47:03 AM]
Trang 35Drawing Simple Text
// Set the desired font if different from default font
// Draw the string such that its base line is at x, y
// Draw the string such that the top-left corner // is at x, y
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's Copyright © 1995-2000 Sun Microsystems, Inc.
Trang 36Drawing Rotated Text
// Draw string rotated clockwise 90 degrees
AffineTransform at = new AffineTransform();
at.setToRotation(Math.PI/2.0);
// Draw string rotated counter-clockwise 90 degrees
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.awt/15.html [8/1/2000 7:47:05 AM]
Trang 37Getting the Dimensions of Text
// From within the paint() method
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D)g;
FontMetrics fontMetrics = g2d.getFontMetrics();
int height = fontMetrics.getHeight();
} // From within a component
MyComponent() {
FontMetrics fontMetrics = getFontMetrics(font);
int height = fontMetrics.getHeight();
} }Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Trang 38Drawing Anti-Aliased Text and Graphics
// Text only
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// Text and graphics
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawGraphics(g2d);
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.awt/17.html [8/1/2000 7:47:07 AM]
Trang 39Drawing with Alpha
drawGraphic1(g2d);
// Set alpha 0.0f is 100% transparent and 1.0f is // 100% opaque
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
Trang 40Setting the Clipping Area with a Shape
drawGraphics(g2d);
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved Terms of Use Privacy Policy
http://developer.java.sun.com/developer/codesamples/java.awt/19.html [8/1/2000 7:47:10 AM]