Javascript required
Skip to content Skip to sidebar Skip to footer

Drawing a Clear Line Java

Up Next
Go up to Drawing Methods
Go forward to Drawing Text

Drawing Lines

                      pen.drawLine(                          startX, startY, endX, endY,              drawingColor            )  pen.clearLine(                          startX, startY, endX, endY)  pen.invertLine(                          startX, startY, endX, endY)                  

The simplest of the drawing methods are used to draw straight lines in the applet. The basic line drawing method is named "drawLine". In addition, there is a "clearLine" method which can be used to erase a line (i.e. draw the line in the background color) and an "invertLine" method that reverses the colors of the pixels that form a line. As you will see, this variety of methods is common to all of the drawing primitives JavaTools provides.

To draw a line, you must specify its two end-points. This is done by providing four numbers as parameters. The first pair of parameter values is used as the x and y coordinates of one end-point. The second pair is used as the coordinates of the other end-point. Thus, an instruction of the form:

pen.drawLine(20,30,100,90);
would cause the browser to draw a line between the points (20,30) and (100,90) in the applet's rectangle within the browser window.

By default, the lines drawn by "drawLine" are drawn using the current foreground color. The "drawLine" method will accept one optional parameter describing a color that should be used instead of the foreground color when drawing the line specified by the first four parameters. For example, the line above could be drawn in blue using the instruction:

pen.drawLine(20,30,100,90,"Blue");
The color parameter to "drawLine" can also be specified using a color constructor method as described in the general discussion of drawing colors. Thus, another way to draw a blue line would be to issue the instruction:
pen.drawLine(20,30,100,90, new Color(0,0,255));

To clarify the behavior of the drawLine primitive, an applet is included below. If you draw a line on the rectangular region on the right side of the applet, the drawLine invocation that would produce the line is shown in the text area at the bottom of the applet. You can also use the menu on the right side of the applet to change the color of the line drawn and see how that color would be specified as a parameter to an invocation of drawLine.

WHOOPS. Your applet does not appear to support Java

The "clearLine" primitive is equivalent to "drawLine" except it draws the line specified using the current background color rather than the foreground color. The "clearLine" method will not accept a color as an optional fifth parameter.

Finally, "invertLine" can be used to reverse the color of the pixels that form a line. The main property of this "reversal" is that it is "reversible". That is, if you invert the same line twice, the display is left as it was before the two invocations of the "invertLine" primitive. This is useful if you want to temporarily add a line to the display. If you added the line using "drawLine" and then removed it with "clearLine", any other lines or objects that the line crossed would be partially erased by the "clearLine". When "invertLine" is used, however, the second "invertLine" restores all the pixels the line crossed to their original color.

To illustrate the use of "invertLine" (and the "invert" methods associated with other forms of drawing), an applet is included below. If you depress the mouse button while pointing within the left half of the applet display and then drag the mouse about, the applet draws a "rubber band" line that follows the mouse. That is, each time you move the mouse it draws a line from the point where the mouse button was first depressed to the current position of the mouse and erases the line drawn the last time the mouse was moved. The effect is that the line follows the mouse. When you finally release the mouse it draws a line between the point where the mouse was depressed and the point where it was released. In addition, the applet draws a circle in the middle of the applet's drawing area.

WHOOPS. Your applet does not appear to support Java

By default, the applet draws and erases the rubber band lines using "invertLine". As a result, if you drag the mouse so that the line overlaps the circle in the center of the screen, you will notice that where the line overlaps the circle, it is white rather than black. Also, as the mouse moves so that the lines drawn pass through the circle, notice that portions of the cirlce that had been white when a line was drawn through them are restored to black.

You can use the "Mode" menu on the right side of the applet to change its behavior so that it produces the rubber band lines by drawing lines with "drawLine" and erasing them with "clearLine". Now, you will notice that lines that overlap the circle are drawn in black. As lines are erased when the mouse moves, however, portions of the circle are permanently erased too.


Up Next

Drawing a Clear Line Java

Source: http://www.cs.williams.edu/~tom/courses/105/labs/javatools/javaTools_7.html