Getting oriented to the Java WorkShop workspace
Java WorkShop has several screen areas shown in the image below:
NOTE: If you are having a lot of problems with the
display in Java WorkShop, try
setting the Look and Feel under Preferences to
Motif. Also, Java WorkShop runs slowly on some machines and you may need to
refresh the display manually if things don't quite look right (Reload Source File
option on the File menu). Java WorkShop runs slowly, especially on older
machines that don't have enough memory. Be patient.
Creating your first program in Java WorkShop
In order to create your program, you will:
A portfolio is a way of grouping related programs together in the same
way you might group email into folders. All of your programs will be
organized into portfolios. A portfolio has a name and can contain
one or more projects. These instructions assume that you will be organizing
all of your programs into a single portfolio called programs.
Portfolio option under New on the
File menu (click with left mouse button):
File menu at the top of the menu.
New.
New has an arrow indicating it has submenus. Move the
cursor over the arrow to bring down the next submenu.
Portfolios and click.
Create Portfolio box should appear on your screen.
programs.psf:
programs.psf in the box.
Create
programs should appear in
your listing of portfolios in the Project Browser on the left of your
workspace.
Java WorkShop uses a fast internal compiler by default. Unfortunately
it doesn't do well with packages and may also have problems with the
file permissions on our system. (The symptom of this problem is
that you won't be able to compile a project because Java WorkShop
can't create a create a file You should switch to the standard
compiler. (Under Preferences select the Builder
option to bring up the following window in which you
can select the standard Java compiler as shown.)
Each program you write is called a project.
programs portfolio in the
Navigator window. The
programs portfolio must be highlighted
before you can put a project in it.)
Project option under New on the
File menu.
HelloStandalone:
untitled1
with HelloStandalone).
Standalone button.
Next button at the bottom of the dialog.
Finish button to finish up.
HelloStandalone.java:
main.
Enter key. Notice that the cursor moves to
the next line several spaces in.
System.out.println("Hello World");
Save option on the
File menu or click on one of the pictures of a floppy disk
.)
. Messages will appear in the window
at the lower right of the workspace (the Console Window)
indicating that the build
was successful. If you made a typo, you'll have to
fix it in your program source and try the build again.
You can also build your program by using the Build menu.
. The words Hello World should show up in the Console Window.
An applet is a Java class that can be run from a browser. Applets can be tested by calling them from a web page, running them from an appletviewer or running them from Java WorkShop (which is what we will do here). In this exercise you will create an applet to output Hello World in an applet window.
programs portfolio. (The programs
portfolio should now be highlighted in the Navigation Window.)
Project option under New on the
File menu.
HelloApplet.
(Accept all of the default choices when responding to the menus.)
import java.awt.*;
public void paint(Graphics g) {
g.drawString("Hello World", 10, 10);
g.setColor(Color.red);
g.fillOval(10, 20, 50, 50);
}
This code provides instructions to output the string "Hello World"
in the applet window starting at a position 10 pixels down and
10 pixels to the right. The code then sets the graphics color to red
and draws a circle. The circle has diameter of 50 and is displayed
in a rectangular area that starts at (10, 20) on the screen).
(A screen image of the source window is
shown below.)
.
.
.
This exercise gives you instructions for using Java class libraries.
programs
portfolio entitled HelloLibrary.
import java.util.*;
main
function:
Date todaysDate = new Date();
System.out.println("Hello World, today is " + todaysDate);
An example of the source and output for HelloLibrary
is shown below.
HelloLibrary program.
This exercise explains how to incorporate a prewritten package into your Java WorkShop environment. The steps are:
simpleIO
Java source files into your simpleIO directory.
simpleIO project and add the simpleIO java source files to it.
simpleIO package.
HelloSimple standalone test program.
simpleIO to HelloSimple's
CLASSPATH.
HelloSimple.
simpleIO
package provided as part of textbook Problem Solving with Java
by Koffman and Wolz.
simpleIO
Java source files into your simpleIO directory:
You can download the package from the
simpleIO source directory
ftp://ftp.aw.com/cseng/authors/koffman/psjava/psjava/simpleIO
provided at the publisher's website.
Make a directory called simpleIO in the same place
as your helloStandalone directory. Download all of the
files in the publisher's simpleIO directory into
your simpleIO directory. (You can download them one at
a time or can download the zipped version at
ftp://ftp.aw.com/cseng/authors/koffman/psjava/pc/simpleIO.zip , but
be sure to unzip them into the simpleIO directory.)
simpleIO project in the
programs portfolio and add the simpleIO java source files to it:
Select the
programs portfolio and create a new project entitled
simpleIO under this portfolio.
(See
http://vip.cs.utsa.edu/classes/JavaInfo/JavaWorkShop.html#project if you
need a refresher.) Be sure to create simpleIO as a package, rather than as a
standalone or applet. When prompted for the project name, replace
the default name with simpleIO. Enter the package name as
simpleIO.
Since you have already downloaded the files of the simpleIO
package, you should check Yes to the question
Are there files already there that should be added to the project?.
After you finish creating the simpleIO project, the files you
downloaded should appear in the Navigation Window. If they don't,
you will need to add them by hand. Under the Project menu
at the top, select Add and then file. Click on each file to
select it. (Holding down the shift key is useful here.) Make sure that
all of the files in the simpleIO subdirectory are in the
simpleIO project before
going on to the next step.
simpleIO package:
Just click the Build icon
.
The package should compile with one warning.
HelloSimple standalone test program:
Create a standalone project entitled HelloSimple in your
programs portfolio. (Follow the directions that you used
to create HelloStandalone as given above at:
http://vip.cs.utsa.edu/classes/JavaInfo/JavaWorkShop.html#project .)
Bring your newly created standalone program HelloSimple into
the Editing Window.
Insert the line:
import simpleIO.*;
at the top of the source file. Insert the following lines in between the
braces in main:
SimpleGUI wind = new SimpleGUI();
wind.displayResult("Hello World");
Now build
the
HelloSimple program. The build will fail because Java WorkShop
can't find the package simpleIO.
simpleIO to HelloSimple's CLASSPATH:
In the Project menu select the Properties submenu. It will
show a tabbed dialog box.
Make sure the Class Path tab is selected. Add the path of your
simpleIO package to the HelloSimple project as shown:
In this example, the HelloSimple project is in the default directory,
D:\WINNT\Profiles\krobbins\jws\HelloSimple and the
simpleIO package source
and class files are in a:\simpleIO. Notice that only
a:\ is given as the path name for the package. The package
files have to be stored in a directory of the same name as the package
and that is added to the path automatically.
and run
the HelloSimple program. A
small window with the words "Hello World" should appear.
Adding your own class to a project
HelloCount standalone project:
As above, select the programs portfolio and create a standalone
project called HelloCount that contains the following code:
public class HelloCount {
/**
* Constructor.
*/
public HelloCount () {
}
public static void main(String args[]) {
CountPrint hello = new CountPrint("Hello World");
hello.printString();
hello.printString();
hello.printString();
System.out.println(hello.getString() + " was printed " +
hello.getCount() + "times ");
System.out.println(hello); // uses toString method
}
}
CountPrint class:
On the File menu select New and then File.
Add the following in the Source Editing Window:
public class CountPrint {
// fields hold internal state of the object
private int myCount;
private String myString;
// constructor initializes object during its creation
public CountPrint(String s) {
myString = s;
myCount = 0;
}
// accessors don't change object state
public int getCount() { return myCount; };
public String getString() { return myString; };
public String toString() { return myString + " printed: " + myCount + " times" ;}
// modifier outputs myString and updates myCount
public void printString() {
System.out.println(myString);
myCount++;
}
}
Save the file as CountPrint.java by selecting the Save As
item on the File menu. Add the CountPrint.java file
to the HelloCount project by selecting the Add option
on the Project menu. CountPrint should appear
in the Navigation Window under the HelloCount project.
and run
the project
Java comes with many libraries or packages that
can extend the functionality of your program. You can also develop your
own packages.
This section works through an example of adding your own package in Java WorkShop.
Be forewarned that the example is more complicated than the previous examples.
We will create a package called PrettyPrint that
contains a class called FancyText. FancyText
stores a string and provides different ways of printing out the string
(e.g. underlining or putting a box around it). After developing the
PrettyPrint package, we will write a standalone test program
called HelloSimple.
Create the PrettyPrint package:
PrettyPrint
in the programs portfolio.
FancyText.
PrettyPrint package.
FancyText.
HelloClass program to
call FancyText.
CLASSPATH so that HelloClass
can find the PrettyPrint package.
PrettyPrint package:
PrintyPrint
in the programs
portfolio.
package
rather than as a standalone or applet.
PrettyPrint.
D:\WINNT\Profiles\krobbins\jws\PrettyPrint. The
root directory is then D:\WINNT\Profiles\krobbins\jws.
FancyText:
File menu select New and then
File.
package PrettyPrint;
public class FancyText {
private String myString;
public FancyText (String s) {
myString = s;
}
public void printBoxed(char boxChar) {
int boxLength = myString.length() + 2;
for (int i = 0; i < boxLength; i++)
System.out.print(boxChar);
System.out.println();
System.out.println(boxChar + myString + boxChar);
for (int i = 0; i < boxLength; i++)
System.out.print(boxChar);
System.out.println();
}
public void printUnderlined() {
System.out.println(myString);
for (int i = 0; i < myString.length(); i++)
System.out.print('-');
System.out.println();
}
}
FancyText.java. (Under the
File menu use the Save As option.)
FancyText class file to the PrettyPrint
package. (Under the Project menu select the Add
option and the File suboption under that.
If the File is faint it means that you haven't properly
selected PrettyPrint as the package. This is tricky.)
The FancyText class file should appear as shown below.
PrettyPrint package:
Preferences select the Builder
option. This will bring up the following window in which you
can select the standard Java compiler as shown
above.
Build the package and correct any errors.
PrettyPrint package:HelloClass program to
call FancyText:
HelloClass
in the programs portfolio.
import PrettyPrint.*;
main:
public static void main(String args[]) {
FancyText hello = new FancyText("HelloWorld");
hello.printBoxed('*');
System.out.println();
hello.printUnderlined();
}
CLASSPATH so that HelloClass
can find the PrettyPrint package;
Project menu select the
Properties option. This selection will bring up a dialog box
as shown below. (The box looks a little different because we
set the Look and Feel under Preferences to
Motif.)
Class Path tab and add the root path of the
PrettyPrint package. In the example, the PrettyPrint
package is in the same directory (shown at the same level in the
Navigation Window.
The root path is d:\WINNT\Profiles\krobbins\jws .)
P references menu and
under Look and Feel select Motif. Now
go back under P references under
Look and Feel and select Windows.
Exit command work?
It's a mystery. However the exit box on the upper right corner of the window
does work. Click it when you want to quit.