Java Chapter Two
Java Chapter Two
JAVA APPLET
JAVA APPLET
To start the applet's execution, such as when the applet's loaded or when the
user revisits a page that contains the applet.
.
paint()
• This method is called by applet container after init() and start()
methods.
• Painting is how an applet actually draws something on the screen, be
it text, a line, a colored background, or an image.
• You override the paint() method if your applet needs to have an
actual appearance on the screen (that is, most of the time).
• The paint method is where the real work of this applet (what little
work goes on) really occurs.
Cont.…
• The paint( ) method has one parameter of type Graphics.
• This parameter will contain the graphics context, which describes the
graphics environment in which the applet is running.
• This context is used whenever output to the applet is required.
• called back when the applet drawing area must be refreshed, e.g.,
another window partially covers the applet
cont.….
Example
public void paint(Graphics g) {
g.drawString("Hello again!", 5, 40);
}
stop( )
• The stop( ) method is called when a web browser leaves the HTML
document containing the applet
• when it goes to another page, for example.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("welcome to applet",150,150);
} }
java applet vs java application
Java application Java Applet
• Java Application is a type of • A Java Applet is a small program that
program that can get independently makes use of another application
executed on a computer. program so that we can execute it.
• The execution of the Java application • The Java applet initializes through the
begins with the main() method. The init(). It does not require the usage of
usage of the main() is a prerequisite any main() method.
here. • It cannot run independently but
• It cannot run alone, but it requires requires APIs for its execution (Ex.
JRE for its execution. APIs like Web API).
Cont.….
• public void fillOval(int x, int y, int width, int height): is used to fill oval
with the default color and specified width and height.
• public void drawLine(int x1, int y1, int x2, int y2): is used to draw line
between the points(x1, y1) and (x2, y2).
• public boolean drawImage(Image img, int x, int y, ImageObserver
observer): is used draw the specified image.
cont.….
• public abstract void drawArc(int x, int y, int width, int height, int
startAngle, int arcAngle): is used draw a circular or elliptical arc.
• public abstract void fillArc(int x, int y, int width, int height, int
startAngle, int arcAngle): is used to fill a circular or elliptical arc.
• public abstract void setColor(Color c): is used to set the graphics
current color to the specified color.
• public abstract void setFont(Font font): is used to set the graphics
current font to the specified font.
Eg. Drawing Lines and Rectagles
import java.awt.*;
import java.applet.*;
public class LineRect extends Applet {
public void paint(Graphics g) {
g.drawstring(“Draw line and rectangle”,5,5);
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30); }}
Eg. Drawing fill rectangle and
drawRoundRect
import java.awt.*;
import java.applet.*;
public class LineRect extends Applet {
public void paint(Graphics g) {
g.fillRect(60,10,30,80);
g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(20,110,60,30,5,5);
}}
THE END