Documente Academic
Documente Profesional
Documente Cultură
Component
Button
Checkbox
TextComponent
TextArea
TextField
Choice
List
Container
Panel
Window
ScrollPane
Applet
Dialog
Label
Canvas
ScrollBar
Frame
Netscape(JVM)
Loads HTML file
Loads Java Applet
TCP/IP
(socket)
TCP/IP
(HTTP)
Client
Internet
TCP/IP
(HTTP)
TCP/IP
(socket)
Sambar Server
HTML files(Applet Tags)
Applets
Web Server
Java Applet
Java Applet
Applets will have the following methods:
public void init ( ) {
HelloWorldApplet1.java
import
import
import
import
java.awt.Graphics;
java.awt.Font;
java.awt.Color;
java.applet.Applet;
.
</APPLET>
Options:
CODEBASE
ALIGN
Designates the applet alignment choice (Left, Right, Top, Bottom, Middle)
HSPACE
Designates the empty space at the right and left of the applet
VSPACE
NAME
ARCHIVE
<HTML>
<HEAD>
<TITLE>
Hello World Example!
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Java Lecture 2
</H2>
<P>
<APPLET code="HelloWorldApplet1.class" width="500" height="50">
Hello World!
</APPLET>
</P>
<P>
<A href="HelloWorldApplet1.java">The Source</A>
</P>
</BODY>
</HTML>
HellowWorldApplet2.html
<HTML>
<HEAD>
<TITLE>
Hello World Example!
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Java Lecture 1
</H2>
<P>
<APPLET code="HelloWorldApplet.class" width="500" height="50">
<PARAM name="name" value="Roger L. Norton">
Hello World!
</APPLET>
</P>
<P>
<A href="HelloWorldApplet.java">The Source</A>
</P>
</BODY>
</HTML>
HelloWorldApplet3.html
<HTML>
<HEAD>
<TITLE>
Hello World Example!
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Java Lecture 1
</H2>
<P>
<APPLET code="HelloWorldApplet.class" width="500" height="50">
Hello World!
</APPLET>
</P>
<P>
<A href="HelloWorldApplet.java">The Source</A>
</P>
</BODY>
</HTML>
An Interesting Example
import java.awt.Graphics;
import java.awt.Color;
public class ColorBoxes extends java.applet.Applet {
void delay(int time){
for(int i =1;i<=time;i++);
}
public void paint(Graphics g) {
int rval, gval, bval;
String tempDelay = getParameter("delay");
int finalDelay = Integer.parseInt(tempDelay);
for (int k = 1;k<=100;k++){
for (int j = 30; j < (size().height -25); j += 30)
for (int i = 5; i < (size().width -25); i += 30) {
rval = (int)Math.floor(Math.random() * 256);
gval = (int)Math.floor(Math.random() * 256);
bval = (int)Math.floor(Math.random() * 256);
g.setColor(new Color(rval,gval,bval));
g.fillRect(i, j, 25, 25);
g.setColor(Color.black);
g.drawRect(i, j, 25, 25);
};
delay(finalDelay);
};
};
}
ColorBoxes.html
<HTML>
<HEAD>
<TITLE>
Colored Boxes
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Example #3
</H2>
<H2>
Colored Boxes
</H2>
<P>
<APPLET code="ColorBoxes.class" width="600" height="300">
<PARAM name="delay" value="10000000">
</APPLET>
</P>
<P>
<A href="ColorBoxes.java">The Source</A>
</P>
</BODY>
</HTML>
Events in Java
In Java most objects within the AWT class hierarchy have the ability to observe events
occurring with respect to itself (e.g. a button can observe that it has been pressed, or a
textbox can detect that it has been written to). The object can then send this
information to any other object that has indicated it is interested in knowing about such
happenings. These notified objects can then react to these events.
actionPerformed(ActionEvent)
Button
ButtonPanel
addActionListener(this)
getObjecct()
ActionEvent
buttonPressed
Events in Java
An event source:
can register listener object
can send listeners event objects
A listener object:
implements a listener interface
can be registered as a listener to various event sources
(ActionEvent actionPerformed)
Button
ButtonPanel
addActionListener(this)
getObjecct()
buttonPressed
An event object:
ActionEvent
MouseMotionListener
public void mouseMoved (MouseEvent e)
public void mouseDragged (MouseEvent e)
KeyListener
public void keyPressed(KeyEvent e)
public void keyReleased(keyEvent e)
public void keyTyped(keyEvent e)
getID
getSource
setKeyChar
getKeyCode
isActionKey
getModifiers
setModifiers
isAltDown,
isShiftDown,
isControlDown
getY
getClickCount
getModifiers
Testing an Applet
Construct an HTML file that will locate your applet in a region of an html
page.
Run appletviewer with the name of the html file where you call your applet.