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
$101.06
Apple Inc.
+0.10
MSFT
$47.06
Microsoft Corpora
-0.46
GOOG
$587.37
Google Inc.
-8.71

MacTech Search:
Community Search:

Software Updates via MacUpdate

Evernote 5.6.0 - 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
Monosnap 2.2.2 - Versatile screenshot ut...
Monosnap allows you to save screenshots easily, conveniently, and quickly, sharing them with friends and colleagues at once. It's the ideal choice for anyone who is looking for a smart and fast... Read more
Tunnelblick 3.4beta36 - GUI for OpenVPN...
Tunnelblick is a free, open source graphic user interface for OpenVPN on OS X. It provides easy control of OpenVPN client and/or server connections. It comes as a ready-to-use application with all... Read more
SoftRAID 5.0.4 - High-quality RAID manag...
SoftRAID allows you to create and manage disk arrays to increase performance and reliability. SoftRAID's intuitive interface and powerful feature set makes this utility a must have for any Mac OS X... Read more
Audio Hijack Pro 2.11.3 - Record and enh...
Audio Hijack Pro drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio with Audio Hijack... Read more
Airfoil 4.8.9 - Send audio from any app...
Airfoil allows you to send any audio to AirPort Express units, Apple TVs, and even other Macs and PCs, all in sync! It's your audio - everywhere. With Airfoil you can take audio from any... Read more
WhatRoute 1.13.0 - Geographically trace...
WhatRoute is designed to find the names of all the routers an IP packet passes through on its way from your Mac to a destination host. It also measures the round-trip time from your Mac to the... Read more
Chromium 37.0.2062.122 - Fast and stable...
Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all Internet users to experience the web. FreeSMUG-Free OpenSource Mac User Group build is... Read more
Attachment Tamer 3.1.14b9 - Take control...
Attachment Tamer gives you control over attachment handling in Apple Mail. It fixes the most annoying Apple Mail flaws, ensures compatibility with other email software, and allows you to set up how... Read more
Duplicate Annihilator 5.0 - Find and del...
Duplicate Annihilator takes on the time-consuming task of comparing the images in your iPhoto library using effective algorithms to make sure that no duplicate escapes. Duplicate Annihilator detects... Read more

Latest Forum Discussions

See All

This Week at 148Apps: September 15-19, 2...
Expert App Reviewers   So little time and so very many apps. What’s a poor iPhone/iPad lover to do? Fortunately, 148Apps is here to give you the rundown on the latest and greatest releases. And we even have a tremendous back catalog of reviews; just... | Read more »
Kitty Powers’ Matchmaker – Tips, Tricks,...
Hey There, Kittens: | Read more »
Goblin Sword Review
Goblin Sword Review By Andrew Fisher on September 22nd, 2014 Our Rating: :: RETRO GOODNESSUniversal App - Designed for iPhone and iPad Fun visuals, good music, engaging level design, and lots of content make Goblin Sword an... | Read more »
Major New Update for CSR Racing Adds Fer...
Major New Update for CSR Racing Adds Ferrari and Multiplaye​r Posted by Jessica Fisher on September 22nd, 2014 [ permalink ] | Read more »
Veditor Review
Veditor Review By Jennifer Allen on September 22nd, 2014 Our Rating: :: PIMP YOUR VIDEOUniversal App - Designed for iPhone and iPad Want to add stickers and music to your videos? Veditor can do that easily.   | Read more »
1849′s Nevada Silver DLC is Still Search...
A few months ago, I took a look at 1849 from SomaSim. This Gold Rush-themed city builder for iPad had a fair bit going for it, but lacked in a few crucial areas to make it a true stand-out on the App Store. SomaSim has since added in a sandbox mode... | Read more »
Fruit Ninja Will be Reborn With a Massiv...
Fruit Ninja Will be Reborn With a Massive Update and Origins Animation Series Posted by Jessica Fisher on September 22nd, 2014 [ permalink ] Halfbrick Studios is rebuilding | Read more »
Daniel Tiger’s Grr-ific Feelings Review
Daniel Tiger’s Grr-ific Feelings Review By Amy Solomon on September 22nd, 2014 Our Rating: iPad Only App - Designed for the iPad Daniel Tiger’s Grr-ific Feelings includes activities that allow young children explore different... | Read more »
CloudMagic Updated for iOS 8 – Adds Inte...
CloudMagic Updated for iOS 8 – Adds Interactive Notifications, Share Extension, and More Posted by Jessica Fisher on September 22nd, 2014 [ | Read more »
Starbase Annex (Games)
Starbase Annex 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: "it’s really very clever... a little bit of Hearthstone and a dash of Eclipse" - PocketTactics.com From the creator of Starbase... | Read more »

Price Scanner via MacPrices.net

Apple restocks some refurbished 2014 MacBook...
The Apple Store has restocked some Apple Certified Refurbished 2014 MacBook Airs, with prices starting at $769. An Apple one-year warranty is included with each MacBook, and shipping is free. These... Read more
13-inch 128GB MacBook Air on sale for $949, s...
B&H Photo has the new 2014 13″ 1.4GHz/128GB MacBook Air on sale for $949.99 including free shipping plus NY tax only. Their price is $50 off MSRP. B&H will also include free copies of... Read more
Apple offering free $25 iTunes Gift Card with...
The Apple Store is offering a free $25 iTunes Gift Card with the purchase of a $99 Apple TV for a limited time. Shipping is free. Read more
Apple refurbished iPod touch available for up...
The Apple Store has Apple Certified Refurbished 5th generation iPod touches available starting at $149. Apple’s one-year warranty is included with each model, and shipping is free. Most colors are... Read more
iFixIt Tears Down iPhone 6; Awards Respectabl...
iFixit notes that even the smaller 4.7″ iPhone 6 is a giant among iPhones; so big that Apple couldn’t fit it into the familiar iPhone form factor. In a welcome reversal of a recent trend to more or... Read more
Phone 6 Guide – Tips Book For Both iPhone 6...
iOS Guides has announced its latest eBook: iPhone 6 Guide. Brought to you by the expert team at iOS Guides, and written by best-selling technology author Tom Rudderham, iPhone 6 Guide is packed with... Read more
How to Upgrade iPhone iPad to iOS 8 without D...
PhoneClean, a iPhone cleaner utility offered by iMobie Inc., reveals a solution for upgrading iPhone and iPad to iOS 8 without deleting photos, apps, the new U2 album or anything. Thanks to more than... Read more
Inpaint 6 – Photo Retouching Tool Gets Faster...
TeoreX has announced Inpaint 6, a simple retouching tool for end users that helps remove scratches, watermarks, and timestamps as well as more complex objects like strangers, unwanted elements and... Read more
Worldwide PC Monitor Market Sees Growth in To...
Worldwide PC monitor shipments totaled 32.5 million units in the second quarter of 2014 (2Q14), a year-over-year decline of -2.9%, according to the International Data Corporation (IDC) Worldwide... Read more
Updated Price Trackers
We’ve updated our Mac Price Trackers with the latest information on prices, bundles, and availability on systems from Apple’s authorized internet/catalog resellers: - 15″ MacBook Pros - 13″ MacBook... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Position Opening at *Apple* - Apple (United...
…customers purchase our products, you're the one who helps them get more out of their new Apple technology. Your day in the Apple Store is filled with a range of 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
Position Opening at *Apple* - Apple (United...
**Job Summary** At the Apple Store, you connect business professionals and entrepreneurs with the tools they need in order to put Apple solutions to work in their Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** The Apple Store is a retail environment like no other - uniquely focused on delivering amazing customer experiences. As an Expert, you introduce people Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.