0% found this document useful (0 votes)
9 views94 pages

Unit V - OOPs - Updated On 24.12.22

Uploaded by

510821205029 IT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views94 pages

Unit V - OOPs - Updated On 24.12.22

Uploaded by

510821205029 IT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 94

CS8392 – Object Oriented

Programming
Unit V – EVENT DRIVEN PROGRAMMING
By

D. Durai kumar,
Head of the Department
Department of Information Technology
Ganadipathy Tulsi’s Jain Engineering College
Vellore

By D. Durai kumar, HOD / IT


5.1 Graphics programming
Graphics programming
 Java contains support for graphics that enable
programmers to visually enhance applications
 Java contains many more sophisticated drawing
capabilities as part of the Java 2D API
AWT
 Java AWT (Abstract Window Toolkit) is an API to
develop GUI or window-based applications in java.
 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 are using the
resources of OS.The java.awt package provides classes
for AWT api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
By D. Durai kumar, HOD / IT
5.1 Graphics programming
Java AWT Hierarchy
 The hierarchy of Java AWT classes are given below.

By D. Durai kumar, HOD / IT


5.1 Graphics programming
Java AWT Hierarchy
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.
 All user interface elements that are displayed on the
screen and that interact with the user are subclasses of
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.

By D. Durai kumar, HOD / IT


5.1 Graphics programming
Java AWT Hierarchy
Container
 The Container class is a subclass of Component. The
Container is a component in AWT that can contain
another components like buttons, textfields, labels etc.
The classes that extend Container class are known as
container such as Frame, Dialog and Panel.
Window
 The window is the container that has no borders and
menu bars. You must use frame, dialog or another
window for creating a window.
Panel
 The Panel is the container that doesn't contain title bar
and menu bars. It can have other components like button,
textfield etc. . By D. Durai kumar, HOD / IT
5.1 Graphics programming
Java AWT Hierarchy

By D. Durai kumar, HOD / IT


5.1 Graphics programming
Java AWT Hierarchy
Frame
 The Frame is the container that contain title bar and can
have menu bars.
 It can have other components like button, textfield etc.
 There are two ways to create a Frame. They are,
By Instantiating Frame class
By extending Frame class

By D. Durai kumar, HOD / IT


5.1 Graphics programming
Java AWT Hierarchy
Frame

By D. Durai kumar, HOD / IT


5.1 Graphics programming
Java AWT Hierarchy
Frame

By D. Durai kumar, HOD / IT


5.1 Graphics programming
Java AWT Hierarchy
Frame

By D. Durai kumar, HOD / IT


Event Handling
Event handling:
 Changing the state of an object is known as an event. For
example, click on button, dragging mouse etc.
 The java.awt.event package provides many event classes
and Listener interfaces for event handling.
 Event handling has three main components,
Events : An event is a change in state of an object.
Events Source : Event source is an object that
generates an event.
Listeners : A listener is an object that listens to the
event. A listener gets notified when an event occur

By D. Durai kumar, HOD / IT


Event Handling
How Events are handled ?
 A source generates an Event and send it to one or more
listeners registered with the source.
 Once event is received by the listener, they process the
event and then return.
 Events are supported by a number of Java packages, like
java.util, java.awt and java.awt.event.

By D. Durai kumar, HOD / IT


5.1 Graphics programming
How Events are handled ?

By D. Durai kumar, HOD / IT


5.1 Graphics programming
How Events are handled ?

By D. Durai kumar, HOD / IT


Event Handling
Event Classes
 The classes that represent events are at the core of Java's
event handling mechanism.
 At the root of the Java event class hierarchy is
EventObject, which is in java.util.
 It is the superclass for all events. Its one constructor is
shown here:
EventObject(Object src)
 Here, src is the object that generates this event.
 EventObject contains two methods: getSource( ) and
toString( ).
 The getSource( ) method returns the source of the event.
Its general form is shown here: Object getSource( )
 As expected, toString( ) returns the string equivalent
of the event. By D. Durai kumar, HOD / IT
Event Handling
Event Classes
The ActionEvent Class
 An ActionEvent is generated when a button is pressed, a
list item is double-clicked, or a menu item is selected.
 The ActionEvent class defines four integer constants that
