Java itself can be used in many many different situations, but we will focus our attention on web pages. Java is used to produce applets (think of "miniature applications") which may be inserted a web page and activated by anyone who browses that page. Here [not really, yet] is an example of a web page which contains a simple applet.
In this Java tutorial, you will be learning how to develop applets like
this one. We will start with some very basic Java ideas and gradually
increase the complexity of what we can do until, finally, you will be able
to design applets even more complicated than the one above.
But what are you sending these messages to? In Java, the answer is another general, powerful concept: messages are sent to objects. This, too, works a lot like the "real world": when you send a message to "Turn the TV on," you are sending that message to a particular "object" -- your television. It's important to note, though, that not every object can make sense of every message -- what does it mean to tell your television to "Speed up?" That is, the type of an object determines what kinds of messages make sense to it.
Now, when we're writing computer programs, we won't actually be sending
messages to televisions or cars. But we might send messages to objects
that are supposed to behave like, or simulate, objects in the "real
world". For example, if we were designing a video game to take place
on a race course, we would very likely create Java objects that acted like
(that is, responded to the same messages as) "real" cars.
MyTV.powerOn();The name of the object comes first, followed a period, followed by the name of the message we're sending. The parameters (if there are any) go in parentheses after the name of the message; if there are many parameters they are given in a list separated by commas. Since there are no details in this case, there's nothing in the parentheses -- but the parentheses must be there every time you send a message. The semi-colon tells Java that this is a complete "action", just like a period indicates the end of a complete sentence in English.
Exercise
Give a Java statement which sends a powerOff() message to MyTV.Exercise
Suppose MyTV can also receive changeChannel() messages; this message requires one parameter: the number of the channel to change to. Give a Java statement which sends a message to MyTV telling it to change to channel 7.Exercise
Consider simulating the behavior of a soda vending machine. Suppose we have an object, MySodaMachine, which can receive two kinds of messages:Give a Java statement which sends a message to MySodaMachine telling to give back 45 cents in change.
- dispenseProduct(), which requires one parameter: the name of the soda to dispense, such as "Pepsi", or "Fresca"
- dispenseChange(), which requires one parameter: the amount of change to give, in cents
Exercise
Consider simulating the behavior of a calculator. Suppose we have an object, MyCalculator which can receive four kinds of messages:Each message requires two parameters: the two numbers that are being operated on (the operands). Note that for subtraction and division, the order of the operands is important: 7 - 5 is different from 5 - 7.
- add()
- subtract()
- multiply()
- divide()
Give a Java statement which sends a message to MyCalculator telling it to add 27 and 14.
Give a Java statement which sends a message to MyCalculator telling it to divide 56 by 7.
g.drawRect(40,20,200,100);In fact, the drawRect() message is a standard part of Java; it works exactly as we've used it in this example. Let's see how to write a complete Java applet (our first) which will cause a rectangle to be drawn on the screen just like in this example. Here's the applet:
import java.awt.*;There's our drawRect() statement in the 5th line of text, but what's all that other stuff for? These other lines are necessary for telling Java that we're writing an applet, rather than some other sort of Java program. The first two lines make sure that all the objects and messages we might need for writing an applet will be available to us (if we leave them out, "drawRect" won't make any sense!). The third line tells Java that we are writing an applet (it "extends Applet"), and that the name of our applet is FirstApplet. The fourth line says that what follows describes what the applet should draw (or "paint") on the screen; the applet will use the object g to do the drawing; the word Graphics tells Java the type of g -- it can receive messages about drawing, but not, say, about adding and subtracting, or dispensing change, or turning on a television. Notice that the entire applet is enclosed in {}, as is the entire paint section. Never leave these out!
import java.applet.*;public class FirstApplet extends Applet {
public void paint (Graphics g) {
g.drawRect(40,20,200,100);
}
}
Exercise
Write a complete Java applet which draws a rectangle with width 50 and height 200 with its upper left corner 100 pixels over to the right and 100 pixels down from the top.
Exercise
Notice that these rectangles look "hollow"; they're just line drawings. Objects with type Graphics can also receive fillRect() messages, which work exactly like drawRect() messages except that the rectangle that is drawn is filled in instead of hollow.Write a complete Java applet which draws a filled rectangle with width 75 and height 30 with its upper left corner 150 pixels over to the right and 20 pixels down from the top.
import java.awt.*;This applet causes two rectangles to be drawn on the screen in different places, one hollow and one filled.
import java.applet.*;public class AnotherApplet extends Applet {
public void paint (Graphics g) {
g.drawRect(40,20,200,100);
g.fillRect(80,80,150,100);
}
}
Exercise
Write a complete Java applet which draws two rectangles on the screen as follows:They can be any size you like, and be drawn anywhere on the screen, but you must determine how to fulfill the 4th rule.
- One must be hollow and one must be filled
- They must be the same size
- The hollow one must be to the left of the filled one
- They must be directly adjacent to each other (i.e. "touching" each other)