Java Document
Java Document
which requires understanding of some large libraries, and fairly advanced aspects of Java. In a
graphical system, a windowing toolkit is usually responsible for providing a framework to make it
relatively painless for a graphical user interface (GUI) to render the right bits to the screen at the
right time. Both the AWT (abstract windowing toolkit) and Swing provide such a framework. In this
report, we designed and developed a simple painter project used to enable a user to draw any shape
and any integrated graphic with any color using FreeHand (move the mouse using your hand to draw
any shape and specify the coordinate in JPanel). Several tools such as Undo and Redo process, Clear
JPanel, Set Background Color & set Foreground Color, Save paint (Panel) to file ( *. JPG; *. GIF; *.* ),
and Open paint from image file are considered. The purpose of this project is to give you practice
with graphical user interface programming in Java. This project implemented using the components
from Java's awt and swing library in the Java programming language (NetBenas IDE7.2.1). As the final
result of our project is enabling you to use FreeHand to draw as an easy way to draw the Circle, Line,
Rectangle, Square, and Oval, and integrated graphics such as a car, a street, a football stadium,
traffic signals and others. Keywords: NetBeans IDE 7.2.1, AWT, Swing, GUI. 2 Contents
ABSTRACT.................................................................................................................................... 1 1.
Introduction ............................................................................................................................. 3 1.1
Overview.......................................................................................................................... 3 1.2 Object-
oriented Programming.......................................................................................... 3 1.3 The Basic GUI
(graphical user interface ) Application ................................................... 4 2. Graphics and
Painting.............................................................................................................. 6 2.1 Overview of the
Java 2D API Concepts .......................................................................... 6 2.2
Coordinates ...................................................................................................................... 7 2.3
Colors............................................................................................................................... 8 2.4
Shapes .............................................................................................................................. 9 2.5
Graphics2D .................................................................................................................... 11 3. Painting in
AWT and Swing.................................................................................................. 12 3.1 Evolution of the
Swing Paint System............................................................................. 13 3.2 Painting in
AWT ............................................................................................................ 13 4. Design and
Development our project .................................................................................... 16 4.1 Program Ability
(Objectives):........................................................................................ 17 4.2 System
Framework......................................................................................................... 17 4.3
Components.................................................................................................................... 18 4.4 Program
Structure and Results....................................................................................... 19 4.4.1 Preview
(System Interface)..................................................................................... 19 4.4.2 Undo and
Redo ....................................................................................................... 21 4.4.3
SetColor .................................................................................................................. 22 4.4.4
setBackColor........................................................................................................... 23 4.4.5 Save to
File ............................................................................................................. 24 4.4.6 Open Image from
File............................................................................................. 25 5.
Conclusion.............................................................................................................................. 25
References..................................................................................................................................... 26 3
1. Introduction 1.1 Overview Java is a general-purpose, concurrent, class-based, object-oriented
computer programming language that is specifically designed to have as few implementation
dependencies as possible. It is intended to let application developers "write once, run anywhere"
(WORA), meaning that code that runs on one platform does not need to be recompiled to run on
another. Java applications are typically compiled to byte code (class file) that can run on any Java
virtual machine (JVM) regardless of computer architecture. Java is, as of 2012, one of the most
popular programming languages in use, particularly for client-server web applications, with a
reported 10 million users [1][2]. Java was originally developed by James Gosling at Sun Microsystems
(which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun
Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has
fewer lowlevel facilities than either of them. Java [3] can be used to write applications and applets. A
Java application is similar to any other high-level language program: It can only be compiled and
then run on the same machine. An applet is compiled on one machine, stored on a server in binary,
and can be sent to another machine over the Internet to be interpreted by a Java-aware browser.
Java comes with a large library of ready-made classes and objects. The key difference between Java
1.0 and 1.1 was in this library. Similarly, Java 2.0 has a very much larger library for handling user
interfaces (Swing by name) but only small changes to the core of the language. 1.2 Object-oriented
Programming Java supports object-oriented programming techniques that are based on a hierarchy
of classes and well-defined and cooperating objects [4]. Classes: A class is a structure that defines
the data and the methods to work on that data. When you write programs in Java, all program data
is wrapped in a class, whether it is a class you write or a class you use from the Java API libraries.
Classes in the Java API libraries define a set of objects that share a common structure and behavior.
Objects: An instance is a synonym for object. A newly created instance has data members and
methods as defined by the class for that instance. Well-Defined Boundaries and Cooperation: Class
definitions must allow objects to cooperate during execution. 4 Inheritance and Polymorphism: One
object-oriented concept that helps objects work together is inheritance. Inheritance defines
relationships among classes in an object-oriented language. The relationship is one of parent to child
where the child or extending class inherits all the attributes (methods and data) of the parent class.
In Java, all classes descend from java.lang.Object and inherit its methods. Figure 1 shows the class
hierarchy as it descends from java.lang.Objectfor the classes in the user interface example above.
The java.lang.Objectmethods are also shown because they are inherited and implemented by all of
its subclasses, which is every class in the Java API libraries. java.lang.Objectdefines the core set of
behaviors that all classes have in common. Object Containert Panel Applet SimpleApplet Figure 1.
Object Hierarchy 1.3 The Basic GUI (graphical user interface ) Application There are two basic types
of GUI program in Java [5]: stand-alone applications and (online) applets. An applet is a program that
runs in a rectangular area on a Web page. Applets are generally small programs, meant to do fairly
simple things, although there is nothing to stop them from being very complex. A GUI program offers
a much richer type of user interface, where the user uses a mouse and keyboard to interact with GUI
components such as windows, menus, buttons, check boxes, text input boxes, scroll bars, and so on.
JFrame and JPanel: In a Java GUI program, each GUI component in the interface is represented by an
object in the program. One of the most fundamental types of component is the window. Windows
have many behaviors. They can be opened and closed. They can be resized. 5 They have “titles” that
are displayed in the title bar above the window. And most important, they can contain other GUI
components such as buttons and menus. Java, of course, has a built-in class to represent windows.
There are actually several different types of window, but the most common type is represented by
the JFrame class (which is included in the package javax.swing). A JFrame is an independent window
that can, for example, act as the main window of an application. One of the most important things to
understand is that a JFrame object comes with many of the behaviors of windows already
programmed in. In particular, we has one Jframe “InterfaceForm” that contents all the components,
which enables the user to work on our system easily and draw any shape in Panel and the ability to
be opened and closed. JPanel is another of the fundamental classes in Swing [6]. The basic JPanel is,
again, just a blank rectangle. There are two ways to make a useful JPanel : The first is to add other
components to the panel; the second is to draw something in the panel. Both of these techniques
are illustrated in our sample project. In fact, you will find JPanel in the program: content, display
Panel, which is used as a drawing surface. Components and Layout: Another way of using a JPanel is
as a container to hold other components. In our project, we used NetBeans IDE 7.2.1 to create all
components in JFrame and JPanel. Events and Listeners: The structure of containers and
components sets up the physical appearance of a GUI, but it doesn’t say anything about how the GUI
behaves. That is, what can the user do to the GUI and how will it respond? GUIs are largely event-
driven; that is, the program waits for events that are generated by the user’s actions (or by some
other cause). When an event occurs, the program responds by executing an event-handling method.
In order to program the behavior of a GUI, you have to write event-handling methods to respond to
the events that you are interested in. The most common technique for handling events in Java is to
use event listeners. A listener is an object that includes one or more event-handling methods. When
an event is detected by another object, such as a button or menu, the listener object is notified and
it responds by running the appropriate event-handling method. An event is detected or generated
by an object. Another object, the listener, has the responsibility of responding to the event. The
event itself is actually represented by a third object, which carries information about the type of
event, when it occurred, and so on. This division of responsibilities makes it easier to 6 organize large
programs. As an example, consider the Undo or Redo button in our sample program. When the user
clicks the button, an event is generated. 2. Graphics and Painting Everything you see on a computer
screen has to be drawn there, even the text. The (online) Java API includes a range of classes and
methods that are devoted to drawing. In this section, I’ll look at some of the most basic of these that
helping us to achieve our project. The physical structure of a GUI is built of components. The term
component refers to a visual element in a GUI, including buttons, menus, text-input boxes, scroll
bars, check boxes, and so on. In Java, GUI components are represented by objects belonging to
subclasses of the class java.awt.Component. In a graphical system, a windowing toolkit is usually
responsible for providing a framework to make it relatively painless for a graphical user interface
(GUI) to render the right bits to the screen at the right time. Both the AWT (abstract windowing
toolkit) and Swing provide such a framework. But the APIs that implement it are not well understood
by some developers -- a problem that has led to programs not performing as well as they could. In
order to use graphics in Java programs, there are a number of libraries we need to import. For the
sake of what will be covered in these notes, you need the following statements: import
javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.geom.*
import java.awt.Color; import javax.swing.JLabel; import java.awt.event.WindowAdapter; import
java.awt.event.WindowEvent; import javax.swing.ImageIcon; import java.awt.BorderLayout; import
java.awt.event.*; import java.awt.image.BufferedImage; import
java.awt.image.MemoryImageSource; import java.awt.image.PixelGrabber; import java.io.File;
import java.io.IOException; import javax.imageio.*; 2.1 Overview of the Java 2D API Concepts The
Java 2D™ API [7] provides two-dimensional graphics, text, and imaging capabilities for Java™
programs through extensions to the Abstract Windowing Toolkit (AWT). This comprehensive
rendering package supports line art, text, and images in a flexible, full-featured 7 framework for
developing richer user interfaces, sophisticated drawing programs, and image editors. Java 2D
objects exist on a plane called user coordinate space, or just user space. When objects are rendered
on a screen or a printer, user space coordinates are transformed to device space coordinates. The
following links are useful to start learning about the Java 2D API: Graphics class [8] Graphics2D
class [9] The Java 2D API provides following capabilities: