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
main_loop:
 for (int =0; i < 10; i++) {
 inner_loop1:
 while (1) {
 inner_loop2:
 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
multithreading

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

var

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.

Packages

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:

<html>
 <head>
 <title>MacTech sample</title>
 </head>

 <body>
 <hr>
 <applet code="MacTech.class" width=300 height=300> 
 </applet>
 </hr>
 </body>
</html>

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 MacTech.java.

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.red, Color.green, Color.blue,
 Color.yellow, Color.cyan, Color.magenta,
 Color.black };
 
 TargetPanel() {
 setBackground(Color.white);
 }
 
 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.setColor(menuColors[curColorIndex]);
 g.setPaintMode();
 
 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 (inEvent.id) {
 case Event.WINDOW_DESTROY:
 System.exit(0);
 return true;

 default:
 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 {
 TargetPanelthisPanel;

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());
 setBackground(Color.lightGray);
 
 Choice colorMenu = new Choice();

 colorMenu.addItem("Red");
 colorMenu.addItem("Green");
 colorMenu.addItem("Blue");
 colorMenu.addItem("Yellow");
 colorMenu.addItem("Cyan");
 colorMenu.addItem("Magenta");
 colorMenu.addItem("Black");
 
 colorMenu.setBackground(Color.lightGray);
 add(colorMenu);
 }

 public void paint(Graphics g) {
 Rectangle r = bounds();
 
 g.setColor(Color.lightGray);

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 (inEvent.target 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
 thisPanel.SetColorIndex(0);
 else // because of RTTI I can typecast the target object and
    // access the menu index directly
 thisPanel.SetColorIndex(((Choice)
 inEvent.target).getSelectedIndex());

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

 thisPanel.repaint();
 }
 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.

 
AAPL
$95.60
Apple Inc.
-2.55
MSFT
$43.16
Microsoft Corpora
-0.42
GOOG
$571.60
Google Inc.
-15.82

MacTech Search:
Community Search:

Software Updates via MacUpdate

OneNote 15.2 - Free digital notebook fro...
OneNote is your very own digital notebook. With OneNote, you can capture that flash of genius, that moment of inspiration, or that list of errands that’s too important to forget. Whether you’re at... Read more
iStat Menus 4.22 - Monitor your system r...
iStat Menus lets you monitor your system right from the menubar. Included are 8 menu extras that let you monitor every aspect of your system. Some features: CPU -- Monitor cpu usage. 7 display... Read more
Ember 1.8 - Versatile digital scrapbook....
Ember (formerly LittleSnapper) is your digital scrapbook of things that inspire you: websites, photos, apps or other things. Just drag in images that you want to keep, organize them into relevant... Read more
OmniPlan 2.3.6 - Robust project manageme...
With OmniPlan, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success,... Read more
Command-C 1.1.1 - Clipboard sharing tool...
Command-C is a revolutionary app which makes easy to share your clipboard between iOS and OS X using your local WiFi network, even if the app is not currently opened. Copy anything (text, pictures,... Read more
Knock 1.1.7 - Unlock your Mac by knockin...
Knock is a faster, safer way to sign in. You keep your iPhone with you all the time. Now you can use it as a password. You never have to open the app -- just knock on your phone twice, even when it's... Read more
Mellel 3.3.6 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
LibreOffice 4.3.0.4 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
Freeway Pro 7.0 - Drag-and-drop Web desi...
Freeway Pro lets you build websites with speed and precision... without writing a line of code! With it's user-oriented drag-and-drop interface, Freeway Pro helps you piece together the website of... Read more
Drive Genius 3.2.4 - Powerful system uti...
Drive Genius is an OS X utility designed to provide unsurpassed storage management. Featuring an easy-to-use interface, Drive Genius is packed with powerful tools such as a drive optimizer, a... Read more

Latest Forum Discussions

See All

Dawn of the Immortals Review
Dawn of the Immortals Review By Jennifer Allen on July 31st, 2014 Our Rating: :: RESPECTABLE EXPLORATIONUniversal App - Designed for iPhone and iPad Dawn of the Immortals might not re-invent the wheel, but it does tweak it a little... | Read more »
80 Days Review
80 Days Review By Jennifer Allen on July 31st, 2014 Our Rating: :: EPIC ADVENTUREUniversal App - Designed for iPhone and iPad A fantastic and fascinating re-envisioning of the classic novel by Jules Verne, 80 Days is a delightful... | Read more »
Battleheart Legacy Guide
The world of Battleheart Legacy is fun and deep; full of wizards, warriors, and witches. Here are some tips and tactics to help you get the most enjoyment out of this great game. | Read more »
Puzzle Roo Review
Puzzle Roo Review By Jennifer Allen on July 31st, 2014 Our Rating: :: PUZZLE-BASED TWISTUniversal App - Designed for iPhone and iPad A different take on the usual block dropping puzzle game, Puzzle Roo is quite pleasant.   | Read more »
Super Crossfire Re-Release Super Crossfi...
Super Crossfire Re-Release Super Crossfighter Coming Soon, Other Radiangames Titles Go 50% Off Posted by Ellis Spice on July 31st, 2014 [ | Read more »
Hexiled Review
Hexiled Review By Rob Thomas on July 31st, 2014 Our Rating: :: HEX SELLSUniversal App - Designed for iPhone and iPad In space, no one can hear you… spell? Hexiled is a neat concept for a word scramble puzzle, but it doesn’t go too... | Read more »
Summoners War: Sky Arena Passes 10 Milli...
Summoners War: Sky Arena Passes 10 Million Installs! Posted by Jessica Fisher on July 31st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Deep Loot Review
Deep Loot Review By Jennifer Allen on July 31st, 2014 Our Rating: :: DIVE DEEPUniversal App - Designed for iPhone and iPad Dive deep in this fun explore-em-up that’s a little grind heavy but ultimately quite entertaining.   | Read more »
Despicable Me: Minion Rush is One Year O...
Despicable Me: Minion Rush is One Year Old, Gets its Biggest Update Yet Posted by Jennifer Allen on July 31st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Fish & Shark Review
Fish & Shark Review By Jordan Minor on July 31st, 2014 Our Rating: :: FLAPPY FISHUniversal App - Designed for iPhone and iPad Fish & Shark’s beauty is only scale deep.   | Read more »

Price Scanner via MacPrices.net

All Over For Tablets Or Just A Maturing, Evol...
CNN’s David Goldman weighs in on tablet sector doom and gloom, asking rhetorically: “Is this the beginning of the end for the tablet?” Answering that, he contends that hysteria and panic are... Read more
Letterspace 1.0.1 – New Free iOS Text Editor...
Bangkok, Thailand based independent developer Sittipon Simasanti has released Letterspace, a new text editor for iPhone, iPad, and iPod touch devices. Letterspace is a note taking app with an... Read more
Save up to $130 on an iPad mini with Apple re...
The Apple Store has Certified Refurbished 2nd generation iPad minis with Retina Displays available for up to $130 off the cost of new models, starting at $339. Apple’s one-year warranty is included... Read more
iPad Cannibalization Threat “Overblown”
Seeking Alpha’s Kevin Greenhalgh observes that while many commentators think Apple’s forthcoming 5.5-inch panel iPhone 6 will cannibalize iPad sales, in his estimation, these concerns are being... Read more
Primate Labs Releases July 2014 MacBook Pro P...
Primate Labs’ John Poole has posted Geekbench 3 results for most of the new MacBook Pro models that Apple released on Tuesday. Poole observes that overall performance improvements for the new MacBook... Read more
Apple Re-Releases Bugfixed MacBook Air EFI Fi...
Apple has posted a bugfixed version EFI Firmware Update 2.9 a for MacBook Air (Mid 2011) models. The update addresses an issue where systems may take longer to wake from sleep than expected, and... Read more
Save $50 on the 2.5GHz Mac mini, plus free sh...
B&H Photo has the 2.5GHz Mac mini on sale for $549.99 including free shipping. That’s $50 off MSRP, and B&H will also include a free copy of Parallels Desktop software. NY sales tax only. Read more
Save up to $140 on an iPad Air with Apple ref...
Apple is offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. Stock tends to come and go with some of these... Read more
$250 price drop on leftover 15-inch Retina Ma...
B&H Photo has dropped prices on 2013 15″ Retina MacBook Pros by $250 off original MSRP. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.3GHz Retina MacBook Pro: $2249, $250 off... Read more
More iPad Upgrade Musings – The ‘Book Mystiqu...
Much discussed recently, what with Apple reporting iPad sales shrinkage over two consecutive quarters, is that it had apparently been widely assumed that tablet users would follow a two-year hardware... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.