I/O

Streams

A Stream is an abstraction that either produces or consumes information.

A stream is linked to a physical device by the Java I/O system.

Byte Streams and Character Streams.

Byte streams provide means for handling input and output of bytes. For eg. When reading or writing binary data.

Character streams provide a convenient means for handling input and output of characters. They use Unicode and can be internationalized.

Byte Stream Classes

At top are 2 abstract classes: InputStream and OutputStream.

Character Stream Classes

At top are 2 abstract classes: Reader and Writer.

 

Applet Fundamentals

import java.awt.*;

import java.applet.*;

public class SimpleApplet extends Applet {

public void paint (Graphics g) {

g.drawString("First Applet", 50, 50 );

}

}

AWT is Abstract Window Toolkit.

AWT contains support for a window-based, graphical interface.

Paint method defined by AWT. Called whenever the applet must redraw its output.

Applet Tag

<applet code="SimpleApplet" width=200 height=200>

</applet>

else

C:\>appletviewer MyHTML.html

Applet Architecture

Event driven

An applet waits until an event occurs.

The AWT notifies the applet about an event by calling event handler that has been provided by the applet.The applet takes appropriate action and then quickly return control to AWT.

Applet Skeleton

import java.awt.*;

import java.applet.*;

/*

<applet code="AppletSkel" width=300 height=200>

</applet>

*/

public class AppletSkel extends Applet {

//Called First

public void init() {

// initialization

}

/* Called second, after init(). Also called whenever the applet is restarted.*/

public void start() {

// start or resume execution

}

// Called when the applet is stopped.

public void stop() {

// Suspends execution

}

/*Called when applet is terminated. This is last method executed.*/

public void destroy() {

//perform shutdown activities

}

//Called when an applet’s window must be restored.

public void paint(Graphics g) {

//redisplay contents of window

}

}

Order

When applet begins:

init()

start()

paint()

When an applet is terminated

stop()

destroy()

Simple Applet Display Methods

void drawstring(String message, int x, int y)

void setBackground(Color newColor)

void setForeground(Color newColor)

 

Sample Program

import java.awt.*;

import java.applet.*;

/*

<applet code="Sample" width=300 height=200>

</applet>

*/

public class Sample extends Applet {

String msg;

public void init() {

setBackground(Color.cyan);

setForeground(Color.red);

msg = "Inside init() –";

}

public void start() {

msg += "Inside start() –";

}

public void paint(Graphics g) {

msg += "Inside paint().";

g.drawString(msg, 20, 30);

}

}

 

Requesting Repainting

Applet must quickly return control to the AWT run-time system.

So to change a particular information itself, we can not make a loop in the paint method that repeatedly updates it.

So, whenever your applet needs to update the information displayed in its window, it simply calls repaint().

 

 

void repaint() //entire window

or

void repaint(int left, int top, int width, int height) // specifies region to be repainted

 

It is possible for a method other than paint() or update() to output to an applet’s window. To do so, it must obtain a graphics context by calling getGraphics() (defined by Component) and then use this to output to the window.

A Simple Banner Applet

import java.awt.*;

import java.applet.*;

/*

<applet code="SampleBanner" width=300 height=200>

</applet>

*/

public class SampleBanner extends Applet implements Runnable{

String msg = "A Simple Moving Banner.";

Thread t = null;

int state;

boolean stopFlag;

public void init() {

setBackground(Color.cyan);

setForeground(Color.red);

}

public void start() {

t = new Thread(this);

stopFlag = false;

t.start();

}

public void run () {

char ch;

for( ; ; ) {

try {

repaint();

Thread.sleep(250);

ch = msg.charAt(0);

msg = msg.subString(1, msg.length());

msg += ch;

if(stopFlag)

break;

} catch(InterruptedException e) {}

}

}

public void stop() {

stopFlag = true;

t = null;

}

public void paint(Graphics g) {

g.drawString(msg, 50, 30);

showStatus("This is shown in the status window.");

}

}

Passing Parameters to Applets

<applet code="ParamDemo" width=300 height=80>

<param name=fontName value=Courier>

<param name=fontSize value=14>

<param name=counter value=2>

</applet>

How to use these parameters

String fontName;

String param;

int fontSize;

fontName = getParameter("fontName");

if(fontName == null)

fontName = "Not Found";

 

param = getParameter("fontSize");

try {

if (param != null)

fontSize = Integer.parseInt(param);

else

fontSize = 0;

}catch(NumberFormatException e) {

fontSize = -1;

}