Site hosted by Angelfire.com: Build your free website today!

The Source Code


//Draw a Line

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

import java.awt.Graphics;

public class Line extends Applet implements ActionListener {

Label prompt1;

Label prompt2;

Label prompt3;

Label prompt4;

TextField input1;

TextField input2;

TextField input3;

TextField input4;

int coord1;

int coord2;

int coord3;

int coord4;

public void init()

{

prompt1 = new Label( "Enter Starting Horizontal Coordinate here:" );

add( prompt1 );

input1 = new TextField( 10 );

add( input1 );

prompt2 = new Label( "Enter Starting Vertical Coordinate here:" );

add( prompt2 );

input2 = new TextField( 10 );

add( input2 );

prompt3 = new Label( "Enter Ending Horizontal Coordinate here:" );

add( prompt3 );

input3 = new TextField( 10 );

add( input3 );

prompt4 = new Label( "Enter Ending Vertical Coordinate here:" );

add( prompt4 );

input4 = new TextField( 10 );

add( input4 );

coord1 = 0;

coord2 = 0;

coord3 = 0;

coord4 = 0;

input1.addActionListener( this );

input2.addActionListener( this );

input3.addActionListener( this );

input4.addActionListener( this );

}

public void actionPerformed( ActionEvent e )

{

coord1 = Integer.parseInt( input1.getText() );

coord2 = Integer.parseInt( input2.getText() );

coord3 = Integer.parseInt( input3.getText() );

coord4 = Integer.parseInt( input4.getText() );

}

public void paint( Graphics g )

{

g.drawLine( (coord1), (coord2), (coord3), (coord4) );

}

}

The first line, //Draw a Line, is a comment. The comment is designated with the "//". It is used mainly to help sort things out for debugging or knowing what a program is for. This comment tells what the program does.

The next 4 lines,

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

import java.awt.Graphics;

import all of the needed information. Java programs like to use as little space as possible. That's why you list the places to import from.

The next line, public class Line extends Applet implements ActionListener {, says that this applet is "public" (requires no password) and that it uses "ActionListener". "ActionListener" is a function that makes the applet listen for when a button is pressed (in this case, the Enter key).

The next 12 lines,

Label prompt1;

Label prompt2;

Label prompt3;

Label prompt4;

TextField input1;

TextField input2;

TextField input3;

TextField input4;

int coord1;

int coord2;

int coord3;

int coord4;

basically state the variables and integers used in the applet. They then have space reserved for them to be used later on in the applet.

The next line, public void init(), states that the following statements are "public" and "void" (it returns no information when the process is done). The "init" stands for initialize, as in this sequence starts when you initialize the applet (start it).

The next 24 lines,

prompt1 = new Label( "Enter Starting Horizontal Coordinate here:" );

add( prompt1 );

input1 = new TextField( 10 );

add( input1 );

prompt2 = new Label( "Enter Starting Vertical Coordinate here:" );

add( prompt2 );

input2 = new TextField( 10 );

add( input2 );

prompt3 = new Label( "Enter Ending Horizontal Coordinate here:" );

add( prompt3 );

input3 = new TextField( 10 );

add( input3 );

prompt4 = new Label( "Enter Ending Vertical Coordinate here:" );

add( prompt4 );

input4 = new TextField( 10 );

add( input4 );

coord1 = 0;

coord2 = 0;

coord3 = 0;

coord4 = 0;

input1.addActionListener( this );

input2.addActionListener( this );

input3.addActionListener( this );

input4.addActionListener( this );

state the first part of the actions. The "prompt"s and "input"s place the words and text boxes you saw on the applet (assuming you went there first). The "coord =" statements simply state that the coord variables are currently set to 0. The "input.add..." means that the "ActionListener" function should be used in "this" applet and should be listened for at the "input"s.

The next line, public void actionPerformed( ActionEvent e ), means that the next sequence of commands are "public" and "void", and that they occur when an "action" is "Performed".

The next 4 lines,

coord1 = Integer.parseInt( input1.getText() );

coord2 = Integer.parseInt( input2.getText() );

coord3 = Integer.parseInt( input3.getText() );

coord4 = Integer.parseInt( input4.getText() );

simply set the "coord" integers to the value stated in the text boxes.

The next line, public void paint( Graphics g ), states that the next sequence is "public", "void", and uses visual "graphics".

The final important line, g.drawLine( (coord1), (coord2), (coord3), (coord4) );, states that using the "g Graphics", it "draw"s a "Line" using the included coordinates. The "coord" variables are in parenthesis to designate their being variables. The "coord" variables are replaces by their integer value, making the statement valid and creating the line with the input coordinates.

Now take a minute or so to let it sink in. It takes time to understand something as complicated as a new language.

Home