|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
| Note |
|---|
| The Workspaces tab reveals an icon named Empty Workspace, which identifies a workspace category where the workspace contains no project and subprojects. All workspaces in JCreator's current version base themselves on this category. Future versions might add additional workspace categories with different features. |
After entering the workspace name, click the OK button. On my platform, JCreator creates a C:\Program Files\Xinox Software\JCreator
Pro\MyProjects\Graphics directory associated with the Graphics workspace name. Furthermore, a Graphics.jcw file appears in that directory. That file describes the project and any subprojects you add to Graphics.
Now that we have a workspace, we need to introduce a project, that is, an application, applet, or other program type into that workspace. I have in mind a Java applet that draws randomly colored lines in random positions. I've named that project Lines.
To introduce Lines, select New from the File menu. From the resulting New dialog box's Projects tab, select the Basic Java Applet project category, enter Lines as the project name in the Project name text field, and ensure the Add To Current Workspace radio button is selected. (A close look at the Location text field reveals a project directory named Lines that appears inside the Graphics directory.)
Along with Basic Java Applet, the Projects tab reveals project categories Basic Java Application and Empty Project. Those three categories associate with project templates—files of prewritten source code and special symbols. When you select a category, JCreator generates the contents of your project's primary Java source file from the appropriate project template. (You'll learn how to create a project template later.)
Once you finish, click the OK button. The FileView window reveals one project, named Lines, in the Graphics workspace. That
window also reveals two files that comprise the project: Lines.htm and Lines.java. If you double-click Lines.java and scroll down the Editor window, you see source code similar to what appears in Figure 2.
Figure 2. The FileView window displays the Lines project's files, and the Editor window displays Lines.java. Click on thumbnail to view full-size image.
Figure 2's source code is a good start for our Lines applet. However, our applet requires additional code to do its job. Listing
1 presents the complete source code that must go in Lines.java (of course, the comments are optional):
Listing 1: Lines.java
// Lines.java
import java.awt.*;
import java.applet.Applet;
public class Lines extends Applet implements Runnable
{
int width, height;
Thread runner;
public void init ()
{
// Acquire the applet's width and height.
width = getSize ().width;
height = getSize ().height;
}
public void start ()
{
// If no thread exists, create a Thread object associated with the
// current Lines object and start a new thread that invokes the current
// Lines object's run() method.
if (runner == null)
{
runner = new Thread (this);
runner.start ();
}
}
public void paint (Graphics g)
{
// Generate a random color and establish that color as the Graphics
// context's drawing color.
Color c = new Color (rnd (256), rnd (256), rnd (256));
g.setColor (c);
// Generate a random set of coordinates in the upper-left quadrant of
// the drawing surface and use those coordinates as the starting
// coordinates for a new line.
int x1 = rnd (width / 2);
int y1 = rnd (height / 2);
// Compute the line's ending coordinates by mirroring the starting
// coordinates in the lower-right quadrant.
int x2 = width - x1;
int y2 = height - y1;
// Draw the line.
g.drawLine (x1, y1, x2, y2);
// Draw an inverse of the line (for symmetry).
g.drawLine (x1, y2, x2, y1);
}
static int rnd (int limit)
{
// Return a random integer that ranges from 0 through limit-1.
return (int) (Math.random () * limit);
}
public void run ()
{
// Obtain a reference to the thread that was started in the applet's
// start() method.
Thread current = Thread.currentThread ();
// As long as runner contains the same reference, keep looping. The
// reference in runner becomes null when the applet's stop() method
// executes.
while (current == runner)
{
// Invoke the paint(Graphics g) method to draw another
// randomly colored and randomly positioned line.
repaint ();
// Pause for 50 milliseconds to achieve an eye-pleasing display.
try
{
Thread.sleep (50);
}
catch (InterruptedException e)
{
}
}
}
public void stop ()
{
// Tell the line-drawing thread to terminate.
runner = null;
}
// The following method is overridden to prevent the drawing surface from
// being automatically cleared after a line is drawn. Stay tuned to Java
// 101 to learn more about this method.
public void update (Graphics g)
{
paint (g);
}
}
Listing 1's applet source code describes the creation of a background thread that draws randomly colored lines in random positions.
My extensive comments thoroughly detail this applet's inner workings. I defer a discussion of the public void paint(Graphics g) method to a future article. For those wanting to know more about applets, please consult my "Applications, Applets, and Hybrids" (November 2000) article.