Download View File - UET Taxila

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
OBJECT ORIENTED
PROGRAMMING
LAB # 13
Object Oriented Programming
3rd Semester-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Applets
The purpose of this laboratory session is to have a closer look at applets.
Experiment 1.0: About Applets
Applets are small applications that are accessed on an Internet Server, transported over the Internet. They
are automatically installed and are embedded in web browser.
An applet is a window based program, therefore different from console-based program and applets are
event driven. AWT notifies the applet about an event by calling an event handler of applet. Therefore an
applet does not maintain control for an extended time period.
Applets do not need a main() method for it’s execution. They actually run in appletviewer or javacomaptible browser.
Applets at least import two packages:
• Java.awt.*;
• Java.applet.*;
Applet is a child class of AWT (Abstract Window Toolkit) Panel. Panel is a child class of Container which
further is a child class of Component class. These classes, so provide support for Java’s window-based
graphical interface.
Applet Frame
Not all but most of the applets override a set of following methods that provide the basic way by
appletviewer or browser to control its execution. Four of the methods are defined by the Applet class:
• init(): It is the 1st method to be called. This method is only called once.
•
•
•
start(): It is called after init(). It can also be called to restart an applet.
stop(): It is called when a web browser leaves that document which contains applet.
destroy(): When applet is to be completely removed from the memory.
and a very important method:
• paint(): it is called when you have to draw or re-draw your output.
is provided by AWT’s Component class. Applets may not override any of these methods if they do not
need.
Sample Code
Import jav.awt.*;
Import java.applet.*;
/*
<applet code=”Sample_Code” width=400 height=200>
</applet>
*/
Applet Tag
public class Sample_Code extends Applet
{
String msg;
Object Oriented Programming
3rd Semester-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
public void init()
{
setBackground(Color.pink);
setForeground(Color.gray);
msg=”Hello ”;
}
init() method
public void paint(Graphics g)
paint() method
{
msg = ”Painting our first applet”;
g.drawString(msg,40,40);
}
}
Compiling and Running an applet
C:\........\bin>javac Sample_Code.java
C:\........\bin>appletviewer Sample_Code.java
Compilation &
Running an applet
Understanding Repaint
The repaint() method is defined by AWT. It causes the AWT run-time system to execute a call to applet’s
update() method, which in it’s default implementation calls paint(). And Inside paint()output is displayed
with the help of drawstring() method.
The repaint() method has four forms:
• void repaint().: entire window is repainted.
• void repaint(int left, int top, int width, int height): left &top defines the coordinates of upper left
corner. This version of repaint() specifies certain area to be repainted not the entire region.
• void repaint(long maxDelay, int x, int y, int width, int height): max number of milliseconds that pass
before update() is called.
• void (maxDelay).
Sample Code
import java.awt.*;
import java.applet.*;
import javax.swing.*;
/*
<applet code="SimpleApplet" width=400 height=400>
</applet>
*/
public class SimpleApplet extends Applet{
Object Oriented Programming
3rd Semester-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
public void paint(Graphics g)
{
g.drawString("A simple applet",40,40);
repaint(1000);
}
}
repaint()
Note: Explore the related methods and additions in applet tags.
Understanding Positioning
import java.awt.*;
import java.applet.*;
import javax.swing.*;
/*
<applet code="SimpleApplet" width=400 height=400>
</applet>
*/
public class SimpleApplet extends Applet{
public void paint(Graphics g)
{
Initial positions
g.drawString("A simple applet",40,40);
New positions
g.drawString("A simple applet", 50,20);
}
}
Experiment 1.1: Task
Q1) Design an applet in which you print name in this manner:
Name
Name
Name
Name
Object Oriented Programming
3rd Semester-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Q2) Design an applet in such a manner that a big rectangle appears and then small and it diminishes
finally displaying a msg: file saved
File
Saved
Q3) Design an applet in which a small ball is rotating along with a big ball. Set different colors for both the
balls to differentiate between the two.
//Hint: Explore methods: g.fillOval(x,y,a,b);
Font f = new Font("Serif" , Font.BOLD ,32);
setFont(f);
g.fillOval(x,y,a,b);
Object Oriented Programming
3rd Semester-SE
UET Taxila