TweetFollow Us on Twitter

Java Intro
Volume Number:12
Issue Number:3
Column Tag:Internet Tools

Writing Java 102

Moving beyond “Hello, World” - way, way beyond

By Richard Cardona, TradeWave Corporation

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

In an attempt to carve a path through the forest of current announcements, discussion, and interest in Java, let me lay out the components of Java and then proceed to a discussion in detail [see also MacTech Magazine 12.1 (January, 1996) 51-57 - man].

Java consists of a programming language, a virtual machine architecture, and a runtime environment developed by Sun Microsystems. The most common instance of Java technology is a Web browser running applets with their own interface. These applets are downloaded in-line on a web page, just like images. Thus applets can be served by all standard web servers, including those on the Mac, as just another MIME type.

HotJava was the first Java-enabled Web browser. It was authored by Sun completely in Java, to prove that complex applications could be written in Java. A Java-aware browser runs applets with the help of the Java Virtual Machine, an interpreter of bytecodes produced by a Java compiler. Although bytecodes are currently interpreted, Sun expects to provide just-in-time compilation to improve speed and rival native applications.

The most compelling reason for applets is that they let dynamic web media be targeted for content processing by the client. Unlike today’s Common Gateway Interface programs (CGI), Java applets execute on the client machine in the browser. This relieves the burden on many servers that are far too busy already, and reduces server cost, in a sense by introducing distributed computing over the net.

Lest the idea of horrible viruses loom in your mind, consider that applets run under a layered, well-defined environment with user-controlled security restrictions. Applets could be authenticated with public-key technology and encrypted in transit (this is not implemented yet).

The built-in Java classes are very net-savvy, and provide for firewalls, proxies, and default access control (for accessing the originating server only). Depending on security, applets can seamlessly access other program components and data, such as images and databases, across the Internet. These features are why Java has been dubbed the Internet programming language.

As of this writing, Sun is working toward implementing the Java Virtual Machine under the MacOS, and is also porting its HotJava browser to the Mac. Netscape is also implementing a plug-in that allows Java applets to run in Navigator 2.0. On the development side, Metrowerks and Symantec both announced Java support by adding a new compiler to their suite of compiler plug-ins [and Borland recently made a similar announcement - jaw]. Natural Intelligence of Cambridge, MA, was first to bring Java to the Mac, by delivering their Java IDE, Roaster, at MacWorld San Francisco in January; they implemented their Java environment working entirely from the published Java specification. All compiler vendors are pledged to support applet and stand-alone application development, integrated debugging, and a standard Java Virtual Machine environment.

Another related component, JavaScript, became necessary to bridge the technical boundary between an HTML author with scripting skills and a full-fledged programmer. The Java language is too unwieldy for a scripting-level webmaster, partially because it also requires an external development environment. JavaScript is the result of Sun and Netscape collaborating on an object-based end-user scripting language capable of generating Java applets transparently. To minimize the need for learning several scripting languages on several platforms, and to leverage the name recognition of Java, Sun and Netscape agreed on enhancing Navigator 2.0’s LiveScript. JavaScript and the Java language share no syntax or similarities. JavaScript will most likely appeal to AppleScript-savvy users as a way to script Netscape.

History of Java

Originally named Oak at Sun Microsystems, Java was intended to be the simple, object-oriented, distributed language for the much-hyped $500 Internet set-top boxes. At first, C++ was the language of choice for these consumer devices; but Sun decided that constant recompilation of code, version control, and problems with pointers, memory leaks, and security, to name a few, were defeating the development effort. James Gostling, a principal architect of Java, decided he needed a new small, reliable, portable, distributed, interactive language - one that would facilitate distribution of updates, would attend to network security, and would not be as fragile about changes in source code of classes.

The fragility of compiled code, even with shared libraries, was a primary influence in Oak’s design. Oak needed the functionality of binary-compatible libraries similar to IBM’s System Object Model (SOM) in a small footprint. Through its simplicity (and real-world shipping schedule!), Oak fulfilled its intended purpose for coding small, distributed, robust, platform-independent agents.

Unfortunately, the market and network infrastructure for Sun’s appliances did not materialize. In late 1994, Oak was renamed and redirected towards the Internet. A large part of the success of Java has been Sun’s brilliant positioning for the Web. After brainstorming for the name “Java”, Sun sold the idea of applets to Netscape, and devised the plan to write the HotJava browser to prove the language.

