Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Become a programming Picasso with JHotDraw - JavaWorld February 2001

Become a programming Picasso with JHotDraw - JavaWorld February 2001

Tutorial Details:

Become a programming Picasso with JHotDraw
Become a programming Picasso with JHotDraw
By: By Wolfram Kaiser
Use the highly customizable GUI framework to simplify draw application development
oftware developers want to build software applications quickly and still focus on quality. One way to reduce development time and improve software quality is to use a framework. Frameworks are designed for reuse; they offer prefabricated components as building blocks and design patterns as blueprints for the architecture.
Many Java programmers frequently use a framework -- maybe even without realizing it. JFC Swing can be viewed as a rather simple framework for creating almost all general-purpose graphical user interfaces (GUIs). Although you can use it for many applications, JFC Swing lacks a clear structure for a GUI-based application. A more specific framework in that respect is JHotDraw (see Resources for a link), which targets applications for drawing technical and structured graphics -- such as network layouts and Pert diagrams -- and offers much better support to develop editors for those purposes. JHotDraw demonstrates frameworks' power and usefulness within their application domain. Before I examine JHotDraw in detail, I'll discuss frameworks and the concepts they embody.
Theory of frameworks and design patterns
By using frameworks, developers usually reuse not only code, but also the design and architecture of a prototype application. Consequently, frameworks must be tailored to meet the needs of the problem domain. Unlike a class library, frameworks provide not only components, but also a structure for integrating those components, a predefined interoperation of components, and often a basic skeleton of an application. The skeleton is not passive like a class library, but has its own execution path from which user-defined component code is called, resulting in an inversion of control.
Developers often face problems in software design that are recurring and typical of a certain situation. You can capture a proven solution to those problems in a design pattern. A design pattern describes a problem, its context, and a reusable solution. It also assigns the problem a meaningful name, which can be used to communicate the solution to other developers.
Frameworks often rely on design patterns to help achieve a flexible general-purpose application design. The patterns introduce indirections and abstractions, which let you plug in your own classes and components.
A framework helps create an application that is developed in a timely manner and customized to the user's requirements, and still benefits from the framework's maturity with regard to robustness and stability. However, this result comes at a price -- the cost of learning and understanding a framework's interactions and even limitations. Most frameworks are rather complex pieces of software at high levels of abstraction. Understanding a framework can be difficult, and debugging framework code is sometimes cumbersome. Frameworks offer some customization facilities, but they can impose some restrictions and may require special programming techniques, especially if you want to perform functionality slightly out of the framework's defined scope.
Before you use a framework, it is important to understand these things: its strengths and weaknesses, what target applications it addresses, its components and structure, the development process, and its fundamental design patterns and programming techniques.
Description of JHotDraw
In contrast to JFC Swing, JHotDraw defines a basic skeleton for a GUI-based editor with tools in a tool palette, different views, user-defined graphical figures, and support for saving, loading, and printing drawings. The framework can be customized using inheritance and combining components.
Besides the main drawing window, JHotDraw offers little support for different kinds of windows, such as text editors. With some knowledge of JHotDraw's structure, you can extend the framework to include missing functionality. If you run the examples included, you can see what a typical application developed with JHotDraw looks like. (See Resources for information on where to download the package.) For example, JavaDraw is a standard drawing application that provides a good overview of what is possible with JHotDraw. You can start JavaDraw by typing:
java CH.ifa.draw.samples.javadraw.JavaDrawApp
in the directory where you unzipped JHotDraw. In addition, CH.ifa.draw.samples.pert.PertApplication demonstrates some of JHotDraw's customization possibilities.
Figure 1. JavaDraw as a typical
application of JHotDraw. Click
on thumbnail to view full-size
image (17 KB)
JHotDraw is interesting from a software engineering point of view as well. Originally developed in Smalltalk by Kent Beck and Ward Cunningham, JHotDraw was one of the first software development projects explicitly designed for reuse and labeled a framework. It was also documented very early in terms of design patterns (see Resources for a link to "Documenting Frameworks Using Patterns"), and was therefore very influential to the design pattern community. Erich Gamma and Thomas Eggenschwiler developed the original version of JHotDraw. (See Resources for a link.)
This article describes a new version of JHotDraw (v. 5.2) in which the original AWT components have been replaced by their JFC Swing counterparts. It also supports new JFC Swing features like windows with several internal frames, split panes, scrollbars, toolbars, and pop-up menus. Therefore, JHotDraw -- as an application-specific GUI framework -- is based on the general-purpose GUI facilities that the JFC Swing framework offers, but adds its own features and functionality.
Package organization
All JHotDraw classes and interfaces are organized in packages according to their functionality. The package CH.ifa.draw.framework contains mostly interface definitions of core component requirements -- their responsibility, functionality, and interoperation. You can find a standard implementation of these interfaces in CH.ifa.draw.standard . You can locate additional functionality in CH.ifa.draw.figures and CH.ifa.draw.contrib . A skeleton of an application or applet is defined in CH.ifa.draw.application or CH.ifa.draw.applet respectively.
Structure of JHotDraw
A more detailed look at the packages -- in particular, the core framework package -- reveals JHotDraw's structure and shows what role each of its components plays. (See Figure 2 below.)
Figure 2. Basic components of
JHotDraw's architecture. Click
on thumbnail to view full-size
image. (4 KB)
Any application that uses JHotDraw has a window dedicated for drawing. This DrawWindow is the editor window and is a subclass of javax.swing.JFrame . It contains one or more internal frames, each associated with a drawing view. The DrawingView , a subclass of javax.swing.JPanel , is an area that can display a Drawing and accepts user input. Changes in the Drawing are propagated to the DrawingView that is responsible for updating any graphics. The Drawing consists of Figure s, which in turn can be containers for other Figure s. Each Figure has Handle s, which define access points and determine how to interact with the Figure (for example, how to connect the Figure with another Figure ). In a DrawingView , you can select several figures and manipulate them. The DrawWindow itself usually has one active Tool from the tool palette, which operates on the Drawing associated with the current DrawingView .
Typical development process using JHotDraw
The following is a list of recurring tasks involved with developing an application with JHotDraw. The tasks focus on integrating JHotDraw into your application and working together with your object model from the "Problem description for a sample application" section of this article.
Create your own graphical figures and symbols for your application. More often than not, it is necessary to define your own graphical figures. Fortunately, some predefined figures like AbstractFigure , CompositeFigure , and AttributeFigure are already available. You can refine their behavior by subclassing and overriding some methods, such as draw() , to customize the graphical representation in the diagram. Typically, the graphical figures should correspond and somehow relate to the objects used in your application.
Develop your own tools to create figures and manipulate them according to application requirements. Again, JHotDraw offers some starting points: for instance, CreationTool , ConnectionTool , SelectionTool , and TextTool . Subclassing those tools and overriding methods like mouseUp() and mouseDown() allows you to specify your own application interaction and perform the tasks your application needs -- such as manipulating the object defined by your application.
Create the actual GUI and integrate it into your application. Unsurprisingly, JHotDraw already includes a basic application skeleton: either a basic DrawApplication , a MDI_DrawApplication with support for several internal frames, or a DrawApplet . You can define your own menus by refining createMenus() , createFileMenu() , and so on, and plug in new tools by overriding the createTools() method in a subclass. A complete GUI is created when you instantiate your application at runtime and call the open() method.
Compile the applications using javac. It is important to incorporate all packages needed by JHotDraw into the classpath when calling either javac or java.
Some of these tasks involve applying certain design patterns, which I'll discuss later in more detail.
Problem description for a sample application
Before using a framework, it is important to know its target application domain (if there is one) and how the framework addresses problems found within that domain.
The development of a simple class diagram editor called JModeller will serve as a sample application in this article. JModel


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Become a programming Picasso with JHotDraw - JavaWorld February 2001