can be used to identify any modifiers associated with an
action event: ALT_MASK, CTRL_MASK,
META_MASK, and SHIFT_MASK.
 In addition, there is an integer constant,
ACTION_PERFORMED, which can be used to identify
action events.
 ActionEvent has these two constructors:
ActionEvent(Object src, int type, String cmd)
ActionEvent(Object src, int type, String cmd, int
modifiers)
By D. Durai kumar, HOD / IT
Event Handling
Event Classes
The AdjustmentEvent Class
 An AdjustmentEvent is generated by a scroll bar.
 There are five types of adjustment events.
 The AdjustmentEvent class defines integer constants that
can be used to identify them.
 The constants and their meanings are shown here

By D. Durai kumar, HOD / IT


Event Handling
Event Classes
The AdjustmentEvent Class
 AdjustmentEvent has this constructor:
AdjustmentEvent(Adjustable src, int id, int type, int data)
 Here, src is a reference to the object that generated this
event.
 The id equals ADJUSTMENT_VALUE_CHANGED.
 The type of the event is specified by type, and its
associated data is data.

By D. Durai kumar, HOD / IT


Event Handling
Event Classes
The ComponentEvent Class
 A ComponentEvent is generated when the size, position,
or visibility of a component is changed.
 There are four types of component events.
 The ComponentEvent class defines integer constants that
can be used to identify them.
 The constants and their meanings are shown here:

By D. Durai kumar, HOD / IT


Event Handling
Event Classes
The ContainerEvent Class
 A ContainerEvent is generated when a component is
added to or removed from a container.
 There are two types of container events.
 The ContainerEvent class defines int constants that can
be used to identify them: COMPONENT_ADDED and
COMPONENT_REMOVED.
 They indicate that a component has been added to or
removed from the container.
 ContainerEvent is a subclass of ComponentEvent and has
this constructor:
ContainerEvent(Component src, int type, Component comp)

By D. Durai kumar, HOD / IT


Event Handling
Event Classes
The FocusEvent Class
 A FocusEvent is generated when a component gains or
loses input focus.
 These events are identified by the integer constants
FOCUS_GAINED and FOCUS_LOST.
 FocusEvent is a subclass of ComponentEvent and has
these constructors:
 FocusEvent(Component src, int type)
 FocusEvent(Component src, int type, boolean
temporaryFlag)

By D. Durai kumar, HOD / IT


Event Handling
Event Classes
The MouseEvent Class
 There are seven types of mouse events. The MouseEvent
class defines the following integer constants that can be
used to identify them:

By D. Durai kumar, HOD / IT


5.1 Graphics programming
Steps to handle events:
 Implement appropriate interface in the class.
 Register the component with the listener.

How to implement Listener


 1. Declare an event handler class and specify that the
class either implements an ActionListener(any listener)
interface or extends a class that implements an
ActionListener interface.
For example:
 public class MyClass implements ActionListener
 {
 // Set of Code
 }
By D. Durai kumar, HOD / IT
5.1 Graphics programming

How to implement Listener


 2. Register an instance of the event handler class as a
listener on one or more components.
 For example:
someComponent.addActionListener(instanceOfMyClass);
 3. Include code that implements the methods in listener
interface.
 For example:
 public void actionPerformed(ActionEvent e)
 {
 //code that reacts to the action
 }
By D. Durai kumar, HOD / IT
5.1 Graphics programming
How to implement Listener

By D. Durai kumar, HOD / IT


5.1 Graphics programming
How to implement Listener

By D. Durai kumar, HOD / IT


5.1 Graphics programming
How to implement Listener

By D. Durai kumar, HOD / IT


Java Adapter Classes
Java adapter classes
 Java adapter classes provide the default implementation
of listener interfaces.
 If you inherit the adapter class, you will not be forced to
provide the implementation of all the methods of listener
interfaces.
 So it saves code.

 The adapter classes are found in java.awt.event,


java.awt.dnd and javax.swing.event packages.

By D. Durai kumar, HOD / IT


Java Adapter Classes
Java adapter classes

By D. Durai kumar, HOD / IT


AWT Event Hierarchy

By D. Durai kumar, HOD / IT


Java Swing
Swing
 Java Swing is a part of Java Foundation Classes (JFC)
that is used to create window-based applications.
 It is built on the top of AWT (Abstract Windowing
Toolkit) API and entirely written in java.
 Unlike AWT, Java Swing provides platform-independent
