1、 - 1 - 外文原文 Graphics, Fonts, and Color The Graphics Class With Javas graphics capabilities, you can draw lines, shapes, characters, and images to the screen inside your applet. Most of the graphics operations in Java are methods defined in the Graphics class. You dont have to create an instance of G
2、raphics in order to draw something in your applet;in your applets paint() method (which you learned about yesterday), you are given a Graphics object. By drawing on that object, you draw onto your applet and the results appear on screen.The Graphics class is part of the java.awt package, so if your
3、applet does any painting (as it usually will), make sure you import that class at the beginning of your Java file: import java.awt.Graphics; public class MyClass extended java.applet.Applet . The Graphics Coordinate System To draw an object on the screen, you call one of the drawing methods availabl
4、e in the Graphics class. All the drawing methods have arguments representing endpoints, corners, or starting locations of the object as values in the applets coordinate systemfor example, a line starts at the points 10,10 and ends at the points 20,20. Javas coordinate system has the origin (0,0) in
5、the top left corner. Positive x values are to the right, and positive y values are down. All pixel values are integers; there are no partial or fractional pixels. Figure 9.1 shows how you might draw a simple square by using this coordinate system. - 2 - Javas coordinate system is different from many
6、 painting and layout programs that have their x and y in the bottom left. If youre not used to working with this upside-down graphics system,it may take some practice to get familiar with it. Drawing and Filling The Graphics class provides a set of simple built-in graphics primitives for drawing, including lines, rectangles, polygons, ovals, and arcs. Lines To draw straight lines, use the drawLine method. drawLine takes four arguments: the x and y coordinates of the starting point an