Java PPT - 5 by Adi
Java PPT - 5 by Adi
Unit-5
Dr. K ADISESHA
Java AWT Graphics 2
& Java I/O
Introduction
Java I/O
Prof. K. Adisesha
3
Introduction
Java Graphics:
The Graphics class is the abstract super class for all graphics contexts which allow an
application to draw onto components that can be realized on various devices, or onto off-
screen images as well.
➢ A Graphics object encapsulates all state information required for the basic rendering
operations that Java supports. State information includes the following properties.
❖ The Component object on which to draw.
❖ A translation origin for rendering and clipping coordinates.
❖ The current clip.
❖ The current color.
❖ The current font.
❖ The current logical pixel operation function.
Prof. K. Adisesha
4
Introduction
Window Fundamentals:
Java Abstract window tool kit package is used for displaying the data within a GUI
Environment. Features of AW T Package are as Followings.
➢ The main purpose for using the AWT is using for all the components displaying on the screen.
➢ It provides us a set of user interface components
including windows buttons text fields scrolling
list etc.
➢ It provides us the way to laying out these above
components.
➢ It provides to create the events upon these
components.
Prof. K. Adisesha
5
Introduction
Java - Applet:
Graphics is an abstract class provided by Java AWT which is used to draw or paint on
the components.
➢ Class declaration: Following is the declaration for java.awt.Graphics class:
public abstract class Graphics
extends Object
➢ Class constructors: Following is the declaration for Constructs a new Graphics object:
Graphics() { }
Example: Following is the declaration for java.awt.Component class:
public abstract class Component
extends Object
implements ImageObserver, MenuContainer, Serializable
Prof. K. Adisesha
9
AWT Graphics Class
Prof. K. Adisesha
11
AWT Graphics Class
Prof. K. Adisesha
12
AWT Graphics Class
Example: MyFrame.java
import java.awt.*; public void paint(Graphics g)
import java.awt.event.WindowAdapter; { g.drawRect(100, 100, 100, 50); }
import java.awt.event.WindowEvent;
public static void main(String[] args)
public class MyFrame extends Frame { new MyFrame(); }
{ public MyFrame()
{ setVisible(true); }
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ @Override
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
}
Prof. K. Adisesha
13
AWT Graphics Class
Event :
Change in the state of an object is known as event i.e. event describes the change in state
of source.
➢ Events are generated as result of user interaction with the graphical user interface components.
➢ For example, clicking on a button, moving the mouse, entering a character through keyboard,
selecting an item from list, scrolling the page are the activities that causes an event to happen.
➢ Types of Event
❖ Foreground Events - Those events which require the direct interaction of user. They are
generated as consequences of a person interacting with the graphical components in
Graphical User Interface. For example, clicking on a button, moving the mouse.
❖ Background Events - Those events that require the interaction of end user are known as
background events. Operating system interrupts, hardware or software failure, timer
expires, are the example of background events.
Prof. K. Adisesha
16
AWT Event Handling
Event Handling:
Event Handling is the mechanism that controls the event and decides what should
happen if an event occurs.
➢ This mechanism have the code which is known as event handler that is executed when an event
occurs.
➢ Java Uses the Delegation Event Model to handle the events by defining the standard mechanism
to generate and handle the events.
➢ The Delegation Event Model has the following key participants namely:
❖ Source - The source is an object on which event occurs. It is responsible for providing
information of the occurred event to it's handler.
❖ Listener - It is also known as event handler. It is responsible for generating response to an
event. Listener waits until it receives an event. Once the event is received, the listener
process the event an then returns.
Prof. K. Adisesha
17
AWT Event Handling
Prof. K. Adisesha
19
AWT Event Handling
Java Stream:
Java uses the concept of a stream to make I/O operation fast. The java.io package
contains all the classes required for input and output operations.
➢ In Java, a stream is composed of a sequence of data bytes. It's called a stream because it
is like a stream of water that continues to flow.
➢ In Java, 3 streams are created for us automatically. All these streams are attached with
the console.
❖ System.out: standard output stream
❖ System.in: standard input stream
❖ System.err: standard error stream.
Prof. K. Adisesha
21
Java Input/Output
Java Stream:
The Streams in Java IO are of the following types:.
Prof. K. Adisesha
22
Java Input/Output
InputStream class:
InputStream class is an abstract class. It is the superclass of all classes representing an
input stream of bytes.
➢ An output stream accepts output bytes and sends them to some sink.
➢ Useful methods of InputStream
❖ public abstract int read() throws IOException: reads the next byte
of data from the input stream. It returns -1 at the end of the file.
❖ public int available() throws IOException: returns an estimate of
the number of bytes that can be read from the current input stream.
❖ public void close() throws IOException: is used to close the current
input stream.
Prof. K. Adisesha
24
Java File Stream
FileOutputStream:
Java FileOutputStream is an output stream used for writing data to a file.
➢ You can write byte-oriented as well as character-oriented data through FileOutputStream class.
➢ For character-oriented data, it is preferred to use FileWriter than FileOutputStream.
➢ FileOutputStream class declaration: public class FileOutputStream extends OutputStream
➢ FileOutputStream class methods:
❖ protected void finalize() It is used to clean up the connection with the file output stream.
❖ void write(byte[] ary) It is used to write ary.length bytes from the byte array to the file output
stream.
❖ void write(byte[] ary, int off, int len) It is used to write len bytes from the byte array starting at offset
off to the file output stream.
❖ void write(int b) It is used to write the specified byte to the file output stream.
❖ void close() It is used to closes the file output stream.
Prof. K. Adisesha
25
Java File Stream
FileOutputStream:
Java FileOutputStream is an output stream used for writing data to a file.
➢ Java FileOutputStream Example 1: write byte example 2: write string.
import java.io.FileOutputStream; import java.io.FileOutputStream;
public class FileOutputStreamExample { public class FileOutputStreamExample
public static void main(String args[]){ { public static void main(String args[])
try{ { try
FileOutputStream fout=new FileOutputStream("D:\\f1.txt"); { FileOutputStream fout=new FileOutputStream("D:\\f2.txt");
fout.write(15); String s="Welcome to java Programming.";
fout.close(); byte b[]=s.getBytes();//converting string into byte array
System.out.println("success..."); fout.write(b);
}catch(Exception e){System.out.println(e);} fout.close();
} System.out.println("success...");
} }catch(Exception e){System.out.println(e);}
}
Prof. K. Adisesha
}
26
Java File Stream
FileInputStream Class:
Java FileInputStream class obtains input bytes from a file. It is used for reading byte-oriented
data (streams of raw bytes) such as image data, audio, video etc.
➢ You can also read character-stream data. But, it is recommended to use FileReader class.
➢ Class declaration: public class FileInputStream extends InputStream
➢ FileInputStream class methods:
❖ int read() It is used to read the byte of data from the input stream.
❖ int read(byte[] b) It is used to read up to b.length bytes of data from the input stream.
❖ long skip(long x) It is used to skip over and discards x bytes of data from the input stream.
❖ FileChannel getChannel() It is used to return the unique FileChannel object associated with the file
input stream.
❖ protected void finalize() It is used to ensure that the close method is call when there is no more
reference to the file input stream.
Prof. K.❖ void close() It is used to closes the stream.
Adisesha
27
Java File Stream
BufferedReader Class:
Java BufferedReader class is used to read the text from a character-based input stream.
It can be used to read data line by line by readLine() method.
➢ It makes the performance fast. It inherits Reader class.
package com.javatpoint;
import java.io.*;
public class BufferedReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\f3.txt");
BufferedReader br=new BufferedReader(fr);
int i;
while((i=br.read())!=-1){ System.out.print((char)i); }
br.close();
fr.close();
Prof. K. Adisesha
} }
28
Java File Stream
Java Scanner:
Scanner class in Java is found in the java.util package. Java provides various ways to
read input from the keyboard, the java.util.Scanner class is one of them.
➢ The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by
default. It provides many methods to read and parse various primitive values.
➢ Java Scanner where we are getting a single input from the user. Here, we are asking for a string
through in.nextLine() method.
import java.util.*; String name = in.nextLine();
public class ScannerExample { System.out.println("Name is: " + name);
public static void main(String args[]){ in.close();
Scanner in = new Scanner(System.in); }
System.out.print("Enter your name: "); }
Prof. K. Adisesha
29
Discussion
Queries ?
Prof. K. Adisesha
9449081542