and lightweight components.
 The javax.swing package provides classes for java swing
API such as JButton, JTextField, JTextArea,
JRadioButton, JCheckbox, JMenu, JColorChooser etc.

By D. Durai kumar, HOD / IT


Java Fx
 JavaFX is a Java library that is used to develop Desktop
applications as well as Rich Internet Applications (RIA).
 The applications built in JavaFX, can run on multiple
platforms including Web, Mobile and Desktops.
 Our JavaFX tutorial includes all topics of JavaFX library
such as Fundamentals, 2D Shapes, 3D Shapes, Effects,
Animation, Text, Layouts, UI Controls, Transformations,
Charts, JavaFX with CSS, JavaFX with Media etc.

By D. Durai kumar, HOD / IT


JavaFx UI Controls
 The graphical user interface of every desktop application
mainly considers UI elements, layouts and behaviour.
 The UI elements are the one which are actually shown to
the user for interaction or information exchange.
 Layout defines the organization of the UI elements on
the screen.
 Behaviour is the reaction of the UI element when some
event is occurred on it.
 However, the package javafx.scene.control provides all
the necessary classes for the UI components like Button,
Label, etc.
 Every class represents a specific UI control and defines
some methods for their styling.

By D. Durai kumar, HOD / IT


JavaFX UI Controls

By D. Durai kumar, HOD / IT


JavaFX UI Controls

By D. Durai kumar, HOD / IT


Controls – Radio Button
 The Radio Button is used to provide various options to
the user.
 The user can only choose one option among all. A radio
button is either selected or deselected.
 It can be used in a scenario of multiple choice questions
in the quiz where only one option needs to be chosen by
the student.

By D. Durai kumar, HOD / IT


Controls – Radio Button

By D. Durai kumar, HOD / IT


JavaFX Layouts
 Layouts are the top level container classes that define the
UI styles for scene graph objects.
 Layout can be seen as the parent node to all the other
nodes.
 JavaFX provides various layout panes that support
different styles of layouts.
 In JavaFX, Layout defines the way in which the
components are to be seen on the stage.
 It basically organizes the scene-graph nodes.
 We have several built-in layout panes in JavaFX that are
HBox, VBox, StackPane, FlowBox, AnchorPane, etc.
 Each Built-in layout is represented by a separate class
which needs to be instantiated in order to implement that
particular layout pane.
By D. Durai kumar, HOD / IT
JavaFX Layouts
 All these classes belong to javafx.scene.layout package.
 javafx.scene.layout.Pane class is the base class for all
the built-in layout classes in JavaFX.

By D. Durai kumar, HOD / IT


JavaFX Layouts Classes
 All these classes belong to javafx.scene.layout package.
 javafx.scene.layout.Pane class is the base class for all
the built-in layout classes in JavaFX.

By D. Durai kumar, HOD / IT


JavaFX Layouts Classes
Steps to create layout
 In order to create the layouts, we need to follow the
following steps.
 Instantiate the respective layout class, for
example, HBox root = new HBox();
 Setting the properties for the layout, for
example, root.setSpacing(20);
 Adding nodes to the layout object, for
example, root.getChildren().addAll(<NodeObjects>);

By D. Durai kumar, HOD / IT


JavaFX Layouts - BorderPane
Steps to create layout
 BorderPane arranges the nodes at the left, right, centre,
top and bottom of the screen.
 It is represented
by javafx.scene.layout.BorderPane class.
 This class provides various methods like setRight(),
setLeft(), setCenter(), setBottom() and setTop() which
are used to set the position for the specified nodes.
 We need to instantiate BorderPane class to create
the BorderPane layout.

By D. Durai kumar, HOD / IT


JavaFX Layouts - BorderPane

By D. Durai kumar, HOD / IT


JavaFX Layouts - BorderPane
Constructors
There are the following constructors in the class.
 BorderPane() : create the empty layout
 BorderPane(Node Center) : create the layout with the
center node
 BorderPane(Node Center, Node top, Node right, Node
bottom, Node left) : create the layout with all the nodes

By D. Durai kumar, HOD / IT


JavaFX Layouts - BorderPane

By D. Durai kumar, HOD / IT


