Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Using Graphics
This isn't meant as a knock to those of us who enjoy displaying arrays, incrementing variables, or using a constructor method, but let's face it—many subjects in a programming language such as Java tend to be dry. It's hard to impress your non-programming acquaintances with the way your if-else statement determines which method to use in a mathematical application. Dates don't get nearly as excited as you do when a switch-case block statement handles a variety of different circumstances correctly. Nobody ever made a movie about the ternary operator.
Graphics programming is the exception to this general rule. When you write a program that does something interesting with graphics, it's a way to have fun with a programming language and impress relatives, friends, strangers, and prospective employers.
Drawing things such as lines and polygons is as easy in a Java applet as displaying text. All you need are Graphics and Graphics2D objects to define the drawing surface and objects that represent the things to draw.
The Graphics class stores information required to display something onscreen. The most common use of the class is as an object that represents the area where something can be drawn in a container, such as a panel or an applet window.
In a container such as a JPanel component, a Graphics object is sent to the paintComponent() method as an argument:
public void paintComponent(Graphics comp) {
// ...
}
The same thing happens in the paint() method of an applet:
public void paint(Graphics comp) {
// ...
}
Both of these methods are called automatically whenever the container must be redrawn. For instance, if you have a Java frame open as an application is running and cover it up with another window, the frame's paintComponent() method will be called when the other window is closed.
Inside these paint methods, you should begin by calling the same method in the superclass, as in the following example:
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
}
Next, the Graphics object argument can be used to create a Graphics2D object, as in the following statement:
Graphics2D comp2D = (Graphics2D) comp;
Once you have a Graphics2D object, you draw by calling its methods. This Graphics2D object is called comp2D throughout this book, and its methods are used to draw text with a command such as the following:
comp2D.drawString("Draw, pardner!", 15, 40);
This statement causes the text Draw, pardner! to be displayed at the (x,y) coordinates of (15,40).
All of the shape- and line-drawing methods work using the same (x,y) coordinate system as text. The (0,0) coordinate is at the upper left corner of the container. The x values go up as you head to the right, and y values go up as you head downward. You can determine the maximum (x,y) value you can use in an applet with the following statements:
int maxXValue = getSize().width; int maxYValue = getSize().height;
Drawing Lines and Shapes | Next Section

Account Sign In
View your cart