Introduction
One of the most important features of Java is its ability to draw
graphics.
●We can write Java applets that draw lines , figures of different
shapes, images and text in different fonts and styles.
●We can also incorporate colors in display.
●Every applet has its own area of the screen known as canvas,
where it creates it display.
●The size of an applets space is decided by the attributes of the
<APPLET…>tag.
●A Java applet draws graphical image inside its space using the
coordinate system
●Java coordinate system has the origin(0,0) in the upper-left
corner. Positive x values are to right and positive y values are to
the bottom.
Graphics
class
• Javas Graphics class includes methods for drawing many
different types of shapes from simple lines to polygons
to text in a variety of fonts.
• Main feature in java is creating a graphical interface.
• Graphics in any language gives a wonderful look and feel
to the users.
• Two packages that are mainly used to draw graphics.
• Applet package
• awt package
https://docs.oracle.com/javase/7/docs/api/
https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html
Nov 28, 2021 2
Graphics Programming Graphics
•
Class
The Graphics class is the abstract base class.
• A Graphics object manages a graphics context and draws
pixels on the screen that represent text and other
graphical object (e.g., lines, ellipses, rectangles and
other polygons).
• Graphics objects contain methods for drawing, font
manipulation, color manipulation and the like.
Nov 28, 2021 3
Drawing methods of graphics
class str, int x, int y)
• drawString(String
• Draws the text given by the specified string, using this graphics
context's current font and color.
• abstract void drawLine(int x1, int y1, int x2, int y2)
• Draws a line, using the current color,
between the points (x1, y1) and (x2, y2) in this graphics
context's coordinate system.
• abstract void drawOval(int x, int y, int width, int height)
• Draws the outline of an oval.
Nov 28, 2021 4
Drawing methods of graphics
class
• drawArc(int x, int y, int width, int height, int startAngle, int
arcAngle)
• Draws the outline of a circular or elliptical arc covering the
specified rectangle.
• drawRect(int x, int y, int width, int height)
• Draws the outline of the specified rectangle.
• fillArc(int x, int y, int width, int height, int startAngle, int
arcAngle)
• Fills a circular or elliptical arc covering the specified
rectangle.
Nov 28, 2021 5
Drawing methods of graphics class
• fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
• Fills a closed polygon defined by arrays of x and y
coordinates.
• fillRect(int x, int y, int width, int height)
• Fills the specified rectangle.
Nov 28, 2021 6
Drawing methods of graphics
class
• setColor(Color c)
• Sets this graphics context's current color to the
specified color.
• setFont(Font font)
• Sets this graphics context's font to the specified font.
• getColor()
• Gets this graphics context's current color.
Nov 28, 2021 7
// Drawing (or printing) texts on the graphics screen:
drawString(String str, int xBaselineLeft, int yBaselineLeft);
// Drawing lines:
drawLine(int x1, int y1, int x2, int y2);
drawPolyline(int[] xPoints, int[] yPoints, int numPoint);
// Drawing primitive shapes:
drawRect(int xTopLeft, int yTopLeft, int width, int height);
drawOval(int xTopLeft, int yTopLeft, int width, int height);
drawArc(int xTopLeft, int yTopLeft, int width, int height, int
startAngle, int arcAngle);
draw3DRect(int xTopLeft, int, yTopLeft, int width, int height,
boolean raised);
drawRoundRect(int xTopLeft, int yTopLeft, int width, int
height, int arcWidth, int arcHeight)
drawPolygon(int[] xPoints, int[] yPoints, int numPoint);
// Filling primitive shapes:
fillRect(int xTopLeft, int yTopLeft, int width, int height);
fillOval(int xTopLeft, int yTopLeft, int width, int height);
fillArc(int xTopLeft, int yTopLeft, int width, int height, int
startAngle, int arcAngle);
fill3DRect(int xTopLeft, int, yTopLeft, int width, int height,
boolean raised);
fillRoundRect(int xTopLeft, int yTopLeft, int width, int
height, int arcWidth, int arcHeight)
fillPolygon(int[] xPoints, int[] yPoints, int numPoint);
// Drawing (or Displaying) images:
drawImage(Image img, int xTopLeft, int yTopLeft, ImageObserver
obs); // draw image with its size
drawImage(Image img, int xTopLeft, int yTopLeft, int width,
int height, ImageObserver o); // resize image on screen
Abstract Window Toolkit
(AWT)
• The Abstract Window Toolkit (AWT) was
Java‟s first GUI framework, and it has been part of
Java since version 1.0.
• It contains numerous classes and methods that
allow you to create windows and simple controls.
• Java AWT (Abstract Windowing Toolkit) is an
API to develop GUI or window-based application in
java.
Nov 28, 2021 10
Abstract Window Toolkit (AWT)
• Java AWT components are platform-dependent i.e.
components are displayed according to the view of
operating system.
• AWT is heavyweight i.e. its components uses the resources
of system.
• The java.awt package provides classes for AWT API such as
TextField, Label, TextArea, RadioButton, CheckBox, Choice,
List etc.
https://docs.oracle.com/javase/7/docs/api/java/awt/package-summary.html
Nov 28, 2021 11
AWT classes
• The AWT classes are contained in the java.awt
package.
• It is one of Java‟s largest packages.
Nov 28, 2021 12
Hierarchy Of AWT
Nov 28, 2021 13
Component & Container
•In Java, a component is the basic user interface object and is found in
all Java applications.
• Components include lists, buttons, panels, and windows.
• To use components, you need to place them in a container.
•A container is a component that holds and manages other
components.
• Containers display components using a layout manager.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Nov 28, 2021 14
Component
• At the top of the AWT hierarchy is the Component class .
• Component is an abstract class that encapsulates all of the
attributes of a visual component.
• Except for menus, all user interface elements that are displayed
on the screen and that interact with the user are subclasses of
Component.
Nov 28, 2021 15
Component
• It defines over a hundred public methods that are responsible
for managing events, such as mouse and keyboard input,
positioning and sizing the window, and repainting.
• A Component object is responsible for remembering the current
foreground and background colors and the currently selected
text font.
Nov 28, 2021 16
Container
• Before adding the components that make up
a user interface, the programmer must create a container.
• The Container is a component in AWT that can contain another
components like buttons, textfields, labels etc.
• The classes that extends Container class are
known as container such as Frame, Dialog and Panel.
• A container is responsible for laying out (that is, positioning) any
components that it contains
Nov 28, 2021 17
Containe
r
• class Window - The window is the container that have
no borders and menu bars. You must use frame, dialog
or another window for creating a window.
• Frame - The Frame is the container that contain title bar
and can have menu bars. It can have other components
like button, text field etc.
• Panel - The Panel is the container that doesn't contain
title bar and menu bars. It can have other components
like button, text field and etc.
• Canvas - It represents a rectangular area where
application can draw something or can receive inputs
created by user.
Nov 28, 2021 18
Useful Methods of Component
class
Nov 28, 2021 19