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
$116.47
Apple Inc.
+0.16
MSFT
$47.98
Microsoft Corpora
-0.72
GOOG
$537.50
Google Inc.
+2.67

MacTech Search:
Community Search:

Software Updates via MacUpdate

Cobook 3.0.7 - Intelligent address book....
Cobook Contacts is an intuitive, engaging address book. Solve the problem of contact management with Cobook Contacts and its simple interface and powerful syncing and integration possibilities.... Read more
StatsBar 1.9 - Monitor system processes...
StatsBar gives you a comprehensive and detailed analysis of the following areas of your Mac: CPU usage Memory usage Disk usage Network and bandwidth usage Battery power and health (MacBooks only)... Read more
Cyberduck 4.6 - FTP and SFTP browser. (F...
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
Maya 2015 - Professional 3D modeling and...
Maya is an award-winning software and powerful, integrated 3D modeling, animation, visual effects, and rendering solution. Because Maya is based on an open architecture, all your work can be scripted... Read more
Evernote 6.0.1 - Create searchable notes...
Evernote allows you to easily capture information in any environment using whatever device or platform you find most convenient, and makes this information accessible and searchable at anytime, from... Read more
calibre 2.11 - Complete e-library manage...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital... Read more
Herald 5.0.1 - Notification plugin for M...
Note: Versions 2.1.3 (for OS X 10.7), 3.0.6 (for OS X 10.8), and 4.0.8 (for OS X 10.9) are no longer supported by the developer. Herald is a notification plugin for Mail.app, Apple's Mac OS X email... Read more
Firetask 3.7 - Innovative task managemen...
Firetask uniquely combines the advantages of classical priority-and-due-date-based task management with GTD. Stay focused and on top of your commitments - Firetask's "Today" view shows all relevant... Read more
TechTool Pro 7.0.6 - Hard drive and syst...
TechTool Pro is now 7, and this is the most advanced version of the acclaimed Macintosh troubleshooting utility created in its 20-year history. Micromat has redeveloped TechTool Pro 7 to be fully 64... Read more
PhotoDesk 3.0.1 - Instagram client for p...
PhotoDesk lets you view, like, comment, and download Instagram pictures/videos! (NO Uploads! / Image Posting! Instagram forbids that! AND you *need* an *existing* Instagram account). But you can do... Read more

Latest Forum Discussions

See All

Ubisoft Gives Everyone Two New Ways to E...
Ubisoft Gives Everyone Two New Ways to Earn In-Game Stuff for Far Cry 4 Posted by Jessica Fisher on November 21st, 2014 [ permalink ] | Read more »
Golfinity – Tips, Tricks, Strategies, an...
Dig this: Would you like to know what we thought of being an infinite golfer? Check out our Golfinity review! Golfinity offers unlimited ways to test your skills at golf. Here are a few ways to make sure your score doesn’t get too high and your... | Read more »
Dark Hearts, The Sequel to Haunting Meli...
Dark Hearts, The Sequel to Haunting Melissa, is Available Now Posted by Jessica Fisher on November 21st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Meowza! Toyze Brings Talking Tom to Life...
Meowza! | Read more »
Square Enix Announces New Tactical RPG f...
Square Enix Announces New Tactical RPG for Mobile, Heavenstrike Rivals. Posted by Jessica Fisher on November 21st, 2014 [ permalink ] With their epic stories and gorgeous graphics, | Read more »
Quest for Revenge (Games)
Quest for Revenge 1.0.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.0 (iTunes) Description: The great Kingdom of the west has fallen. The gods ignore the prayers of the desperate. A dark warlord has extinguished... | Read more »
Threadz is a New Writing Adventure for Y...
Threadz is a New Writing Adventure for You and Your Friends Posted by Jessica Fisher on November 21st, 2014 [ permalink ] In the tradition of round-robin storytelling, | Read more »
SteelSeries Stratus XL Hardware Review
Made by: SteelSeries Price: $59.99 Hardware/iOS Integration Rating: 4 out of 5 stars Usability Rating: 4.5 out of 5 stars Reuse Value Rating: 4.25 out of 5 stars Build Quality Rating: 4.5 out of 5 stars Overall Rating: 4.31 out of 5 stars | Read more »
ACDSee (Photography)
ACDSee 1.0.0 Device: iOS iPhone Category: Photography Price: $1.99, Version: 1.0.0 (iTunes) Description: Capture, perfect, and share your photos with ACDSee. The ACDSee iPhone app combines an innovative camera, a powerful photo... | Read more »
ProTube for YouTube (Entertainment)
ProTube for YouTube 2.0.2 Device: iOS Universal Category: Entertainment Price: $1.99, Version: 2.0.2 (iTunes) Description: ProTube is the ultimate, fully featured YouTube app. With it's highly polished design, ProTube offers ad-free... | Read more »

Price Scanner via MacPrices.net

27-inch 3.5GHz 5K iMac in stock today and on...
 B&H Photo has the new 27″ 3.5GHz 5K iMac in stock today and on sale for $2299 including free shipping plus NY sales tax only. Their price is $200 off MSRP, and it’s the lowest price available... Read more
21-inch 1.4GHz iMac on sale for $979, save $1...
B&H Photo has the new 21″ 1.4GHz iMac on sale for $979.99 including free shipping plus NY sales tax only. Their price is $120 off MSRP. B&H will also include free copies of Parallels Desktop... Read more
13-inch 1.4GHz/256GB MacBook Air on sale for...
B&H Photo has lowered their price on the 13″ 1.4GHz/256GB MacBook Air to $1059.99 including free shipping plus NY sales tax only. Their price is $140 off MSRP, and it’s the lowest price for this... Read more
Save up to $400 with Apple refurbished 2014 1...
The Apple Store has restocked Apple Certified Refurbished 2014 15″ Retina MacBook Pros for up to $400 off the cost of new models. An Apple one-year warranty is included with each model, and shipping... Read more
New 13-inch 1.4GHz MacBook Air on sale for $8...
 Adorama has the 2014 13″ 1.4GHz/128GB MacBook Air on sale for $899.99 including free shipping plus NY & NJ tax only. Their price is $100 off MSRP. B&H Photo has the 13″ 1.4GHz/128GB MacBook... Read more
Apple Expected to Reverse Nine-Month Tablet S...
Apple and Samsung combined accounted for 62 percent of the nearly 36 million branded tablets shipped in 3Q 2014, according to early vendor shipment share estimates from market intelligence firm ABI... Read more
Stratos: 30 Percent of US Smartphone Owners t...
Stratos, Inc., creator of the Bluetooth Connected Card Platform, has announced results from its 2014 Holiday Mobile Payments Survey. The consumer survey found that nearly one out of three (30 percent... Read more
2014 1.4GHz Mac mini on sale for $449, save $...
 B&H Photo has lowered their price on the new 1.4GHz Mac mini to $449.99 including free shipping plus NY tax only. Their price is $50 off MSRP, and it’s the lowest price available for this new... Read more
Check Apple prices on any device with the iTr...
MacPrices is proud to offer readers a free iOS app (iPhones, iPads, & iPod touch) and Android app (Google Play and Amazon App Store) called iTracx, which allows you to glance at today’s lowest... Read more
64GB iPod touch on sale for $249, save $50
Best Buy has the 64GB iPod touch on sale for $249 on their online store for a limited time. Their price is $50 off MSRP. Choose free shipping or free local store pickup (if available). Sale price for... Read more

Jobs Board

*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
*Apple* Solutions Consultant (ASC)- Retail S...
**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
Project Manager, *Apple* Financial Services...
**Job Summary** Apple Financial Services (AFS) offers consumers, businesses and educational institutions ways to finance Apple purchases. We work with national and Read more
*Apple* Store Leader Program - College Gradu...
Job Description: Job Summary As an Apple Store Leader Program agent, you can continue your education as you major in the art of leadership at the Apple Store. You'll Read more
*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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.