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
Day 11 Drawing
Its easy to get an application or an applet up and running with Visual J++. All you have to do is create a startup program and put some controls in the Java window, and you can have a program that performs useful functions.
But before long youll outgrow this capability. Youll want to make your programs fancy; youll want to make them look better; youll want them to communicate more than what your text controls can. Today youll learn how to use the Java drawing commands to make your programs look great.
In this chapter, you will do the following:
- Learn how to draw these items:
- Lines
- Rectangles
- Ovals
- Arcs
- Polygons
- See a demonstration program that uses these graphics techniques
- Learn how to copy areas in Java windows
- Learn how to clear your programs window
After youve mastered all these techniques, your programs will take on a new dimension. Theyll start to look professional, unlike some of the amateur and shareware programs you currently see out there. With the drawing commands youll learn today, your programs will sparkle.
The Graphics Class
In the preceding chapter, we covered drawing fonts to Java windows. We used the Graphics class and two of its member functions to perform this task. The two methods we used were drawString() and drawChars().
We got the Graphics class in an applets paint() method or an applications onPaint() method. If youre writing an applet, its possible, though, to get a Graphics class for a component in another method besides the paint() method. You can use the getGraphics() method to obtain a Graphics class with which you can do all of your drawing.
The following line of source code shows you how to get a Graphics class in an applet:
Graphics g = getGraphics();
Note: When you draw in the paint() method, youll already have a Graphics class and wont have to use the getGraphics() method. When you draw elsewhere, youll have to get a Graphics class with the getGraphics() method.
So now, if youre in a method that doesnt have anything to do with a paint() method, you can go ahead and get a Graphics class and draw to a Java window. The only problem is that when the applet redraws or refreshes in response to some sort of a window move or window message, whatever you drew to the window that isnt in the paint() method will not be redrawn.
In what situation would you want to get a Graphics class outside of the paint() method and paint to the window? One example might be when you want to temporarily draw a small figure to a large window. Youd draw the figure to the program window, and then when you wanted it to go away, you could call repaint().
Lines
You can draw a line with the Graphics class drawLine() method. This method takes four arguments. The first and second arguments are the x and y coordinates of the starting point of your line; the third and fourth arguments are the x and y coordinates of the ending point of your line.
The following example draws a line in an applets paint() method:
public void paint(Graphics g)
{
// Draw from (25, 30) to (160, 170)
g.drawLine( 25, 30, 160, 270 );
}
When you draw a line in an applications onPaint() method, your source code will look slightly different from that in the preceding example. Thats because you dont get a Graphics class in an onPaint() method; you get a PaintEvent class. Of course, the PaintEvent class has a Graphics class as one of its member variables.
The following example shows the source code for drawing a simple line in a Visual J++ applications onPaint() method. Figure 11.1 shows what this application looks like when it runs.
protected void onPaint(PaintEvent pe)
{
pe.graphics.drawLine( 25, 30, 160, 270 );
}
Figure 11.1 This simple Java application draws a line in the onPaint() method.
One of the problems with the drawLine() method is that it draws a line thats only a single pixel wide. Many times when youre drawing, you want wide lineslines that have widths of three, four, five, six, and more pixels. One of the ways Ive solved this dilemma is to write special functions that draw wide lines. Diagonal lines are slightly more difficult. The best way to accomplish this job is by drawing a polygon that simulates a thick lineIll show you how after talking about polygons later. As an example, I have two methods to show you. One is called drawHorizontalLine(); the other is called drawVerticalLine(). They draw horizontal and vertical lines of any thickness. The source code is shown in Listing 11.1.
Listing 11.1 Methods to Draw Thick Horizontal and Vertical Lines
1 public void drawHorizontalLine( Graphics g, int x1, int y1,
2 int x2, int y2, int nThickness )
3 {
4 for( int i=0; i<nThickness; i++ )
5 g.drawLine( x1, y1 + i, x2, y2 + i );
6 }
7
8 public void drawVerticalLine( Graphics g, int x1, int y1,
9 int x2, int y2, int nThickness )
10 {
11 for( int i=0; i<nThickness; i++ )
12 g.drawLine( x1 + i, y1, x2 + i, y2 );
13 }
Before you draw lines, youll also probably want to set the color. You do this the same way as you did when you drew text to the Java window. The first thing you do is either create a color object of your own or use one of the predefined Color classes, such as Color.white or Color.black. You then select your color object into the Graphics class. Its just like drawing with text: from that point on, anytime you draw to the Graphics class, the draw operation will be in that color.
|