Amidst significant press coverage and extravagant predictions, Sun’s strategy had worked. All the talk was of how to develop desktop applications that always load the latest code from a server, and how applications could even be used on a metered basis vs. purchased and licensed, while Java itself was still in beta. Even Microsoft, aware of the momentum of Java, and at least a few months behind in defining a similar language, redirected Blackbird, its Visual Basic/OLE-centric paradigm, and embraced Java in its Internet strategy. (Technically, Blackbird lacked the cross-platform momentum of Java because of the dependency on OLE; Blackbird was renamed to Internet Studio and now focuses on implementing custom publishing needs with OLE objects and Visual Basic support.)

Some Mac enthusiasts feared that Java was in direct competition with OpenDoc and Cyberdog. In reality, though, Java represents another type of OpenDoc data that Cyberdog interfaces with over the Internet. Cyberdog [see MacTech 12.2 (February, 1996) 35-39 - man] is a collection of OpenDoc parts Apple is designing to show how document-centric computing works with the Internet. Apple or a third party will simply implement an OpenDoc part that knows how to interpret Java bytecodes, and presto, Java will be accessible with OpenDoc. Parts for OpenDoc can be written in a variety of programming languages because there is a language-neutral binary interface for linking and binding with a part’s interface. Since Java is a programming language, it’s likely that a proper interface for developing OpenDoc parts in Java will be defined; Java allows for external routines to be implemented on a specific platform, and this would be the facility necessary to interface with OpenDoc.

Language Overview

The Java language was still in beta specification at the time of this writing. Java is undergoing peer review on the Internet with enhancements made to usability and security. It is unlikely that any major changes to the language will occur in the interval before this article appears, though. Most of the enhancements and changes are in the large number of built-in classes the Java runtime environment comes with.

Java as a language is difficult to describe because it resembles so many languages including SmallTalk, Objective C, C++ and Eiffel. Fortunately, the similarities go a long way toward learning Java for most programmers. Java was developed in the same time frame as Apple Dylan, and represents a trend of reversal against the size and complexity of C++. In this regard, Java is a simpler, strongly typed, strongly object-oriented, memory management-safe, exception- and thread-aware language.

I’m going to start by using C++ as a reference and describing what is not in Java. From there I will list some features Java adds to help you program more efficiently. Of course, nothing replaces actually coding in the language itself, but this is a good start.

Java, as opposed to C++, has:

• no preprocessor and no typedefs

• no stand-alone procedures and functions

• no structures or unions

• no unsigned numeric types

• no automatic type coercions, e.g. long to short (with data loss)

• no multiple inheritance and operator overloading

• no goto statement (goto is reserved)

• no pointers

• no multiple expression comma operator

This may sound a little limited and constrained, but let me describe what Java does offer. Because Java doesn’t allow #define or typedef, when you look at a data member, you don’t have to guess what it is; it’s either a built-in type or a class. Without structs and unions, you must organize your data structures by using classes. By disallowing stand-alone functions, Java forces you to organize your access to your data and provide a public interface to class members. This means you must play by true object-oriented rules.

Another nice feature to help you (my favorite) is labels on continue and break statements. When you’re deep in several embedded loops, you no longer have to set flags to let you know when to break out of each one; you can break out to the exact level you want. Many routines have program states, graphics states or other conditions that must be restored before exiting. If you use try/catch blocks you will see code duplication in the catch sections to restore exit conditions. Similar code duplication manifests itself with routines that have multiple return statements; each return statement must carry some baggage, such as clearing a flag. The finally block in Java is the answer to both of these problems; it is a code block that executes whenever its scope exits, and this includes exceptions, break, continue and return statements. The following are code samples:

