home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Summary

Using Java’s graphics capabilities will make your programs far more professional- looking than just putting some controls in the window. A program that uses graphics communicates far better than one that simply uses text. It also shows that you’re willing to take the time to make your program look great.

Java provides many methods with which you can draw. Many of them were presented in this chapter. Although there are some other methods in the Graphics class, what you learned in this chapter will cover 99% of all of your needs. Learn these methods well!

Q&A

Q Are there other ways to obtain a Graphics class in an applet besides waiting for one to be passed to your paint() method?

A Yes. You can use the getGraphics() method. The problem with drawing outside of the paint() or onPaint() methods is that screen refreshes will redraw only code that’s invoked from the paint() or onPaint() methods.

Q How do you draw lines?

A The easiest way is with the drawLine() method. It takes four arguments. The first two are the starting coordinates of the line, and the next two are the ending coordinates of the line. The line is drawn with the currently selected color.

Q Are there different methods that draw lines?

A Yes, the drawLines() and the drawLineTo() methods can be used. The drawLines() method draws multiple lines. The drawLineTo() method draws a single line from the currently set draw coordinate to another specified point.

Q What kinds of rectangles can you draw?

A There are three main types: a plain rectangle, a rounded rectangle, and a 3D rectangle. You can draw a plain rectangle with the drawRect() method. You can draw a rounded rectangle with the drawRoundRect() or drawRoundedRect() methods. You can draw a 3D rectangle with the draw3DRect() method.

Q How can you draw polygons?

A The easiest way is with the drawPolygon() method. It takes as an argument an array of points. These points are then used to draw a polygon.

Q Are ovals difficult to draw?

A No more difficult than other shapes. The thing to remember about an oval is that you specify a bounding rectangle for it. Imagine a rectangle into which the oval will fit. Use the x,y coordinate of the rectangle’s upper-left corner for the first two arguments of the drawOval() method. Then get the width and height of the rectangle into which your oval fits. This gives you the third and fourth arguments for the drawOval() method.

Q Why do you need a Graphics class to draw?

A The Graphics class provides the draw methods with a context in which it will paint. It’s important that all painting be done in light of the context in which the operations will be performed. In this way, the paint methods can be used in varying contexts, such as windows, printers, and other devices.

Q Why isn’t a method to draw circles provided?

A The drawOval() method easily accomplishes the same thing. And there isn’t a native circle-drawing function in the Windows API, so nothing would map to any API calls.

Q Why is it important to draw to a Visual J++ program’s window? Why not just use text and user-interface controls?

A Drawing can communicate much more than text and user-interface controls. For instance, you can draw graphics and charts and can illustrate ideas.

Review Exercises

1.  Create an applet. Using a for loop, draw lines across the window from top to bottom with one color. After you’ve done this, go back and change the color each time through the loop.
2.  Draw a picture of a bus with the Java drawing commands. Try to use mostly lines, rectangles, and ovals.
3.  Create an application that draws text inside of drawn rectangles. Center the text in the rectangles. Then add some ovals with text centered in them. Refer to Day 10, “Fonts and Text,” to refresh yourself on using the TextMetrics class to center a text string.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.