|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
SummaryUsing Javas 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 youre 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&AQ 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 thats 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 rectangles 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. Its 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 isnt a method to draw circles provided? A The drawOval() method easily accomplishes the same thing. And there isnt 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++ programs 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
|
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. |