Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

makes hundreds of existing look and feels

OOPR212 REVIEWER available to Swing programs. Many more


look-and-feel packages are available from
Week 2 Module 1 GUI Basics various sources.

Introduction to GUI Building Accessibility API - enables assistive


technologies, such as screen readers and Braille
What is the Importance of having a Graphical User displays, to get information from the user
Interface? interface.

GUI Java 2D API - enables developers to easily


- GUI means Graphical User Interface. incorporate high-quality 2D graphics, text, and
- GUI is a Graphical Interface which is a visual images in applications and applets. Java 2D
representation of communication presented to includes extensive APIs for generating and
the user for easy interaction with the machine. sending high-quality output to printing devices.
- It is the common user Interface that includes
Graphical representation like buttons and icons Internalization - allows developers to build
and communication can be performed by applications that can interact with users
interacting with these icons rather than the usual worldwide in their own languages and cultural
text-based or command-based communication. conventions. With the input method framework
- Program interface that takes advantage of the developers can build applications that accept
computer’s graphics capabilities to make the text in languages that use thousands of different
program easier to use characters, such as Japanese, Chinese, or
- Designed in 1970 by Xerox Corporation’ s Palo Korean.
Alto Research Center

Basic Components Java AWT


- Pointer
- Pointing device
- Icons
- Desktop
- Windows
- Menu

JAVA Gui
- JAVA provides a rich set of libraries to create
Graphical User Interface in a platform
independent way.
- In Java, GUI-based programs are implemented
by using the classes from the standard
- Javax.swing (Swing classes) and - Java’s platform-independent windowing,
- java.awt (AWT classes) packages. graphics, and user-interface widget toolkit part of
- All GUI classes are based on the fundamental the Java Foundation Classes (JFC)
classes in the Application Windowing Toolkit or - The standard API (Application Program
AWT. Interface) for providing graphical user interfaces
for Java programs contains the fundamental
About the JFC and Swing classes used for constructing GUIs.
- JFC is short for Java Foundation Classes,
which encompasses a group of features for AWT Components and Containers
building graphical user interfaces (GUIs) and
adding rich graphics functionality and
interactivity to Java applications.

JFC Features

Swing GUI Components - Includes everything


from buttons to split panes to tables. Many
Components are capable of sorting, printing,
and drag and drop, to name a few of the
supported features.
Java Swing
Pluggable Look-and-Feel Support - the look - Graphical user interface toolkit in Java, which is
and feel of Swing applications is pluggable, one part of the Java Foundation Classes (JFC)
allowing a choice of look and feel. For example , - Includes graphical user interface (GUI) widgets
the same program can use either the Java or the such as text boxes, buttons, split-panes, and
Windows look and feel. Additionally, the Java tables.
platform supports the GTK+ look and feel, which - Runs the same on all platforms.
- Supports pluggable look and feel - not by Import javax.swing.JOptionPane;
using the native platform’s facilities, but by - The JOptionPane class is not
roughly emulating them. automatically available to your Java
- Classes are often described as lightweight programs. This statement tells the
because they do not require allocation of native compiler where to find the JOptionPane
resources in the operating system’s window class.
toolkit.
- UI elements such as dialog boxes and buttons; showMessageDialog[]
you can usually recognize their names because
they begin with J.
- Each Swing component is a descendant of a
JComponent, which in turn inherits from the
java.awt.Container class.

You insert the important statement


Import javax.swing.*;
at the beginning of your Java program files so you can
take advantage of the Swing UI components and their
methods.
Example 1:
Dialog displaying “Hello World!” , with OK
Hierarchy of Java Swing Classes
button, standard “info” icon, and title
“Message”.

JOptionPane.showMessageDialog(null, “Hello!”);

Example 2:
Dialog displaying “Hello!” , with OK button, no
Graphical Input and Output with Option Panes ] standard icon, and title “Sample ONLY”.

JOptionPane class - The JOptionPane class is int messageType = JOptionPane.PLAIN_MESSAGE;


used to provide standard dialog boxes such as JOptionPane.showMessageDialog(null, “Hello” ,
message dialog box, confirm dialog box and “Sample ONLY” , messageType);
input dialog box.
- These dialog boxes are used to display Message Types
information or get input from the user.
The JOptionPane class inherits messageType - defines the style of the
JComponent class. message.The Look and Feel manager may lay
out the dialog differently depending on this
value, and will often provide a default icon. The
showConfirm Dialog - Allows for various combinations possible values are:
of OK, YES, NO, and CANCELno buttons. (Ask a
confirming question, like yes/no/cancel) - ERROR_MESSAGE
- INFORMATION_MESSAGE
showInputDialog - Allows for the user to enter arbitrary - WARNING_MESSAGE
information either in a text field or with a combo-box - QUESTION_MESSAGE
style list of values. (Prompt for some input) - PLAIN_MESSAGE
- It provides only OK and CANCEL buttons.