Labeled break and continue
 for (int =0; i < 10; i++) {
 while (1) {
 do {
 if (fatal())
 break main_loop;// leaves main_loop
 } while (!tired())
 if (not_so_bad())
 continue inner_loop1;  // continues at for

The finally block
while (loop_condition())
 try {
 if (need_to_exit_loop())
 break; // leaves try block because of loop
    if (Process()) // calls throw
 if (Possible_Early_Exit())
 return;// function return statement
 catch (Exception e) {  // catch clause is optional
 throw e; // pass exception to next handler
 finally {
 // always executes - regardless of exit condition

Java defines the exact sizes of the basic data types. In C/C++, lack of platform definitions of the basic data types leads to significant porting problems, when dealing with the int type for instance.

Java shares with C++ the following data types:

char double float long

int short

and the following keywords:

break case catch class

continue default do else

for if new null

private protected public return

static switch this throw

try void while

Java and C/C++ share all the respective operators that do not involve pointers (Java has no pointers):

= > < ! ~ ? :

== <= >= != || && ++

-- + - * / & |

^ % << >> >>> += -=

*= /= &= |= ^= %= <<=

>>= >>>=

You will have noticed two unfamiliar operators above, namely the >>> and >>>= operators. This is an unsigned right shift. It always shifts 0 in the upper bit of numeric value. This was included because Java has no unsigned numeric types.

The following are reserved words new to Java:

abstract used to define abstract base classes

boolean logical data type

byte 8-bit signed numeric data type

extends defines the base class you are subclassing

final a class or variable that can not be subclassed

finally extension to try/catch

implements importing method interfaces

import including other class definitions

instanceof RunTime Type Identification operator

interface declaration of a package’s public interface

native platform-specific routine

package namespace container of classes

super instance of a class’s superclass

synchronized locks out variable or method access for

throws declares possible exception types

transient optimization for non-persistent data

volatile turns off all data access optimization

Reserved but not actually implemented are:

byvalue cast const future generic

goto inner operator outer rest


Java data types are:

boolean can only be assigned literals true or false;
not an int nor a char

char 16-bit Unicode Standard character

byte 8-bit signed number

short 16-bit signed number

int 32-bit signed number

long 64-bit signed number

float 32-bit IEEE 754 floating point number

double 64-bit IEEE 754 floating point number

The Java Virtual Machine specifies network order (big-endian) byte ordering in memory. The byte ordering is irrelevant in most cases since each Java Virtual Machine on its platform will hide the native byte ordering.

The char data type is based on The Unicode Standard: Worldwide Character Encoding, Version 1. Unicode is a 16-bit character standard which incorporates ASCII and most of the world’s non-Roman character sets. All modern operating systems are standardizing around Unicode; the MacOS will support it under Copland. ASCII is binary compatible with the least significant 8 bits of Unicode characters.

An interesting feature of the basic types is that they come with corresponding type wrapper classes whose static members perform common utility functions. For instance, to check if a character is lowercase you call Character.isLowerCase('c'), or you can convert an integer to a string by wrapping it as:

Integer i = new Integer(12);
String s = i.toString();

Classes and Objects

In Java, all code and data are encapsulated in a class. A class is a template for creating an object that has a state and a behavior. All Java classes derive from the Java Object class, which is purely an abstract base class. Class hierarchy is important in Java because there is no multiple inheritance; which class you choose to subclass defines the context with which you begin adding functionality to create your program. Lacking multiple inheritance, the Java class hierarchy looks like a rooted file system with Object at the root. For instance, if you subclass the Applet class you inherit the framework (data members and methods) for being invoked as an applet within a Web browser.

Interface vs. Implementation

You will notice in the “Hello world” example (see below) that the class definition does not include separate interface and implementation sections. In Java, whenever you define an interface, you define a template for methods that will be implemented in a class that imports them. This is where Java differs most from C/C++: you choose a class you wish to subclass from and inherit its data members, but you import any number of class interfaces to implement, as with Objective C. If you have members that are reserved for the class or its subclasses, you do not have to repetitiously define a method and then implement it; you simply implement it.


To organize better the built-in classes and to help you organize your classes, Java offers the package unit. This is similar to creating or importing class definitions via the #include mechanism in C/C++ or the uses statement in Pascal. You can organize your class’s public interfaces into your own package so that others may import and use them. Packages are also a scope with respect to namespace and the default access specifier. The default access specifier is the access you allow when you don’t specify public, protected, or private on a member. When other classes import your package, only the public interfaces and object members with public access are available. Within a package you have access to the object members with public, protected and default access.

Class Member Access Control

An interesting difference between C++ and Java lies in how they protect class members. C++ specifies public, protected and private access, defaulting to private. Java has those access specifiers too, but there is a difference in the meaning of protected and also a difference with the default access specifier. In C++, when a member is declared protected, only members of the class, subclasses, and friends of the class may access that member. In Java, protected allows the class, subclasses, and all the classes in a package to have friendly access as well. When a class wants to have stricter control over a data member, you declare that data member as private protected. The private protected access specifier was a recent addition to the beta specification and allows for the same level of protection as the protected declaration in C++. A little chart will summarize:

C++ data Java data Visibility

member access member access

public public all

protected private protected class, subclasses

private private class only

friend all classes in a package

n/a (default) all classes in a package

Another difference from C++ is the concept of final members. Any class or data member may be defined as final. In the case of a class this means that no other class may subclass it. (A class author would have to have fairly good reason to state that no one else may subclass a class beyond the current hierarchy.) In the case of a data member, it means that the variable must be assigned an initial value in its declarator. Final data members are also not assignable, which effectively renders them to a constant; it is unclear what Sun has planned for the const keyword.

String and Arrays

In Java, a string, like everything else, is a class. This, as opposed to being defined as a static character array, means you can you easily copy, concatenate, compare, and convert strings to a number, all with a standard interface. Actually, Java strings are static objects, so if you wish to use a string with a dynamic length you use the StringBuffer class. The StringBuffer and String classes are complementary, so they can interchange data easily.

Java supports only single-dimension arrays, but you can contain other array references and access them with additional [] pairs. An array is also an object in Java, and its sole method you can access is the array’s length(). Java allows to you specify initial values for array elements but you must provide a value for each element. An array is declared like this:

fixed size array datatypeIdentifier[numElements] = 
 { value1, value2, ...  }

You may also create a reference to an array and allocate the array elements at runtime by leaving the number of elements unspecified, e.g.:

short MyArray[];

To allocate storage for MyArray:

MyArray = new short[20];

Attempting to access MyArray before allocating memory will throw a nullPointer Exception. Similarly, if there is not enough memory in the heap for the array you request, you will raise an exception.

Memory Management

Programming the Macintosh, as fun as it can be, is sometimes problematic in C/C++, especially with handles that can move during some system calls. In Java, wondering when and if to free memory is not a problem, because you can only control when you allocate. Since Java is threaded, it has a low-priority thread whose only job is to free the memory of objects that are no longer in use. Because Java does not have real pointers, any time you leave the scope of an array reference or an object reference, the memory it occupies becomes a candidate for the automatic garbage collector. If memory is tight, the garbage collector can temporarily get a high priority to free all available memory blocks.

Thread Support

The thread support in Java is more than simply adding a thread package class to the language and allowing you to create new threads. Java gives you built-in semaphores with the synchronize keyword for locking critical sections of code at the block code, method and even data member level. Of course there is a performance penalty for using the synchronize modifier, but with proper program design you minimize the variables or sections that need protection.

There are two ways of including a multithreaded aspect to your code. The first is simply to subclass the Thread class and override the run method. Instantiating a class which subclasses the Thread class makes it eligible for thread execution. The start method in the Thread class places the thread in the spool for timeslicing. You can modify the priorities of your threads to manage the CPU balancing. The second way of implementing threading is to import the Runnable interface. You use this approach if you want to give specific CPU time to a visual element or guarantee response to a user interface element. The Runnable interface includes the same basic methods in the Thread class without the overhead.

Java Security

Users wary of leaving their Mac to the mercy of rogue applets written at the whim of any old web page author will be glad to know that the specifications for the Java class loader (the component of the Java Virtual Machine that loads applets) is very strict and suspicious of all code. The incoming bytecodes are not trusted even to be the unmodified output of a trusted Java compiler.

First, all memory references, stack balancing, and array indices are checked to make sure the applet will not make illegal references or behave improperly. Next, the embedded data type information is used to verify that by changing a load instruction you cannot forge an integer into an array or object reference. The type enforcement at the bytecode level is quite strict: each data type has its own specific Load and Store instruction, to prevent coercion. Including this data type information into the compilation output makes any changes after compilation difficult. (It has other benefits that we’ll come to in a moment.)

Once basic integrity verification passes, network classes are loaded into a namespace separate from local classes. This is important because Java gives precedence to classes in the local namespace before searching the network class namespace. Thus no applet can ever override built-in classes.

After all the classes an applet needs are loaded, all the static memory allocation is instantiated. The Java Virtual Machine makes no provision for the ordering of data members in memory, which means each JVM has complete discretion. This is an additional factor against someone attempting to compromise Java security; if you do not know the memory layout of your classes you will not have a clue as to how to access the data illegally.

Network classes can have severe access restrictions controlled entirely by the user. This means you control the level of local file and network access. Most browsers will specify read-only file access into the application directory only; and the default network setting allows you to connect only to the server whence the applet originates. Users who are more confident about the Java applets they run can enable full Internet access, and applets may then also write into the local file system in a designated directory. It is also possible, just the other way, for a paranoid user to disallow all network and file access and force an applet to run self-contained.

Java RunTime Environment

The Java Virtual Machine, which has already been mentioned several times, is the heart of the runtime architecture; it is what actually runs the applets. The first implementations of the JVM will interpret bytecodes, but the bytecode format was developed with native compilation in mind. The JVM Specification lists bytecode instructions complete with opcodes and operand ordering. Anyone implementing a JVM could define criteria for converting bytecodes into machine code. This would be very similar to the dynamic recompilation emulator on current Power Macintosh models, but better defined.

The process of generating native code is much more efficient than normal compilation because in Java the bytecode stream includes data type information for fast linking. (Recall that the data type information is also a critical component of the security verification mechanism.) Sun claims internal testing shows Java applets running as fast as C programs. As for performance in an emulated environment, your mileage may vary; rough estimates put Java emulation, in worst cases, about 10 times slower than native applications, and some Web browser users have complained about the speed in beta versions of plug-ins or with beta versions of the JVM. Although many early Java programmers have created impressive illustrations of Java’s capabilities given these constraints and a changing API, everyday users will have the final say on whether Java implementations will require native translation.

A Java Stand-Alone Application

The Java stand-alone application environment supports both a command-line console-style programming interface and a complete graphics user interface environment. Since Java applications support the classical C programming interface, you can write programs that accept arguments from a command line and write to a terminal console.

Hello World Applet

Here is the bare minimum Applet from the Sun Java Home Page. It could probably be trimmed further, but it illustrates the significance of importing an existing class hierarchy, subclassing the Applet class, and overriding methods as a means of implementing your statement.

Hello World
import browser.Applet;
import awt.Graphics; 

class HelloWorld extends Applet {
 public void init() {// entry point for initializing
 resize(150, 25);// establish drawing size

 public void paint(Graphics g) {
 g.drawString("Hello world!", 50, 25);

The AWT Class Library

Java includes an applet and application framework not unlike MacApp or PowerPlant. If it didn’t, Java would not be a good model of object-oriented programming and re-usable software engineering. The Java class hierarchy includes a small toolbox called the Abstract Window Toolkit (AWT), which is like a combination of light versions of Quickdraw, TextEdit, Menu Manager, Window Manager and Control Manager. The AWT is not as well organized as a traditional application framework, so you must still decide which components to instantiate or whose static class members you can access directly. But it helps you get a lot done if you feel you need to create your own interface in a first class Macintosh Window with other Macintosh user interface elements. The AWT affords you this ability from within an applet in a platform-independent manner.

Note that Java Security will warn the user blatantly that your window or dialog originates from an untrusted Java applet. This prevents you from designing an interface to deceive a naive user into typing a password into your dialog or creating the illusion of a secure Netscape transaction.

The focus of the AWT class hierarchy centers on drawing. The Graphics class implements methods for rendering lines, ovals, arcs, rectangles, round rectangles, and text in different fonts. But upon closer inspection you see it has classes for drawing images like GIFs, moving pixel regions like CopyBits(), dithering colors, and even offscreen buffering. As the AWT expands, Sun will probably implement support for JPEG images and possibly MPEG or QuickTime.

Here is a list of the Abstract Window Toolkit Classes:

BorderLayout CardLayout CheckboxGroup Color

Button Canvas Checkbox Choice Panel

Window Dialog Frame Label List

Scrollbar TextField Dimension Event FileDialog

FlowLayout Font FontMetrics Graphics GridLayout

Image Insets MediaTracker MenuBar MenuItem

Point Polygon Rectangle Toolkit

A Simple Applet Example

Now that we’ve explored the basics of Java, let’s implement a slightly more advanced applet than “Hello World”. It would be nice to illustrate Java’s built-in classes for WWW, ftp, and news, but there are possible security restrictions and likely changes to the Java classes, so instead I have decided to implement a standalone application with simple user interface. This will illustrate the necessary elements for managing drawing contexts and event processing. My example renders a colored rectangle in a Pane that is controlled by a PopUp Menu in a separate Pane. The main Applet class instantiates both Panes and defines the layout relationship. As an exercise, you might change the positioning of the Panes or the layout model to investigate the options the Java visual hierarchy provides.

Figure 1. The Applet running within a browser.

HTML for Applets

Before getting to the actual code, we need to set up a Web page that will serve my Java applet. To specify an applet in an HTML document, you simply use the applet tag. The HTML used to display the applet is:

 <title>MacTech sample</title>

 <applet code="MacTech.class" width=300 height=300> 

The applet tag is actually the second iteration of specifying Java applets. Applets using the alpha specification of Java were defined using an app tag which is recognized only by the HotJava browser. As of this writing the standardization of the fields in the applet tag was still incomplete. Some of the optional fields include specifying beta, version number, applet command-line arguments, and source directories for .class files and Java libraries.

The applet bytecodes are sent with the MIME type application/java. Some Web servers may need to be configured to send this MIME type as a binary file so that no inadvertent text conversion is applied. Browsers that do not understand the applet tag simply ignore it and do not attempt to retrieve applet bytecodes as a file whose contents they would not understand.

In Java, groups of classes are bundled with the package unit. Whenever other programs need to access the public interface to a package, they declare their use with the import statement. In this applet, we are using the AWT and Applet classes in the Java Class Hierarchy. The * indicates we need subsequent subclasses as well.

import java.awt.*;
import java.applet.*;

Each compilation unit in Java has exactly one public interface. In the case where you do not specify a package, your classes go into an unnamed “default” package. Also, a restriction of the beta Java Development Kit requires the name of the source file containing a public interface to match the name of the public class. In this case, the file that contains this source is called

Below, the MacTech class is subclassing the standard Applet class and overriding the required Init() method. By subclassing, you gain much drawing and event-handling functionality automatically, allowing you to focus on your solution.

public class MacTech extends java.applet.Applet {

The init function is called when a Browser instantiates your instance of the Applet class. Any pre-flighting, memory allocation, etc. can be performed to determine if the applet has all the resources it needs to continue.

 public void init() {
 resize(300, 300); // drawing area

The following line may look strange at first to a C/C++ programmer. But since Java does not have a way for the programmer to delete dynamically-allocated memory, it is OK not to save the result of the new() operator. Whenever the applet is done using the Border Layout object, the garbage collector will recover the memory it allocated.

 setLayout(new BorderLayout());
 TargetPanel theTarget = new TargetPanel();

The base drawing context in most C++ application frameworks is the Pane class; the equivalent Java class is Panel. A view hierarchy is created by positioning and creating superviews and their sub-Panels. In the Java AWT, various layout objects enforce positioning and function as superviews to user interface elements. The default border layout object carves the display area into equal areas denoted by directional labels North, South, East, West and Center. (Other layout modes let you have tiled rows, columns and grid patterns. The Sun Java example code program “Cards” provides a visual map of how each layout pattern is defined.)

 add("Center", theTarget);// creates drawing area
 add("South",new PopUpMenu(theTarget));
 // creates pop-up menu

The Applet class implements a Panel interface. In this case we have defined the positioning of our drawing area (theTarget) to be at the center with a Popup Menu Panel directly below it.

The following classes implement the functionality of the Panels instantiated by the main Applet class. The TargetPanel’s job is to reflect the current status and updates controlled by PopUpMenuPanel class. Notice how Panels can be event-driven, much like OpenDoc parts.

class TargetPanel extends Panel {

The private protected access specifier has been discussed above:

 private protected int curColorIndex;
 private protected Color menuColors[] = 
 Color.yellow, Color.cyan, Color.magenta, };
 TargetPanel() {
 public void SetColorIndex(int inColorIndex) {
 curColorIndex = inColorIndex;

The Graphics class contains all the QuickDraw-like routines for drawing arcs, ovals, rectangles and round rectangles. The paint method of a Panel is called in response to update events only, so take this into consideration when threads or other updating alters state information that an update routine depends on.

 public void paint(Graphics g){
 g.fillRect(10,10, 100, 100);

Panels are equipped to handle all user-input events like keyboard input, mouse movement, clicks, and dragging. Most event processing can be left to the built-in classes. In this case, I’m trapping the event sent to a Panel when the browser is closing the window. I could prompt the user for input, or alter the handling of the event.

 public boolean handleEvent(Event inEvent) {
 switch ( {
 return true;

 return false;

Rather than implement an entire menu bar for a menu, I decided to opt for the popup menu. In Java, the popup menu interface element is called Choice selection. In order to respond to the user, I let the Panel perform the normal event processing regarding the tracking and updating. By overriding the action method, I am able to respond only to changes in the value of the PopUp menu and send an update signal to the target Panel.

class PopUpMenu extends Panel {

The constructor for our class receives a reference to the Panel that will need updates when the value of the PopUp Menu is changed. As of this writing, Java lacks a formal resource format; thus all user interface elements must be hard-coded. Bummer. It’s likely that IDE vendors on multiple platforms, or perhaps Sun, may define a resource format to allow strings, menus and control titles to be defined in an external manner.

 public PopUpMenu(TargetPanel inMenu) {
 thisPanel = inMenu;
 setLayout(new FlowLayout());
 Choice colorMenu = new Choice();


 public void paint(Graphics g) {
 Rectangle r = bounds();

This is one my favorite Graphics toolkit calls. It paints those 3D-like shadows around a standard rectangle to yield a bevel. Maybe an Apple engineer can add one to Copland.

 g.draw3DRect(0, 0, r.width, r.height, false);

Generally in a layout you may have several user interface elements of various object types. To identify the correct object type you may use Runtime Type Indentification (RTTI) before attempting to typecast and access a particular method. The instanceof operator in Java returns true if an object is a proper instance of the given class.

 public boolean action(
 Event inEvent, Object inObject) {
 if ( instanceof Choice) {                       
 // Correct object type - PopUpMenu
 String menuSelection = (String) inObject;

Since we are responding to changes in the PopUpMenu control, the action method gives us the current value. The value of a Choice object is a String. This is a little inconvenient since we cannot use Strings in a switch statement. Also it duplicates the references to the contents of the menu, making program maintenance difficult. I illustrate how to process the normal value returned and a way around relying on the String itself.

 if (menuSelection.equals("Red"))
    // processing standard value returned
 else // because of RTTI I can typecast the target object and
    // access the menu index directly

Send the update signal to the Drawing Panel so the change can be updated immediately.

 return true;

The End

This should be enough to get you going [and there will be more in future issues - jaw]. You will find the URLs for companies that are producing Java compilers and other Java-related resources in the Uniform Resource Locator column.


Community Search:
MacTech Search:

Software Updates via MacUpdate

Tidy Up 5.3.7 - Find duplicate files and...
Tidy Up is a full-featured duplicate finder and disk-tidiness utility. Features: Supports Lightroom: it is now possible to search and collect duplicates directly in the Lightroom library. Multiple... Read more
Pinegrow 5.97 - Mockup and design web pa...
Pinegrow (was Pinegrow Web Designer) is desktop app that lets you mockup and design webpages faster with multi-page editing, CSS and LESS styling, and smart components for Bootstrap, Foundation,... Read more
BlueStacks 4.210.0 - Run Android applica...
BlueStacks App Player lets you run your Android apps fast and fullscreen on your Mac. Feature comparison chart How to install Bluestacks on your Mac Go to MacUpdate and click the green "Download"... Read more
WhatsApp 2.2027.10 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Art Text 4.0.1 - $29.99
Art Text is graphic design software specifically tuned for lettering, typography, text mockups and various artistic text effects. Supplied with a great variety of ready to use styles and materials,... Read more
Adobe Dreamweaver CC 2020 20.2 - Build w...
Dreamweaver CC 2020 is available as part of Adobe Creative Cloud for as little as $20.99/month (or $9.99/month if you're a previous Dreamweaver customer). Adobe Dreamweaver CC 2020 allows you to... Read more
Adobe Acrobat DC 20.009.20074 - Powerful...
Acrobat DC is available only as a part of Adobe Creative Cloud, and can only be installed and/or updated through Adobe's Creative Cloud app. Adobe Acrobat DC with Adobe Document Cloud services is... Read more
beaTunes 5.2.10 - Organize your music co...
beaTunes is a full-featured music player and organizational tool for music collections. How well organized is your music library? Are your artists always spelled the same way? Any R.E.M. vs REM?... Read more
DiskCatalogMaker 8.1.5 - Catalog your di...
DiskCatalogMaker is a simple disk management tool which catalogs disks. Simple, light-weight, and fast Finder-like intuitive look and feel Super-fast search algorithm Can compress catalog data for... Read more
Meteorologist 3.4.1 - Popular weather ap...
Meteorologist is a simple interface to weather provided by It provides the ability to show the weather in the main menu bar, displaying more detail in a pop-up menu, whose contents are... Read more

Latest Forum Discussions

See All

Steam Link Spotlight - Disco Elysium
Steam Link Spotlight is a feature where we look at PC games that play exceptionally well using the Steam Link app. Our last entry was Signs of the Sojourner Read about how it plays using Steam Link over here. | Read more »
Distract Yourself With These Great Mobil...
There’s a lot going on right now, and I don’t really feel like trying to write some kind of pithy intro for it. All I’ll say is lots of people have been coming together and helping each other in small ways, and I’m choosing to focus on that as I... | Read more »
Pokemon Go's July Community Day wil...
Pokemon Go developers have announced the details concerning the upcoming Gastly Community Day. This particular event was selected by the players of the game after the Gas Pokemon came in second place after a poll that decided which Pokemon would... | Read more »
Clash Royale: The Road to Legendary Aren...
Supercell recently celebrated its 10th anniversary and their best title, Clash Royale, is as good as it's ever been. Even for lapsed players, returning to the game is as easy as can be. If you want to join us in picking the game back up, we've put... | Read more »
Detective Di is a point-and-click murder...
Detective Di is a point-and-click murder mystery set in Tang Dynasty-era China. You'll take on the role of China's best-known investigator, Di Renjie, as he solves a series of grisly murders that will ultimately lead him on a collision course with... | Read more »
Dissidia Final Fantasy Opera Omnia is se...
Dissidia Final Fantasy Opera Omnia, one of Square Enix's many popular mobile RPGs, has announced a plethora of in-game events that are set to take place over the summer. This will include several rewards, Free Multi Draws and more. [Read more] | Read more »
Sphaze is a neat-looking puzzler where y...
Sphaze is a neat-looking puzzler where you'll work to guide robots through increasingly elaborate mazes. It's set in a visually distinct world that's equal parts fantasy and sci-fi, and it's finally launched today for iOS and Android devices. [... | Read more »
Apple Arcade is in trouble
Yesterday, Bloomberg reported that Apple is disappointed in the performance of Apple Arcade and will be shifting their approach to the service by focusing on games that can retain subscribers and canceling other upcoming releases that don't fit... | Read more »
Pixel Petz, an inventive platform for de...
Pixel Petz has built up a sizeable player base thanks to its layered, easy-to-understand creative tools and friendly social experience. It revolves around designing, trading, and playing with a unique collection of pixel art pets, and it's out now... | Read more »
The King of Fighters Allstar's late...
The King of Fighters ALLSTAR, Netmarble's popular action RPG, has once again been updated with a plethora of new content. This includes battle cards, events and 21 new fighters, which increases the already sizeable roster even more. [Read more] | Read more »

Price Scanner via

$219 Apple AirPods Pro are back at Verizon, s...
Verizon has Apple AirPods Pro on sale again for a limited time for $219.99 on their online store. Their price is $30 off Apple’s MSRP, and it’s the lowest price we’ve seen for AirPods Pro. Available... Read more
Apple’s $779 13″ MacBook Air deal returns to...
Apple has clearance, Certified Refurbished, 2019 13″ MacBook Airs available again starting at $779. Each MacBook features a new outer case, comes with a standard Apple one-year warranty, and is... Read more
$200 13″ MacBook Pro discounts are back at Am...
Amazon has 2020 13″ 2.0GHz MacBook Pros on sale again today for $150-$200 off Apple’s MSRP. Shipping is free. Be sure to purchase the MacBook Pro from Amazon, rather than a third-party seller, and... Read more
Deal Alert! Apple AirPods with Wireless Charg...
Sams Club has Apple AirPods with Wireless Charging Case on sale on their online store for only $149.98 from July 6, 2020 to July 9, 2020. Their price is $50 off Apple’s MSRP, and it’s the lowest... Read more
Xfinity Mobile promo: Apple iPhone XS models...
Take $300 off the purchase of any Apple iPhone XS model at Xfinity Mobile while supplies last. Service plan required: – 64GB iPhone XS: $599.99 save $300 – 256GB iPhone XS: $749.99 save $300 – 512GB... Read more
New July 2020 promo at US Cellular: Switch an...
US Cellular has introduced a new July 2020 deal offering free 64GB Apple iPhone 11 smartphones to customers opening a new line of service. No trade-in required, and discounts are applied via monthly... Read more
Apple offers up to $400 Education discount on...
Apple has launched their Back to School promotion for 2020. They will include one free pair Apple AirPods (with charging case) with the purchase of a MacBook Air, MacBook Pro, iMac, or iMac Pro (Mac... Read more
July 4th Sale: Woot offers wide range of Macs...
Amazon-owned Woot is blowing out a wide range of Apple Macs and iPads for July 4th staring at $279 and ranging up to just over $1000. Models vary from older iPads and 11″ MacBook Airs to some newer... Read more
Apple Pro Display XDR with Nano-Texture Glass...
Abt Electronics has Apple’s new 32″ Pro Display XDR model with the nano-texture glass in stock and on sale today for up to $144 off MSRP. Shipping is free: – Pro Display XDR (nano-texture glass): $... Read more
New 2020 Mac mini on sale for up to $100 off...
Amazon has Apple’s new 2020 Mac minis on sale today for $40-$100 off MSRP with prices starting at $759. Shipping is free: – 2020 4-Core Mac mini: $759 $40 off MSRP – 2020 6-Core Mac mini: $998.99 $... Read more

Jobs Board

Physical Therapist Assistant - *Apple* Hill...
Physical Therapist Assistant - Apple Hill Rehab - Full Time Tracking Code 62519 Job Description General Summary: Under the direct supervision of a licensed Physical Read more
Operating Room Assistant, *Apple* Hill Surg...
Operating Room Assistant, Apple Hill Surgical Center - Full Time, Day Shift, Monday - Saturday availability required Tracking Code 62363 Job Description Operating Read more
Perioperative RN - ( *Apple* Hill Surgical C...
Perioperative RN - ( Apple Hill Surgical Center) Tracking Code 60593 Job Description Monday - Friday - Full Time Days Possible Saturdays General Summary: Under the Read more
Product Manager, *Apple* Commercial Sales -...
Product Manager, Apple Commercial Sales Austin, TX, US Requisition Number:77652 As an Apple Product Manager for the Commercial Sales team at Insight, you Read more
*Apple* Mac Product Engineer - Barclays (Uni...
Apple Mac EngineerWhippany, NJ Support the development and delivery of solutions, products, and capabilities into the Barclays environment working across technical Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.