JavaFX Layouts - BorderPane
Constructors
There are the following constructors in the class.
 BorderPane() : create the empty layout
 BorderPane(Node Center) : create the layout with the
center node
 BorderPane(Node Center, Node top, Node right, Node
bottom, Node left) : create the layout with all the nodes

By D. Durai kumar, HOD / IT


Java Swing
Swing

By D. Durai kumar, HOD / IT


Java Swing
Swing

By D. Durai kumar, HOD / IT


Java Swing
Swing

By D. Durai kumar, HOD / IT


Java Swing
Swing

By D. Durai kumar, HOD / IT


Java Swing
Swing

By D. Durai kumar, HOD / IT


Java Swing
 Difference between AWT and Swing

By D. Durai kumar, HOD / IT


Layout management
Layout management
Java LayoutManagers
 The LayoutManagers are used to arrange components in
a particular manner.
 LayoutManager is an interface that is implemented by all
the classes of layout managers.

By D. Durai kumar, HOD / IT


Layout management

By D. Durai kumar, HOD / IT


Layout management

By D. Durai kumar, HOD / IT


Layout management

By D. Durai kumar, HOD / IT


Layout management
Example: Border layout
Example: Boxlayout

By D. Durai kumar, HOD / IT


Layout management

By D. Durai kumar, HOD / IT


Layout management

By D. Durai kumar, HOD / IT


Layout management

By D. Durai kumar, HOD / IT


Swing Components
Swing components:
 Text Fields
The object of a JTextField class is a text component that
allows the editing of a single line text. It inherits
JTextComponent class.
 Text Areas
The object of a JTextArea class is a multi line region that
displays text. It allows the editing of multiple line text. It
inherits JTextComponent class
 Buttons
The JButton class is used to create a labeled button that
has platform independent implementation. The
application result in some action when the button is
pushed. It inherits AbstractButton class.
By D. Durai kumar, HOD / IT
Swing Components
Swing components:
Check Boxes
 The JCheckBox class is used to create a checkbox.
 It is used to turn an option on (true) or off (false).
Clicking on a CheckBox changes its state from "on" to
"off" or from "off" to "on ".
 It inherits JToggleButton class.
Radio Buttons
 The JRadioButton class is used to create a radio button.
 It is used to choose one option from multiple options.
 It is widely used in exam systems or quiz.
 It should be added in ButtonGroup to select one radio
button only.

By D. Durai kumar, HOD / IT


Swing Components
Swing component
 Lists
 The object of JList class represents a list of text items.
The list of text items can be set up so that the user can
choose either one item or multiple items. It inherits
JComponent class.
 Scrollbars
 The object of JScrollbar class is used to add horizontal
and vertical scrollbar. It is an implementation of a
scrollbar. It inherits JComponent class.

By D. Durai kumar, HOD / IT


Swing Components
Swing component
Windows
 The class JWindow is a container that can be displayed
but does not have the title bar
Menus
 The JMenuBar class is used to display menu bar on the
window or frame. It may have several menus.
 The object of JMenu class is a pull down menu
component which is displayed from the menu bar. It
inherits the JMenuItem class.
 The object of JMenuItem class adds a simple labeled
menu item. The items used in a menu must belong to the
JMenuItem or any of its subclass

By D. Durai kumar, HOD / IT


Swing Components
Swing component
Dialog Boxes.
 The JDialog control represents a top level window with a
border and a title used to take some form of input from
the user. It inherits the Dialog class.Unlike JFrame, it
doesn't have maximize and minimize buttons.

By D. Durai kumar, HOD / IT


Swing Components
Swing component

By D. Durai kumar, HOD / IT


Swing Components
Swing component

By D. Durai kumar, HOD / IT


Swing Components
Swing component

By D. Durai kumar, HOD / IT


Swing Components
Swing component

By D. Durai kumar, HOD / IT


Working with 2D shapes
Working with 2D shapes
 Java supports 2-dimensional shapes, text and images
using methods available in Graphics2D class.
 The Graphics2D class extends the Graphics class to
provide more sophisticated control over geometry,
coordinate transformations, color management, and text
layout.
Graphics2D class Constructor
 Graphics2D() //Constructs a new Graphics2D object.
 This class inherits the methods from java.lang.Object.

By D. Durai kumar, HOD / IT