View Tutorial:
Become a programming Picasso with JHotDraw - JavaWorld February 2001

Related Tutorials:

3D graphics programming in Java, Part 3: OpenGL
3D graphics programming in Java, Part 3: OpenGL
 
Connect the enterprise with the JCA, Part 1
Connect the enterprise with the JCA, Part 1
 
Cut down on logging errors with Jylog
Cut down on logging errors with Jylog
 
XSLT blooms with Java
XSLT blooms with Java
 
A birds-eye view of Web services
A birds-eye view of Web services
 
Jini's relevance emerges, Part 1
Jini's relevance emerges, Part 1
 
Effort on the edge, Part 1
Effort on the edge, Part 1
 
Jini Starter Kit 2.0 tightens Jini's security framework
Jini Starter Kit 2.0 tightens Jini's security framework
 
Improve Application Management With JMX
Improve Application Management With JMX Leverage JMX technology and existing tools to boost the operations management capabilities of your business applications.
 
Control your test-environment with DbUnit and Anthill
The inception of the Extreme Programming methodology has brought test-driven development and continuous integration into mainstream Java development practices. Applying these techniques to Java server-side development can quickly become a nightmare if you
 
The HTML Renderer Shootout, Part 1
The HTML Renderer Shootout, Part 1 In this article, we will review 11 different HTML renderers, comparing their features, compliance, and speed; searching for the best one for any project.
 
JDBC scripting, Part 2
JDBC scripting, Part 2 Programming and Java scripting in JudoScript Summary JudoScript is a rich functional scripting language, and an easy and powerful general programming and Java scripting language. JudoScript's power comes from its synergy of
 
Internet & Intranets: Java Servlets
What are servlets? "Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to upd
 
Professional Java Server Programming.
An overview of the new server-side Java platform - Java 2 Enterprise Edition - as it relates to building n-tier web applications.
 
FreeMarker FreeMarker 2.3.1 an open-source HTML template engine.
FreeMarker provides an easy way to get data from Java servlets into Web pages, and helps you keep graphic design separate from application logic. To use it, you encapsulate HTML in templates.
 
Bioinformatics
Bioinformatics Bioinformatics Introduction Bioinformatics is new hot topic after the Software. In the coming days there will be huge demand of Bioinformatics professionals in all sectors of biotechnology, pharmaceutical, and biomedical sciences.
 
Definition of Bioinformatics
Definition of Bioinformatics Definition of Bioinformatics About Bioinformatics In February 2001, the human genome was finally deciphered! In other words, scientists have succeeded in reading the chain of more than 3 billion base pairs that
 
Building Search Engine Applications Using Servlets !
Building Search Engine Applications Using Servlets ! Building Search Engine Applications Using Servlets Please visit http://www.webappcabaret.com/javadevelopers/search to see running copy of our search engine. Introduction This tutorial takes
 
New Technical Articles: 64-bit Programming on Solaris 10 OS for x86 Platforms
Four technical articles describe the new Sun Studio 10 software's 64-bit programming features on the Solaris 10 OS for x86 and AMD64 platforms. Important issues regarding the AMD64 ABI (Application Binary Interface), debugging, migration to 64-bits, and p
 
Solaris 10 OS Certification Beta Exams
If you are an expert in system and network administration, you can get involved in the creation of three new Solaris 10 certification exams. These Beta exams count toward official Solaris Certification and allow you to provide comments and technical feedb
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.