A Java Application
Now that you know how to create Frames to contain the AWT components, you can write Java Windows applications that incorporate all the capabilities of the language. This tutorial started with Java Applications to introduce some of the capabilities of the language but moved on to applets prior to discussing graphics and the AWT. The reason for this should be obvious at this point. Components and graphics need a container. An applet is a subclass of a panel with special capabilities to enable it to be displayed in a browser. We simply paint or add objects in our applet, compile it and place a reference to the resulting .CLASS file in our HTML code and our browser will display them. The applet provided the panel and default layout required.
Applications do not have any default container. We need to explicitly create one and place everything in the container. The container that we normally use is a frame. That frame is then usually subdivided into panels to contain our components and canvases to contain any graphics. Recall that we needed a "public static void main" method for all of our applications. (Applets require an "init" method.) A constructor class is usually used to build the frame and handle the events. The main method of the applet then simply will set the initial parameters and create an instance of the frame. The code below shows how the color applet, covered in the previous section, can be written as an application.
import java.applet.*;
import java.awt.*;
public class colrappn extends Object
{
public static void main(String arg[])
{
Frame colorframe = new appframe("Color
Mixture Application");
colorframe.show();
}
}
|
We import the same files as before. But we don't extend the Applet class. All applications require a main method. This one will... Create a new instance of the frame. with the appropriate title and show it. |
class appframe extends Frame
{
TextField red = new TextField(5);
TextField green = new TextField(5);
TextField blue = new TextField(5);
Canvas mixture;
appframe(String title)
{
super(title);
resize(400,200);
Panel values;
setLayout(new GridLayout(1,2,10,10));
mixture = new Canvas();
mixture.setBackground(new Color(0,0,0));
red.setText("0");
green.setText("0");
blue.setText("0");
values = new Panel();
values.setLayout(new GridLayout(4,2,10,10));
values.add(new Label("Red Value:"));
values.add(red);
values.add(new Label("Green Value:"));
values.add(green);
values.add(new Label("Blue Value:"));
values.add(blue);
values.add(new Label("Hit Enter or this>"));
values.add(new Button(">>>>"));
add(values);
add(mixture);
}
|
This constructor class is the same as
before. Declare the text fields and the canvas. Grab the title parameter and call the Frame class to create a frame with the title. Set the layouts and place the panel and canvas as before (This time we'll also add a button.) (Applications like buttons.) and add the panel and canvas to the frame. |
public boolean action(Event evt, Object whatAction)
{
if((evt.target instanceof TextField))
{
setcolor();
return true;
}
if (evt.target instanceof Button)
{
setcolor();
return true;
}
return false;
}
|
Here we check for the actions. If the user hit enter from within a text field Change the colors and signal the event was handled. If
the user hits the button Otherwise signal that the action wasn't handled. |
public boolean handleEvent(Event evt)
{
if (evt.id == Event.WINDOW_DESTROY)
{
hide();
dispose();
return true;
}
return super.handleEvent(evt);
}
|
|
public void setcolor()
{
String r = red.getText();
String g = green.getText();
String b = blue.getText();
Color c = new Color
(Integer.parseInt(r),
Integer.parseInt(g),
Integer.parseInt(b));
mixture.setBackground(c);
mixture.repaint();
}
|
|
| public Insets insets() { return new Insets(30,10,10,10); } } |
All the code down to this point is the same as before except our inset from the top must allow space for the frame's title bar. That is why the first parameter was increased to 30. |
The easiest way to obtain the code for this Applet is to click here and copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "colrappn.java".
After compiling this program we don't need to create an HTML page with a reference to the class file. Simply type ' javaw colrappn' and the java windows run time package will execute the program. (You could type 'java colrappn' just as you did in the first section on applications. The run time package will execute the program with no problem but you will need to hit <Ctrl>C to terminate the java run time package. For windows applications it is better to use javaw.)
Congratulations! You're now ready to program your own Applets and Applications.