Working with 2D shapes
Some of the commonly used methods of Graphics2D
class are as follows.

By D. Durai kumar, HOD / IT


Working with 2D shapes
Some of the commonly used methods of Graphics2D
class are as follows.

By D. Durai kumar, HOD / IT


Working with 2D shapes
 The Java 2D library organizes geometric shapes in an
object oriented fashion.
 In particular, there are classes to represent lines,
rectangles, and ellipses:
Line2D
Rectangle2D
Ellipse2D
 These classes all implement the Shape interface.
 To draw a shape, you first create an object of a class that
implements the Shape interface and then call the draw()
method of the Graphics2D class.
 For example:
Rectangle2D rect = . . .;
g2.draw(rect);
By D. Durai kumar, HOD / IT
Working with 2D shapes
Rectangle2D class
 Consider the Rectangle2D class. This is an abstract class
with two concrete subclasses, which are also
– Rectangle2D.Float
– Rectangle2D.Double
Methods:
 java.awt.geom.RectangularShape
• double getCenterX()
• double getCenterY()
• double getMinX()
• double getMinY()
• double getMaxX()
• double getMaxY()
 returns the center, minimum, or maximum x- or y-value
of the enclosing rectangle.
By D. Durai kumar, HOD / IT
Working with 2D shapes
Rectangle2D class
Methods:
double getWidth()
double getHeight()
 returns the width or height of the enclosing rectangle.
double getX()
double getY()
 returns the x- or y-coordinate of the top-left corner of the
enclosing rectangle.

By D. Durai kumar, HOD / IT


Working with 2D shapes
Rectangle2D class
Constructors:
 java.awt.geom.Rectangle2D.Double
Rectangle2D.Double (double x, double y, double w, double h)
 constructs a rectangle with the given top-left corner,
width, and height.
 java.awt.geom.Rectangle2D.Float
Rectangle2D.Float (float x, float y, float w, float h)
 constructs a rectangle with the given top-left corner,
width, and height
 java.awt.geom.Ellipse2D.Double
 Ellipse2D.Double (double x, double y, double w, double h)
 constructs an ellipse whose bounding rectangle has the
given top-left corner, width, and height.
By D. Durai kumar, HOD / IT
Working with 2D shapes
Rectangle2D class
Constructors:
 java.awt.geom.Point2D.Double
Point2D.Double(double x, double y)
 constructs a point with the given coordinates.

 java.awt.geom.Line2D.Double
Line2D.Double(Point2D start, Point2D end)

Line2D.Double(double startX, double startY, double


endX, double endY)

 constructs a line with the given start and end points.

By D. Durai kumar, HOD / IT


Working with 2D shapes
Rectangle2D class

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Color
 The setPaint method of the Graphics2D class lets you
select a color that is used for all subsequent drawing
operations on the graphics context.
 For example:
g2.setPaint(Color.RED);
g2.drawString("Warning!", 100, 100);
 You can fill the interiors of closed shapes (such as
rectangles or ellipses) with a color.
 Simply call fill instead of draw:
Rectangle2D rect = . . .;
g2.setPaint(Color.RED);
g2.fill(rect); // fills rect with red color

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Color
 To draw in multiple colors, you select a color, draw or fill,
then select another color, anddraw or fill again.
 The java.awt.Color class offers predefined constants for
the following 13 standard colors:
BLACK, BLUE, CYAN, DARK_GRAY, GRAY,
GREEN, LIGHT_GRAY, MAGENTA, ORANGE,
PINK, RED, WHITE, YELLOW
 You can specify a custom color by creating a Color object
by its red, green, and blue components.
 Using a scale of 0–255 (that is, one byte) for the redness,
blueness, and greenness,call the Color constructor like
this:
Color(int redness, int greenness, int blueness)

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Color
 To set the background color, you use the setBackground
method of the Component class, an ancestor of
JComponent.
MyComponent p = new MyComponent();
p.setBackground(Color.PINK);

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Methods:
 java.awt.Color
 Color(int r, int g, int b)
creates a color object.
 Color getColor()
 void setColor(Color c)
gets or sets the current color. All subsequent graphics
operations will use the newcolor.
 java.awt.Color
 Parameters:
