applets_unit8 oops

47
Java programs are classified into 2 types: a)Applets b)Applications Applications: Application is a ordinary java program. Applets : Applets are small Java programs that are embedded in Web pages. They can be transported over the Internet from one computer to another. It is also a special Java program that can be embedded in HTML documents. A Java applet is

Upload: ramakrishna-battula

Post on 11-Jun-2017

225 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Applets_unit8 oops

Java programs are classified into 2 types:

a)Applets

b)Applications

Applications: Application is a ordinary java program.

Applets : Applets are small Java programs that are embedded in Web pages. They can be transported over the Internet from one computer to another. It is also a special Java program that can be embedded in HTML documents. A Java applet is a Java program written in a special format to have a graphical user interface. The graphical user interface is also called a GUI , and it allows a user to interact with a program by clicking the mouse, typing information into boxes,

Page 2: Applets_unit8 oops

and performing other familiar actions. With a Java applet, GUIs are easy to create even if you've never run into such GUI before.

When we start writing applet program, we need to import two packages namely:

1.java.awt: AWT stands for abstract window toolkit. This supports window based graphical interface such as drawing screen,windows, buttons, text fields, check boxes,radio buttons, drop down lists etc.

2.java.applet: This is essential because we need to use Applet class in our applet program which is included in this package.

Page 3: Applets_unit8 oops

java.awt.Graphics.: A Graphics is something you can paint on

g.drawString(“Hello”, 20, 20); Hello

g.drawRect(x, y, width, height);

g.fillRect(x, y, width, height);

g.drawOval(x, y, width, height);

g.fillOval(x, y, width, height);

Without those import statements, the Java compiler would not recognize the classes Applet and Graphics, which the applet class refers to.

Life Cycle Of an Applet : These are the different stages involved in the life cycle of an applet:

Page 4: Applets_unit8 oops

Initialization State

Running state

Idle or stopped state

Dead state

Display state

Initialization State : This state is the first state of the applet life cycle. An applet is

Page 5: Applets_unit8 oops

created by the method init(). This method initializes the created applet. It is called exactly once in an applet's life when applet is first loaded, which is after object creation, e.g. when the browser visits the web page for the first time.Used to read applet parameters, start downloading any other images or media files, etc.

General form is:

Public void init(){ bgColor = Color.cyan; setBackground(bgColor);}

Page 6: Applets_unit8 oops

Running State : This state is the second stage of the applet life cycle. This state comes when start() method is called. Called at least once.Called when an applet is started or restarted, i.e., whenever the browser visits the web page.General form is :

Public void start(){ -----

-----}

Idle or Stopped State : This state is the third stage of the applet life cycle. This state comes when stop() method is

Page 7: Applets_unit8 oops

called implicitly or explicitly.stop() method is called implicitly. It is called at least once when the browser leaves the web page when we move from one page to another. This method is called only in the running state and can be called any number of times.

General form:

Public void stop(){----

----}

Dead State : This state is the fourth stage of the applet life cycle. This state

Page 8: Applets_unit8 oops

comes when destroy() method is called. In this state the applet is completely removed from the memory. It called exactly once when the browser unloads the applet.Used to perform any final clean-up. It occurs only once in the life cycle. General form:

Public void destroy(){ super.destroy();}

Display State : This state is the fifth stage of the applet life cycle. This state comes when use s the applet displays something on the screen. This is achieved by calling paint()

Page 9: Applets_unit8 oops

method.Paint() method can be called any number of times.This can be called only in the running state.This method is a must in all applets. IGeneral form:

Public void paint(Graphics obj){ ------

------}

Hello World as an Applet

Now we are in the coding step for the Applet version. Because we must follow the rules for naming our source files. We'll name our Applet HelloWorld, so the source file will be

Page 10: Applets_unit8 oops

called HelloWorld.java. Make sure you use the proper case.

Activity: Hello World Applet

1. Go to your edit window. Make sure you are in your working directory, and edit the file HelloWorld.java.

2. edit HelloWorld.java3. In your HelloWorld.java source

file, type in the following code. Make sure you put in your name instead of "Your Name"!

Import java.awt.*;

Import java.applet.*;

Public class HelloWorld extends Applet

Page 11: Applets_unit8 oops

{

Public void paint(Graphics g)

{

g.drawString(“Hello World”,70,98);

}

}

This will be the basic structure for every Java Applet you write. You may want to start by typing in this basic code first. You can then make sure it compiles and runs before coding any of the details.

After entering in the code, compile your program.

To compile with the standard Java compiler command, switch to your DOS

Page 12: Applets_unit8 oops

window (your run window) and run the javac command:

Activity: Compiling HelloWorld.java

1. In your run window, compile the source file HelloWorld.java using the javac command.

2. javac HelloWorld.java3. If it compiled properly, you should

see your DOS prompt again. Compiling created a new file called HelloWorld.class. You can use the DOS dir command to check it. If you type this:

dir HelloWorld.*

You should see something like this:

HelloWorld.class

Page 13: Applets_unit8 oops

This is where you may make one of the most common errors when writing a Java Applet. Remember, for Applets, we must also write some HTML code and then use a browser or the appletviewer command to run them. If you try to run your HelloWorld Applet using the java command, which only works for applications, you will get an error:

Activity: Error - Running the HelloWorld Applet with the java command

1. Running HelloWorld with the java command will generate and error because it is not an application.

java HelloWorld

2. One difference between an application and an Applet is that

Page 14: Applets_unit8 oops

applications must have a main(). Our Applet does not, so we see the error message:

3. Exception in thread "main" java.lang.NoSuchMethodError: main

Let's continue on properly and create our HTML file.

Activity: HTML code for the Hello World Applet

