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.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.