Better still, we could define a method to call this instruction three times and draw the triangle. We would call the method triangle, presumably, and whenever we wanted a triangle drawn, we would call the method.
People who have come to Java from other programming languages will almost certainly have recognised this by now. The idea of methods appears in all programming languages, although sometimes it goes under the name functions and sometimes under the name procedures. The name methods is a throw-back to the language C++, from which Java was developed. In C++, there is an object called a class which can contain methods. However, you will have noticed that everything in Java is enclosed within a class of some sort (after all, the program contains the instruction public class extends Applet), so the functions within it are called methods.
import java.awt.*;
import java.applet.*;
public class calculation extends Applet
{ int first, answer;
public void paint (Graphics g)
{ first = 34;
calculation();
g.drawString("Twice 34 is " + answer, 10, 25);
}
public void calculation ()
{ answer = first * 2;
}
}
The method is called by the statement calculation();. You'll notice that we have to put a pair of brackets after the name, even though there is nothing between them, and that we do, of course, need the semicolon at the end.
Just calling the method isn't enough. It has to be defined somewhere, or Java will not know exactly what to do when it comes across the calculation(); command. The definition starts with either the word public or private (at the moment, it doesn't matter which of these you use - we will be going into these in more detail in a later section), followed by the method name (calcuation in this case) and the pair of brackets again.
On the next line we have an open curly bracket followed by the statements that make up the method. In this case, we only have one statement - the one that sets up the value of answer. Then we have a closing curly bracket to show that we are at the end of the method.
Calling the method calculation() is equivalent to executing the statement answer = first * 2. In this case, it is hardly worth defining a method to carry out one simple instruction. However, it does show the minimum needed for a method to be defined in a program.
A common mistake - and one that I am always making! - is forgetting the closing curly bracket at the end of the method definition. Here is the same definition that you saw above but with a deliberate mistake:
import java.awt.*;
import java.applet.*;
public class calculation extends Applet
{ int first, answer;
public void paint (Graphics g)
{ first = 34;
calculation();
g.drawString("Twice 34 is " + answer, 10, 25);
}
public void calculation ()
{ answer = first * 2;
}
Here, I have missed out the ending bracket. However, Java spots that there is an ending bracket afterwards and assumes that this is the one that ends the method definition. It then looks round for an ending bracket to end the class, doesn't find it, and produces an error message. This is why it is useful to indent your program lines in a similar way to mine, so that you can spot immediately when there is a bracket missing.
The word after the public or private (i.e. the second word of the method declaration) is the return type of the method. We will be dealing with values returned from methods in the next section, so I don't want to say much about them here. The word void in this case indicates that the method does not return a number or a value.
If you want to call a method using a simple instruction (as in the case of the method calculation() above), then you should include the word void after the word public or private.
import java.awt.*;
import java.applet.*;
public class triangleDemo extends Applet
{
public void paint (Graphics g)
{ triangle(g);
}
public void triangle (Graphics my_graphics)
{ my_graphics.drawLine(100, 100, 150, 100);
my_graphics.drawLine(150, 100, 125, 150);
my_graphics.drawLine(125, 150, 100, 100);
}
}
You can see that the applet contains two blocks of code, both similar in structure. These are both methods - the first method is called paint() and it is a special one that is called automatically when the applet is run. The second method is called triangle and it contains three instructions to draw lines. This method is not called automatically, but we have instructed the program to call it when it calls paint. You can see that the paint method contains an instruction that activates the triangle method.
But what is this reference to my_graphics? In fact, this is called a parameter. The triangle method draws lines, but the only way in which it can do this is if it has a Graphics object (the drawLine instruction is built into the Graphics object and is available nowhere else). We have a graphics object inside the paint method - we are provided with one called g. All we have to do is pass it to the triangle method so that it can use it as well.
We pass a parameter to a method by enclosing it within the brackets just after the method's name. You will notice that when the triangle method is called, g is passed to it between the brackets: triangle(g). When the method is defined below this, we also specify the parameter, except:

Let's try passing other parameters - numbers for example:
import java.awt.*;
import java.applet.*;
public class triangleDemo extends Applet
{
public void paint (Graphics g)
{ triangle(g, 100);
}
public void triangle (Graphics g, int x)
{ g.drawLine(x, 100, x + 50, 100);
g.drawLine(x + 50, 100, x + 25, 150);
g.drawLine(x + 25, 150, x, 100);
}
}
I have put the graphics parameter back to g. Most people tend to call the graphics parameter this, although there is no reason why you can't call it just about anything else (as you have seen). I have also included an integer parameter (you see it is preceded by the word int in the declaration of method triangle) called x.
When the method is called, the graphics parameter g is tied to the graphics parameter in the method definition, and the number 100 is tied to the integer parameter x:

So x inside the method is equivalent to 100, and the method triangle is exactly equivalent to the one that we saw before which had just numbers in it. Just as x becomes 100, so x + 50 becomes 150 and x + 25 becomes 125.
Let's add another line that calls the method again:
import java.awt.*;
import java.applet.*;
public class triangleDemo extends Applet
{
public void paint (Graphics g)
{ triangle(g, 100);
triangle(g, 170);
}
public void triangle (Graphics g, int x)
{ g.drawLine(x, 100, x + 50, 100);
g.drawLine(x + 50, 100, x + 25, 150);
g.drawLine(x + 25, 150, x, 100);
}
}
The first time the method is called then x becomes 100 as described above. The second time it is called, x is set to 170, in which case x + 25 becomes 195 and x + 50 becomes 220. The second call to the method is exactly equivalent to the following:
public void triangle (Graphics g)
{ g.drawLine(170, 100, 220, 100);
g.drawLine(170, 100, 195, 150);
g.drawLine(195, 150, 170, 100);
}
In this case, you now get two triangles on the screen. Want to see it? Oh, all right then!
Of course, there is no reason why we shouldn't pass the y-value as a parameter as well. The following version passes three parameters to the triangle method - the graphics parameter g and two integer values, x and y:
import java.awt.*;
import java.applet.*;
public class triangleDemo2 extends Applet
{
public void paint (Graphics g)
{ triangle(g, 15, 90);
triangle(g, 63, 17);
triangle(g, 100, 120);
}
public void triangle (Graphics g, int x, int y)
{ g.drawLine(x, y, x + 50, y);
g.drawLine(x + 50, y, x + 25, y + 50);
g.drawLine(x + 25, y + 50, x, y);
}
}
So what's going on here then? Well, the value of the y parameter is set to 90 for the first triangle, 17 for the second triangle and 120 for the third triangle. This means that y + 50 becomes 140 for the first triangle, 67 for the second, and 170 for the third triangle. Since the height of the applet was defined to be 160 (look at the source code of this page and search for the string triangleDemo2.class), which is less than 170, the last triangle has its point cut off.
Note that the parameter list in the method declaration has to specify the type of every paremeter. This means that the word int must be duplicated for both x and y. You could not get away with doing this:
even though x and y are the same type. Sorry!
![]() Go back |
![]() Top of page |
![]() Main menu |
![]() Go on |