showMesageDialog - allows for only for a message


and an OK button. (Tell the user about something
that has happened)

showOptionDialog - allows you to display buttons


containing the text of your choice. (The Grand
Unification of the above three)

Using the import Statement


showInputDialog[] Coordinates and Dimensions

Java’s Coordinate System


- To begin drawing in Java, we must first
understand Java’s coordinate system which is a
scheme for identifying every point on the screen.
By default, the upper-left corner of a GUI
component (e.g. a window) has a coordinates
(0,0). A coordinate pair is composed of an
x-coordinate (the horizontal coordinate) and
a y-coordinate (the vertical coordinate).

Example 3:
Dialog with empty text field, “Enter value:”
prompt string, plus OK and CANCEL buttons.

showconfirmDialog[]

The class Point


- A location of a painted element is usually
represented by an object of the class
java.awt.Point. Such an object contains
two integer public members (the
coordinates) :
- x- the x coordinate and
Example 4:
- y - the y coordinate
Dialog displaying “Hello!” with
- The constructor point (int x, int y)
YES+NO+CANCEL buttons, standard “question
initializes them with the given values.
mark” icon, and title “Select an Option”
Methods of this class enable simple
operations on points of a plane.void
int res = JOptionPane.showConfirmDialog(null,
move (int x, int y) - moves this point to
“Hello!”);
the specified position.
Option Types
The class Dimension
- The class java.awt.Dimension is used for
optionType - defines the set of option buttons
representing dimensions of components
That appear at the bottom of the dialog box:
and other graphical objects. An object of
this class comprises two public fields:
- DEFAULT_OPTION
width and height.
- YES_NO_OPTION
- The constructor Dimension (int width,
- YES_NO_CANCEL_OPTION
int height) initializes them with the given
- OK_CANCEL_OPTION
values.
Week 3 Module 2: The Graphics Class
Working with Color Class and Fonts
The Graphics Class
Color Class
- Java uses a graphics context - an object of the
- Color objects can be created using a
class java.awt.Graphics - for painting on
constructor:
components.
Public Color(int red, int green, int blue) where
- This class includes methods for drawing
each int value satisfies 0=val=255.
geometrical figures as well as external,
ready-to-use images.
For example:
Color mine = new Color(255, 204, 153);
Displaying Graphics
java.awt.Graphics.
- class provides many methods for graphics
programming.
Week 4 Module 3: Graphics Primitives

Drawing Lines
- Draw a line between point (x1,y1) to point
(x2,y2).

drawLine(x1,y1, x2,y2 );

Example:
g.drawLine (10, 30, 90, 30);

Color Constant
- RED
- GREEN
- BLUE
- ORANGE
- PINK Drawing rectangle
- CYAN - Draws a rectangle of the specified width and
- MAGENTA height. The top-left corner of the rectangle has
- YELLOW the coordinate (x,y).
- BLACK
- WHITE drawRect( x,y, width, height );
- GRAY
- LIGHT_GRAY Example:
- DARK_GRAY g.drawRect(10, 40, 40, 30);

Font Class
- Font objects can be created using the
constructor:
Public Font(String name, int style, int points)
And the constants:

Font.PLAIN, Font.BOLD, and Font.ITALIC.

Examples:

Font f1 = new Font(“Helvetica”, Font.BOLD, 18);


Font f2 = new Font(“Courier”, Font.PLAIN, 12);
- Logical Names (preffered)
- Dialog, DialogInput, Monospaced, Serif,
SansSerif.
Drawing solid rectangle ARC WIDTH AND ARC HEIGHT FOR ROUNDED
- Draws a solid rectangle of the specified width RECTANGLE
and height. The top-left corner of the rectangle
has the coordinate (x, y)

fillRect( x, y, width, height );

Example:
g.fillRect (10, 40, 40, 30);

DRAWING OVALS

Drawing Oval
- Draws an oval in the current color with the
specified width and height. The bounding
rectangle’s top-left corner is ate coordinates
(x , y). The oval touches all four sides of the
bounding rectangle at the center of each side.

Drawing Round rectangle drawOval( x, y, width, height );


- Draws a rectangle with rounded corners in the
current color with the specified width and height. Example:
The arcWidth and arcHeight determine the g.drawOval (10, 20, 30, 50);
rounding of the corners.

