A Rudimentary Introduction to Eclipse

Rudimentary Eclipse Tutorial

Goal – Create an applet to draw some colored boxes.

Compiling Java Programs

Java programs are compiled using a free Java Software Development Kit (SDK), from Sun Microsystems. Once the SDK is installed on your computer, any text editor can be used to prepare the Java files. They are compiled by invoking javac.

Why Use Eclipse?

We choose to use the open source Eclipse interactive development environment, available free from Eclipse provides syntax highlighting, automatic code completion, powerful debuggers and project managers. Eclipse also provides continuous compilation, meaning errors show up immediately as you type. When you make an error, Eclipse often offers “quick fixes”, such as suggesting declaring a variable or including an import. There is code generation for things like getters and setters, and surrounding statements with try/catch blocks. Eclipse also provides refactoring support, such as renaming a program element in a consistent way across files, and lifting a common element to a superclass. Finally, Eclipse’s capabilities may be extended by plug-ins. Some of these must be purchased, while others are available free.

Starting to Build an Applet in Eclipse

To start building the applet, start up Eclipse, and choose new from the File menu. Make sure you select a Java project, and then press Next.

We will build a simple Java applet. All it will do is draw some colored boxes. So we will name the project Boxes and press the Finish button.

The project appears in the Package Explorer.

Right-Click on Boxes. Right-Clicking brings up the context menu. We need to create a new public class, named Boxes. Choose New | Class. This brings up the New Class Dialog Window. Type in Boxes as the Name, and Applet as the Superclass. Notice that this brings up a warning that the superclass does not exist. We will deal with that later. Unclick the option for generating a main method, since applets don’t require a main().

The class opens in an editor. There are red X marks all over, indicating the parts of the project which are contaminated by an error.

Also, the word Applet is underlined in the editor window, indicating precisely where the problem is. The easiest option is to double-click the X to the left of the line, which says “public class boxes extends Applet {“. This brings up the Quick Fix menu.

Select the first recommendation, namely “Import ‘java.applet.Applet’”. The X marks fade. When you do a save, they disappear. The next job is to override the inherited paint() method. In the edit window, right-click the line which declares the Boxes class, and from the context menu select source | Override/ Implement methods. This brings up the Override dialog window. In the Container superclass, check paint(Graphics). The paint() method is included into the file, ready to be overridden.

Noticed that the Task list shows a TODO item, reminding that the stub method needs to be overridden. We will cast the Graphics context, arg0, to a Graphics2D, create a couple of boxes, set the color and draw the filled boxes. When we start typing in the edit window, error flags appear.

As before, we do quick fixes to include the required imports. Once g2 is recognized as a graphics2D object, screen hints will appear showing possible completions as we type in g2.

The completed method is as follows:

publicvoidpaint(Graphicsarg0){

super.paint(arg0);

Graphics2Dg2=(Graphics2D)arg0;

Rectanglebox1=newRectangle(10,10,110,110);

Rectanglebox2=newRectangle(20,20,100,100);

g2.setColor(Color.blue);

g2.fill(box1);

g2.setColor(Color.ORANGE);

g2.fill(box2);

}

We need to run the completed project. On the toolbar are two icons of runners. Press the down arrow to the right of the first, and choose Run as | Java Applet. The applet should look like this:

To run again, simply press the runner icon on the left. To run in the debugger, press the bug icon on the toolbar. It will run until the first breakpoint. The breakpoints are the small filled circles to the left of lines in the edit window. Some of these were inserted automatically for you as you built the program. The debug window shows a wealth of information.

Eclipse operates in different perspectives. These are shown on the left side of the Eclipse window. After stopping the debugger, you should switch back to the Java perspective.

You can change perspectives by clicking on one of the perspective icons in the lower left corner of the picture above, or by opening a perspective from the window menu.

There is a lot more to Eclipse, which is a powerful professional development tool. Furthermore, Eclipse is an open source project, designed from the bottom up to be readily extended by various plug-ins. By writing an Eclipse plug-in you can participate in a world-wide open source development effort. More information about Eclipse is available form

2004 by Michael M Werner111/4/2018