Lesson TenMore advanced graphicsNow that we learned the basics of Java graphics, now we can go on to more advanced graphics methods. First we will draw an arc with the drawArc() method. It requires six arguments: an x and a y-coordinate of the upper-left corner of an imaginary rectangular boundary, the width and height of the boundary, the beginning arc position, and the arc angle. This link shows the circle we created. You can achieve the same effect with the drawOval() and fillOval() methods. However, these methods only require four arguments, the same four as with drawing rectangles. You can also draw rectangles that appear to have a shadow by using the draw3DRect() method. It takes the same coordinates and drawRect(), only it requires a fifth argument which is a Boolean value, true for the shadowing to be on the right and bottom, false for shadowing to be on the left and top. This link illustrates how the fill3DRect() method works. This effect works better with lighter colors. Notice that answering "true" or "false" not only changes the shadow, but the whole rectangle. Say you wanted to create and odd shape that has many lines. You could draw the shape line-for-line, or you could use the drawPolygon() method. This method requires three arguments: an integer array of the x-coordinates, an integer array of the y-coordinates, and the number of pairs of coordinates you want to connect. An example of how you would create a polygon is written below: This program will draw a red star in the upper left-hand corner. To see how this works, click here. We used the same program, yet added the copyArea() to copy the star and place it in a new spot. Then, we drew another star using the fillPolygon() method. Click here to see how this was done. Another set of graphics methods are used to alter fonts. To know what fonts your computer supports, go into your word processing program, click the drop-down arrow next to your default font, and view all the fonts that your computer has. Write a few of them down on a piece of paper, or keep your word processor open. Now we can learn how your fonts are arranged. In newspapers and magazines, the publishers measure and align the font by its' height. The height is measured by the fonts leading (pronounced "ledding"), which is the amount of whitespace above a line of text, its' ascent, which is the height of a capital letter from its' baseline, and its' descent, which is the amount of space that the characters that hang below (like "p" and "y") the baseline take up. Here's how we created a program to read what our computer renders: When we compiled and ran the applet, this is what we got. (NOTE: We had some problems running this in IE. Netscape let us view the program, while IE would show the output briefly and the disappear.) Question: Cool!! I like creating graphics. But it seems that you have to retype everything over and over. Is there a way around that? Answer: Good. You're learning something. Yes, there is an easier way to write programs. It's called inheritance.
If you have a question about any of the lessons, feel free to ask. |