You have already seen in the previous section that graphics commands are called as methods belonging to a special type of variable, called a Graphics Environment variable. Normally, these commands will only be called in the special method called paint, which is given the task of drawing the screen.
When the Java applet is activated, Java looks for a method called paint which has a single parameter of type Graphics. If it doesn't find one, then it doesn't spit out an error - but it doesn't draw anything on the screen either! Java also calls the paint method automatically in some other circumstances. For instance, if you resize the browser window in which the applet is sitting, you will see the applet flicker as the screen is automatically updated.
Usually, Java applets contain a method like this:
public void paint (Graphics g)
{
Screen-drawing statements go here
}
All the screen drawing instructions will start with the characters g., indiciating that they are part of the Graphics parameter passed. However, the parameter is only called g by tradition - there's no logical reason why the parameter shouldn't be called any legal name you want. You could easily write a paint method that looked like this:
public void paint (Graphics three_blind_mice)
{
three_blind_mice.drawLine(100,150,325,72);
}
In this case, all the drawing instructions would start with three_blind_mice. as that is the name of the parameter. There may be cases where you need to draw things on the screen at some other point in the program. You can access the screen at any point by declaring a variable of type Graphics and using the methods within it. I will go into variables in more detail in a later section, but suffice it to say that if you ever need to draw something on the screen at some point other than in the paint() method, then include the following lines in your program at the relevant point:
Graphics temp_g = getGraphics(); temp_g.drawOval(300,120,100,75);
The first of the two lines declares a graphics environment variable - I've chosen to call it temp_g, but you could give it any name you wanted - and sets it to the current graphics settings, using the built-in method getGraphics. Be careful to get the capital letter in that - Java is case sensitive, and it won't recognise the method if you call it GETGRAPHICS or Getgraphics etc.
The second of the two lines is an example of how the new graphics variable could be used. Obviously, you would replace that line with the particular instruction that you wanted.
All graphics instructions use a co-ordinate system just like the one that you learned at school. Each "unit" is one pixel wide, so the entire screen is about 600 units wide and about 400 or so units high. This is why co-ordinates in Java programs tend to be fairly large values.
The (0,0) point of the co-ordinates (the origin) is always the top-left corner of the Java applet window. The first number of each pair refers to the number of pixels across the screen, the second number refers to the number of pixels down the screen. The maximum co-ordinate values in each case depend on the size of the applet window, which was determined when the applet was called in the web page. You will recall that you called it with the command
<applet code="first.class" width="150" height="150"></applet>
The width="150" height="150" part determines the maximum co-ordinate as (150,150). If you try to draw something at a co-ordinate larger than this, then it wouldn't appear on the screen. If you draw anything (a line, oval or rectangle) that would be partly on the screen and partly off, then only a fraction of the shape would appear, chopped off by the edge of the window.
The graphics environment contains the following instructions for drawing lines, ovals (ellipses - including circles), rectangles and arcs:
g.drawLine(100,100,420,312); g.drawRect(72,160,200,35); g.drawOval(300,120,100,75); g.drawArc(250,250,100,100,0,180);
Note that all the instructions start with g. as they are part of the Graphics Environment variable g. They all also end with a semicolon (;). All statements - graphics or otherwise - in Java end with a semicolon. Also, bear in mind that names in Java are case sensitive, so the names must be typed in in lower case letters with the single upper case (capital) letter in the middle of the name as shown. Otherwise the compiler won't recognise them.
The instruction drawLine draws a line (surprise, surprise!) It is given four numbers. The first two numbers represent the x- and y-co-ordinates of one end of the line, and the last two number represent the x- and y-co-ordinates of the other end. The instruction that you see above draws a line from co-ordinates (100,100) to (420,312).
The instruction drawRect draws a rectangle. It is given four numbers. The first two numbers are the x- and y-co-ordinates of the top-left corner. You might expect the other two numbers to be the x- and y-co-ordinates of the opposite corner (as they would be in some other programming languages), but no! They represent the width and the height of the rectangle. In the case of the example above, the rectangle has its top-left corner at (72,160) and a width of 200 and a height of 35. Its bottom-right corner is at (72+200,160+35) i.e. (272,195).
The instruction drawOval is given four numbers. You have to imagine that the oval is drawn within an invisible rectangle. The four numbers represent the top-left co-ordinates of the rectangle and its width and height, in the same way as for rectangles. This mean that the oval drawn in the example above completely fills an invisible rectangle whose top-left co-ordinate is at (300,120) and whose bottom-right co-ordinate is at (400,195). The width of the oval at its widest point is 100 pixels, and its height at its maximum point is 75. There is no standard way of drawing an oval at a slanted angle.
The graphics environment also gives a method which draws arcs, i.e. sections of oval shapes (or circles, of course). Arcs have starting angles and arc angles, with all angles being measured from the "East" position (pointing to the right). The arc angle basically means the ending angle minus the starting angle.
To draw an arc, use the drawArc command. This is given six numbers. The first four specify the rectangle in which the entire oval is enclosed, even though only part of that oval is to be drawn. The last two numbers specify the starting angle and the arc angle (both in degrees). Here's an example:
drawArc(100,100,80,60,45,120);
Of course, if the start angle is specified as 0 and the ending angle as 360, then the arc drawn is a complete oval.
There are three more shape-drawing instructions, they are the "fill" equivalent of drawRect, drawOval and drawArc:
g.fillRect(72,160,200,35); g.fillOval(300,120,100,75); g.fillArc(250,250,100,100,0,180);
The results they produce is simply a filled-in version of the basic shape, except for fillArc which produces a pie-slice effect.
You already met the instruction used to display strings on the screen. In case you didn't, you won't be surprised to hear that it is drawString. Just to remind you - a string is a piece of text, anything from a single character (a term which includes letters, punctuation, symbols such as @ or ^, and even the space character) to a whole sentence. Strings are specified by placing them within double quotation marks.
The method drawString is given three parameters between brackets. The first item is the string itself to be displayed on the screen. The other two numbers specify where the string is to appear. It represents the top-left corner of the string. However, the second number actually specifies the y-co-ordinate of a line on which the text sits. Some characters, such as 'g' or 'y' will drop down below this imaginary line, as shown in the following example:
g.drawString("This is hugely easy",40,60);
If you don't specify a colour for drawing shapes or text, then Java uses the default colour of black. Specifying a colour is fairly easy - although you can only specify one of 13 standard colours (including white).
There is a standard method built into graphics environment variables called setColor (note the American spelling of 'Color'). It expects to be given a colour name, but the colour name must be of a standard type specified in yet another class called Color. What all this means is that colours are chosen as follows:
g.setColor(Color.blue); g.setColor(Color.orange);
Here are the thirteen standard colours. Make sure you use them exactly as specified - don't alter the case of the letters or insert spaces in the middle of them, and note the spelling of 'Gray'.
black |
Gray |
orange |
yellow |
blue |
green |
pink |
cyan |
lightGray |
red |
darkGray |
magenta |
white |
||
Any colour that you choose will then be used for drawing all lines, text, shapes and as the fill-in colour for shapes until you choose another colour. You can also determine the colour of the background to the applet. This is done using the following instruction:
setBackground(Color.cyan);
Again, like the setColor method, it is important to note that the name of the method is all lower case letters except for the capital B in the middle. However, please note that setBackground is not part of the graphics environment, and as such, does not need to be preceded by g. In fact, if you do put g. at the beginning, then the Java compiler complains. I have fallen into this trap many times!
Here is an example Java applet which demonstrates the commands that I have described. I have written out the source code below it.
I suggest you use the copy+paste facility to copy this code into a text file, compile it with your Java compiler and run it. If you want to see what effect the different lines have try changing the numbers, removing some of the lines, or altering the order of the lines. Here's the code that I used to create it:
import java.awt.*;
import java.applet.*;
public class graph_ex extends Applet
{
public void paint (Graphics g)
{ setBackground(Color.cyan);
g.drawString("Here are a selection of blank shapes.",20,40);
g.drawLine(20,40,200,40);
g.setColor(Color.blue);
g.drawLine(20,50,70,90);
g.setColor(Color.red);
g.drawRect(100,50,32,55);
g.setColor(Color.green);
g.drawOval(150,46,60,60);
g.setColor(Color.magenta);
g.drawArc(230,50,65,50,30,270);
g.setColor(Color.black);
g.drawString("Here are the filled equivalents.",20,140);
g.drawLine(20,140,200,140);
g.setColor(Color.yellow);
g.fillRect(100,150,32,55);
g.setColor(Color.pink);
g.fillOval(150,146,60,60);
g.setColor(Color.darkGray);
g.fillArc(230,150,65,50,30,270);
}
}
Occasionally, there will be points in the program where you will want it redraw the screen. Perhaps the screen has changed (the paint method can easily cope with if statements that get it to draw different things in different circumstances). To call the paint method in these circumstances, use the following instruction:
repaint();
Note that there are no parameters (no numbers, strings or graphics environments) passed to this method, but you still must include the two brackets and follow them with the obligatory semicolon, like this ();
It is vital that you don't include this method inside the paint method itself. If you did, the paint method would never terminate, as it would always be calling itself.