1. In your edit window, use the edit command to open a file called HelloWorld.html. If you are still in edit, you can just select File New. If you are at the DOS prompt, type:

2. edit HelloWorld.html3. Now type in the following HTML

code.

Page 15: Applets_unit8 oops

4. <html>5. <head>6. <title>Hello World by Your

Name</title>7. </head>8. <body>9. <p>Hello World Applet10. <applet code="HelloWorld.class"

width=300 height=200>11. </applet>12. </body>13. </html>14. Note: The added code to run

under any browser

Activity: Running the HelloWorld Applet

1. Run the HelloWorld Applet with appletviewer.

Page 16: Applets_unit8 oops

appletviewer HelloWorld.html

2. You will see the output from your Applet.

3. Click on the when finished. you will be back at the command prompt:

4. You could also run your Applet in your browser window. For the URL, type in the path for your HTML file, for instancec:\java\source\HellowWorld.html.

Page 17: Applets_unit8 oops

prog 2:

import java.applet.*;

import java.awt.*;

/*

<applet code=”Main.class” width=200 height=100>

Page 18: Applets_unit8 oops

*/

public class Main extends Applet{

public void paint(Graphics g){

g.drawString("Welcome in Java Applet.",40,20);

}

}

import java.applet.*;

import java.awt.*;

/*

<applet code="Main1.java" width=300 height=100>

Page 19: Applets_unit8 oops

</applet>

*/

public class Main1 extends Applet{

public void paint(Graphics g){

g.drawString("Welcome in Java Applet.",40,20);

}

}

import java.awt.*;

import java.applet.*;

Page 20: Applets_unit8 oops

import java.awt.event.*;

/* <applet code="Fact.java" width=300

height=100>

</applet>

*/

public class Fact extends Applet

implements ActionListener

{

int n,f=1;

Button compute;

TextField t1;

String s1,s2;

public void init()

Page 21: Applets_unit8 oops

{

t1=new TextField();

compute = new Button("compute");

add(t1);

add(compute);

t1.setText("0");

compute.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

String s=ae.getActionCommand();

if(s.equals("compute"))

{ s1=t1.getText();

Page 22: Applets_unit8 oops

n=Integer.parseInt(s1);

while(n!=0)

{

f*=n;

n--;

}

s2=String.valueOf(f);

}

repaint();

}

public void paint(Graphics g)

{

g.drawString("factorial:"+s2,6,50);

Page 23: Applets_unit8 oops

}

}

import java.awt.*;

import java.applet.*;

/*

<applet code="Line1" width=300 height=100>

</applet>

*/

publicclass Line1 extends Applet

Page 24: Applets_unit8 oops

{

publicvoid paint(Graphics g)

{

g.drawLine(10,10,50,50);

g.drawRect(10,60,40,30);

g.setColor(Color.blue);

g.fillRect(60,10,30,80);

g.setColor(Color.green); g.drawRoundRect(10,100,80,50,10,10);

g.setColor(Color.yellow); g.fillRoundRect(20,110,60,30,5,5);

g.setColor(Color.red);

Page 25: Applets_unit8 oops

g.drawLine(100,10,230,140);

g.drawLine(100,140,230,10);

g.drawString("Line Rectangles Demo",65,180);

g.drawOval(230,10,200,150);

g.setColor(Color.blue);

g.fillOval(245,25,100,100);

}

}

Page 26: Applets_unit8 oops

import java.applet.*;

import java.awt.*;

/*<APPLET CODE="FillColor.class" WIDTH="800" HEIGHT="500"></APPLET>

*/

public class FillColor extends Applet{

public void paint(Graphics g){

g.drawRect(300,150,200,100);

g.setColor(Color.yellow);

g.fillRect( 300,150, 200, 100 );

g.setColor(Color.magenta); g.drawString("Rectangle",500,150);

}

Page 27: Applets_unit8 oops

}

import java.applet.*;

import java.awt.*;

/*<APPLET CODE="CircleLine.class" WIDTH="800" HEIGHT="500"></APPLET>

*/

public class CircleLine extends Applet{

Page 28: Applets_unit8 oops

int x=300,y=100,r=50;

public void paint(Graphics g){

g.drawLine(3,300,200,10);

g.drawString("Line",100,100);

g.drawOval(x-r,y-r,100,100);

g.drawString("Circle",275,100);

g.drawRect(400,50,200,100);

g.drawString("Rectangel",450,100);

}

}

import java.awt.*;

import java.applet.*;

Page 29: Applets_unit8 oops

/*

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

<param name=fontName value=Courier>

<param name=fontSize value= 14>

<param name=leading value=2>

<param name=accountEnabled value=true>

</applet>

*/

public class Param extends Applet

{

String fontName;

Page 30: Applets_unit8 oops

int fontSize;

float leading;

boolean active;

public void start()

{

String param;

fontName=getParameter("fontName");

if(fontName==null)

fontName="Not Found";

param=getParameter("fontSize");

try

Page 31: Applets_unit8 oops

{

if(param!=null)

fontSize=Integer.parseInt(param);

else

fontSize=0;

}

catch(NumberFormatException e)

{

fontSize=-1;

}

param=getParameter("leading");

try

Page 32: Applets_unit8 oops

{

if (param!=null)

leading=Float.valueOf(param).floatValue();

else

leading=0;

}

catch(NumberFormatException e)

{

leading=-1;

}

Page 33: Applets_unit8 oops

param=getParameter("accountEnabled");

if(param!=null)

active=Boolean.valueOf(param).booleanValue();

}

public void paint(Graphics g)

{

g.drawString("Font name:"+ fontName,0,10);

g.drawString("Font size:" + fontSize,0,26);

Page 34: Applets_unit8 oops

g.drawString("leading:" + leading,0,42);

g.drawString("Account Active:" + active,0,58);

}

}