r The red value (0–255)
g The green value (0–255)
b The blue value (0–255)
By D. Durai kumar, HOD / IT
Using color, fonts, and images
Methods:
java.awt.Graphics2D
Paint getPaint()
 void setPaint(Paint p)
gets or sets the paint property of this graphics context. The
Color class implements the Paint interface.
Therefore, you can use this method to set the paint
attribute to asolid color.
 void fill(Shape s)
fills the shape with the current paint.

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Methods:
java.awt.Component
 Color getBackground()
 void setBackground(Color c)
gets or sets the background color.
 Color getForeground()
 void setForeground(Color c)
gets or sets the foreground color

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Fonts
 The java.awt.Font class is used to create Font objects to
set the font.
 Constructor:
 Font(String name, int style, int size):
Creates a new font with the specified name,style and
point size.

 Example:
 Font sansbold14 = new Font("SansSerif", Font.BOLD,
14);

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Fonts
 Methods:
 java.awt.Font
 1.Font(String name, int style, int size)
creates a new font object.

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Fonts
 Methods:
 String getFontName()
gets the font face name (such as ―Helvetica Bold‖).
 String getFamily()
gets the font family name (such as ―Helvetica‖).
 String getName()
gets the logical name (such as ―SansSerif‖) if the
font was created with a logical font name; otherwise, gets
the font face name.
 Rectangle2D getStringBounds(String s,
FontRenderContext context)
returns a rectangle that encloses the string. The origin
of the rectangle falls on thebaseline. The negative of the
ascent equals By D. Durai kumar, HOD / IT
Using color, fonts, and images
Fonts
 Methods:
 Rectangle2D getStringBounds(String s,
FontRenderContext context)
returns a rectangle that encloses the string. The origin
of the rectangle falls on thebaseline. The negative of the
ascent equals to the top y-coordinate of the rectangle. The
height of the rectangle is the sum of ascent, descent, and
leading.’

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Fonts
 Methods:
 Font deriveFont(int style)
 Font deriveFont(float size)
 Font deriveFont(int style, float size)
returns a new font that equals this font, except that it
has the given size and style.

By D. Durai kumar, HOD / IT


Using color, fonts, and images
Fonts
 Methods:
 java.awt.font.LineMetrics
 float getAscent()
gets the font ascent—the distance from the baseline to the
tops of uppercase characters.
 float getDescent()
gets the font descent—the distance from the baseline to
the bottoms of descenders.
 float getLeading()
gets the font leading—the space between the bottom of
one line of text and the top of the next line.
 float getHeight()
gets the total height of the font—the distance between the
two baselines of textBy D. Durai kumar, HOD / IT
Using color, fonts, and images
Fonts
 Methods:
 java.awt.Graphics
 Font getFont()
 void setFont(Font font)
gets or sets the current font.
 void drawString(String str, int x, int y)
draws a string in the current font and color.
Parameters: str The string to be drawn
 void drawString(String str, float x, float y)
draws a string in the current font and color.
Parameters: str The string to be drawn
 x The x-coordinate of the start of the string
 y The y-coordinate of the baseline of the string
By D. Durai kumar, HOD / IT
Using color, fonts, and images
 Images
 Once images are stored in local files or someplace on the
Internet, you can read them into a Java application and
display them on Graphics objects.
String filename = "...";
Image image = ImageIO.read(new File(filename));
 Now the variable image contains a reference to an object
that encapsulates the image data.
 You can display the image with the drawImage() method
of the Graphics class.
public void paintComponent(Graphics g)
{
...
g.drawImage(image, x, y, null);
} By D. Durai kumar, HOD / IT
Using color, fonts, and images
 Images
 Methods:
 javax.imageio.ImageIO
 staticBufferedImage read(File f)
 staticBufferedImage read(URL u)
reads an image from the given file or URL.

By D. Durai kumar, HOD / IT


Using color, fonts, and images
 Images
 Methods:
 java.awt.Graphics
 boolean drawImage(Image img, int x, int y,
 ImageObserver observer)
 draws an unscaled image. Note: This call may return
before the image is drawn.
 Parameters: imgThe image to be drawn
 x The x-coordinate of the top-left corner
 y The y-coordinate of the top-left corner
 width The desired width of image
 height The desired height of image
 observer The object to notify of the progress of the
rendering process (may be null)
By D. Durai kumar, HOD / IT

You might also like