Note |
No special command for drawing individual pixels is available. Just use drawLine as shown in Listing 17.9; alternatively, you can use drawRect or fillRect, which are discussed later in this section. |
Listing 17.9. Drawing straight lines, arcs, and a point.
import java.awt.*;
import java.applet.Applet;
public class drawpoint extends Applet{
public void paint(Graphics g)
{
int x_final, y_final;
int i;
//this draws a line
g.drawLine(10,10,100,100);
//this draws a point at 10,30
g.drawLine(10,30,10,30);
//this draws an arc
g.drawArc(50,50,30,30,0,180);
//this draws a filled arc
g.fillArc(50,100,20,40,90,90);
}
}
Here are the full descriptions for the three line-drawing methods.
drawLine(int x_start, int y_start, int x_end, int y_end)
This draws a line between two points.
x_start: Starting x position
y_start: Starting y position
x_end: Final x position
y_end: Final y position
drawArc(int x, int y, int width, int height, int start_angle, int delta_angle)
This routine draws an arc, a segment of an ellipse, or a circle
x: Upper-left corner of the rectangle that contains the ellipse the arc is from.
y: Upper-left corner of the rectangle that contains the ellipse the arc is from.
width: The width of the rectangle that contains the ellipse the arc is from.
height: The height of the rectangle that contains the ellipse the arc is from.
start_angle: The angle at which the arc starts; 0 degrees is at the 3 o'clock position, and the value increases in the counterclockwise direction.fillArc(int x, int y, int width, int height, int start_angle, int delta_angle)
This is the same as drawArc, except that the arc is filled with the current color.One of the key shortcomings with current Java drawing routines is that you can't specify a line width. All the drawing commands draw unit-width lines. If you want to draw thicker lines, you can follow an approach similar to the one shown in Listing 17.10.Listing 17.10. Drawing thicker lines.
public void draw_thick_line(Graphics g, int x_start,
int y_start, int x_final,
int y_final, int width) {
int i;
for (i=0;i<width;i++) {
g.drawLine(x_start+i,y_start + i,x_final+i,y_final+i);
}
}
This method just draws several lines to make a thicker line. You can use the same approach for the shapes you'll see in the next section.
The final way to draw very complex lines is to use the drawPolygon method, which is described in the next section. With it, you can draw arbitrarily complex paths.
No comments:
Post a Comment