Home E-Books Special Effects and Game Development in Java interesting methods

Special Effects and Game Development in Java(TM) - Other interesting methods in the Graphics class

by Anibal Wainstein

2.1.4 Other interesting methods in the Graphics class

In the last section we reviewed the Graphics methods setColor() and fillRect(). There are many methods that you have use for in Graphics. The following example shows what you can do with some of them.  

 import java.applet.*;
import java.awt.*;
public class risingsun extends Applet {
public void paint(Graphics g)
   //Create the sky.
     g.fillRect( 0, 0, 200, 100);
     //Create the sun.
   //The method fillOval() draws a filled oval
     //in the middle of the screen with the height and
     //width 50.
    //Create the sun beams with fillArc(),
     //setColor() is not needed because the pen's
     //color will be the same.
//Create the ground
//Create brown color, there is no default brown color
Color ground = new Color(128,64,0); g.setColor(ground);
//Fill the bottom part of the screen with brown g.fillRect( 0, 50, 200, 50); //Create a black text g.setColor(Color.black); g.drawString("Hello Sweden!",70,50); } }
Compile the code above and make a HTML file, which should be an applet with the dimensions 200 times 100 pixels.

You can also click here if you want to see the end result.

We start by looking at the statements in the paint() method from the beginning. The setColor() method in the start, does the same thing that it did in the initbackground applet in the last section. The difference is that we now directly specify the color from the Color class, instead of making a reference to it first. The result is less code and it will be easier to understand for they who read it.
After that we set the pen color to yellow and draw a filled circle in the position (25,25) with the width and height 50. This we do with the help of the fillOval() method. The next step is to draw the sun's beams. These are drawn with the fillArc() method. The method is similar to fillOval() but with the difference that you can specify an angle (in degrees) that you want the beam to start from and also a angle width (in degrees).

How fillArc() works.

Now we fill the lower half of the applet with brown color that we will have to create for ourselves, since it does not exist as default color. We create a rectangle that have half the applet height (Dimensions 200x50) and move it down 50 pixels, which is half the screen.
Finally it is time to add a little greeting with the method drawString() that we draw in black. The method requires a string and one x and y position where the text will be positioned. Note that the y position is a bit misleading. It is the position for the baseline of the font!

The drawString() method draws strings from the base.

You must consider this when you are using drawString(). We will in later chapters show how you can change the font in the text too.

All the methods that we have reviewed here, are well documented in the API reference that you can download from the same place where you download the development packages. There, you will also find a description of other useful methods in the Graphics class. It is just as well that you get used to read the API reference, since it is a tool that is as important as the compiler. Your are not supposed to memorize all these functions. If you try that you will only get bored. In the next chapter we will review threads and you will start making your first special effects.


Next Page >>