Tuesday, 18 June 2013

11th day of training

 Introduction to GUI 

So far, we have covered most of the basic constructs of Java and introduced the important concept of Object-Oriented Programming (OOP). As discussed, OOP permits higher level of abstraction than the traditional procedural-oriented languages (such as C and Pascal). OOP lets you think in the problem space rather than the computer's bits and bytes. You can create high-level abstract data types calledclasses to mimic real-life things and represent entities in the problem space. These classes are self-contained and are reusable.
In this article, I shall show you how you can reuse the graphics classes provided in JDK for constructing your own Graphical User Interface (GUI) applications. Writing your own graphics classes (re-inventing the wheels) will take you many years! These graphics classes, developed by expert programmers, are highly complex and involve many advanced Java concepts.  However, re-using them are not so difficult, if you follow the API documentation, samples and templates provided.
I shall also describe an important concept called nested class (or inner class) in this article.
There are two sets of Java APIs for graphics programming: AWT (Abstract Windowing Toolkit) and Swing.
  1. AWT API was introduced in JDK 1.0. Most of the AWT components have become obsolete and should be replaced by newer Swing components.
  2. Swing API, a much more comprehensive set of graphics libraries that enhances the AWT, was introduced as part of Java Foundation Classes (JFC) after the release of JDK 1.1. JFC, which consists of Swing, Java2D, Accessibility API, Internationalization, and Pluggable Look-and-Feel Support, was an add-on to JDK 1.1 but has been integrated into core Java since JDK 1.2.
Other than AWT/Swing Graphics APIs provided in JDK, others have also provided Graphics APIs that work with Java, such as Eclipse's Standard Widget Toolkit (SWT), Google Web Toolkit (GWT), 3D Graphics API such as Java bindings for OpenGL (JOGL) and Java3D.

Programming GUI with AWT

Java Graphics APIs - AWT and Swing - provide a huge set of reusable GUI components, such as button, text field, label, choice, panel and frame for building GUI applications. You can simply reuse these classes rather than re-invent the wheels. I shall start with the AWT classes before moving into Swing to give you a complete picture. I have to stress that many AWT component classes are now obsolete. They are used only in exceptional circumstances when the JRE supports only JDK 1.1.

2.1  AWT Packages

AWT is huge! It consists of 12 packages (Swing is even bigger, with 18 packages as of JDK 1.7!). Fortunately, only 2 packages - java.awt and java.awt.event - are commonly-used.
  1. The java.awt package contains the core AWT graphics classes:
    • GUI Component classes (such as ButtonTextField, and Label),
    • GUI Container classes (such as FramePanelDialog and ScrollPane),
    • Layout managers (such as FlowLayoutBorderLayout and GridLayout),
    • Custom graphics classes (such as GraphicsColor and Font).
  2. The java.awt.event package supports event handling:
    • Event classes (such as ActionEventMouseEventKeyEvent and WindowEvent),
    • Event Listener Interfaces (such as ActionListenerMouseListenerKeyListener and WindowListener),
    • Event Listener Adapter classes (such as MouseAdapterKeyAdapter, and WindowAdapter).
AWT provides a platform-independent and device-independent interface to develop graphic programs that runs on all platforms, such as Windows, Mac, and Linux.

No comments:

Post a Comment