drawRoundRect( x, y, width, height, arcWidth,


arcHeight ) ;

Example:
g.drawRoundRect ( 10, 20, 60, 50, 20, 20 );

ARC WIDTH AND ARC HEIGHT FOR ROUNDED


RECTANGLE

- Draws a filled oval in the current color with the


specified width and height. The bounding
rectangle’s top-left corner is ate coordinates
(x , y). The oval touches all four sides of the
bounding rectangle at the center of each side.

fillOval( x, y, width, height );

Example:
g.fillOval (10, 20, 30, 50);
Drawing rectangle
- Draws a solid rectangle with rounded corners in
the current color with the specified width and
height. The arcWidth and arcHeight determine
the rounding of the corners.

fillRoundRect( x, y, width, height, arcWidth,


arcHeight );

Example:
g.fillRoundRect( 10, 20, 60, 50, 20, 20 );
arcWidth and arcHeight determine the rounding
DRAWING ARCS of the corners. True means raised.
fill3DRect(x,y,width,height,[true,false]);
Drawing Arcs
- Draws an arc relative to the bounding Example:
rectangle’s top-left coordinates (x, y) with the g.fill3DRect(250,40,50,50,true);
specified width and height. The arc segment is
drawn starting at startAngle and sweeps
arcAngle degrees. CREATING POLYGONS

drawArc (x, y, width, height, startAngle, arcAngle); Drawing 3D rectangle


- Draws the specified closed polygons
Example: drawPolygon(Polygon p);
g.drawArc(30, 20, 40, 40, 0, -270);
Polygon p = new Polygon();
p.addPoint(165,135);
p.addPoint(175,150);
p.addPoint(270,200);
p.addPoint(200,220):
p.addPoint(130,180);
g.drawPolygon(p);

Drawing Arcs
- Draws a solid arc relative to the bounding
rectangle’s top- left coordinates (x,y) with the
specified width and height. The arc segment is
drawn starting at startAngle and sweeps
arcAngle degrees.

fillArc(x,y,width,height, startAngle, arcAngle); WEEK 5 Module 4: GUI with JAVA AWT (Abstract
Window Toolkit)
Example:
g.fillArc(30,20,40,40,0,-270) Java AWT
- A toolkit of classes in Java which helps a
programmer to develop Graphics and Graphical
User Interface components.
- It is a part of JFC (Java Foundation Classes)
developed by Sun Microsystems.
- It was developed for providing a common set of
tools for designing the cross-platform GUIs. One
of the important features of AWT is that it is
platform dependent.

Java AWT Hierarchy

CREATING 3-DIMENSIONAL RECTANGLE

Drawing 3D rectangle
- Draws a 3D rectangle in the current color with
the specified width and height. The arcWidth and
arcHeight determine the rounding of the corners.
True means raised.
draw3DRect(x,y,width,height,[true,false]);

Example:
g.draw3DRect(250,40,50,50,true);

Drawing 3D rectangle - The Hierarchy of Java AWT classes are given


- Draws a filled 3D rectangle in teh current color below, all the classes are available in java.awt
with the specified width and height. The package.
Component
- Top of AWT Hierarchy
- It is an abstract class
- A component object is responsible for
remembering the current foreground and
background colors and the currently selected Container
text font.
Window - An instance of the Window class has
In order to place every component in a particular no border and no title.
position on a screen, we need to add them to a
container. Dialog - Dialog class has border and title. An
instance of the Dialog class cannot exist without
an associated instance of the Frame class.

Frame - a frame has title, border and menu


bars. It can contain several components like
buttons, text fields, scrollbars etc. This is most
widely used container while developing an
application in AWT.

Panel - Panel does not contain title bar, menu


bar or border. It is a generic container for holding
components. An instance of the Panel class
provides a container to which to add
components.

- All the elements like buttons, labels, etc are Other Components and Containers
known as components.
- In AWT we have classes for each component as
shown in the above diagram.

Button - The button class allows the developer


to create a button with an appropriate label on it.
It also provides a way to define functionality after
clicking the button.

List - a list class is used to display a list of


required items. Using a list, the user can select
one or multiple items in it.

Checkbox - a checkbox can be used when an


application wants the user to choose from either
true or false.

Label - A label provides descriptive text


information on the application window. It is
Container placed inside a container.
- A subclass of component class.
- A component in AWT that can contain another Choice - A choice class is used to display a
components like buttons, textfields, labels etc. drop-down menu. The selected choice can be
- The classes that extends Container class are seen on the head.
known as container such as Frame, Dialog, and
Panel.

You might also like