Sie sind auf Seite 1von 7

WEB TECHNOLOGY LAB - II 1

Java Applet
A Java applet is an applet delivered to users in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Sun's AppletViewer, a stand-alone tool for testing applets. Java applets are written in programming languages that compile to Java bytecode, usually in Java. Since Java's bytecode is cross-platform or platform independent, Java applets can be executed by browsers for many platforms, including Microsoft Windows, UNIX, Mac OS and Linux. It is also trivial to run a Java applet as an application with very little extra code. This has the advantage of running a Java applet in offline mode without the need for any Internet browser software and also directly from the Integrated Development Environment (IDE).

An applet is any small application that performs one specific task that runs within the scope of a larger program, often as a plug-in. Provided that an applet is hosted by an operating system, it can function as any other normal software application but is and performs only a small set of tasks. Applets are used to provide interactive features to web applications that cannot be provided by HTML alone. They can capture mouse input and also have controls like buttons or check boxes. In response to the user action an applet can change the provided graphic content

A Java virtual machine (JVM) is software that is implemented on virtual and non-virtual hardware and on standard operating systems. A JVM provides an environment in which Java bytecode can be executed, enabling such features as automated exception handling, which provides "root-cause" debugging information for every software error (exception), independent of the source code.

Bytecode is computer object code that is processed by a program, usually a virtual machine. The virtual machine converts each generalized machine instruction into a specific machine instruction or instructions that this computer's processor will understand. Bytecode is the result of compiling source code written in a language that supports this approach. The best-known language today that uses the bytecode and virtual machine approach is Java. Rather than being interpreted one instruction at a time, Java bytecode can be recompiled at each particular system platform by a just-intime compiler. Usually, this will enable the Java program to run faster. In Java, bytecode is contained in a binary file with a .CLASS suffix. Java bytecode is the form of instructions that the Java virtual machine executes. Each bytecode opcode is one byte in length, although some require parameters, resulting in some multi-byte instructions. A bytecode program is normally executed by parsing the instructions one at a time. This kind of bytecode interpreter is very portable.

AppletViewer is a standalone, command line program from Sun to run Java applets. AppletViewer is generally used by developers for testing their applets before being deployed to a website. Java applets do not involve the use of a web browser. Even though the applet viewer logically takes the place of a web browser, it functions very differently from a web browser. The applet viewer operates on HTML documents, but all it looks for is embedded applet tags; any other HTML code in the document is ignored. The only drawback to using the applet viewer is that it will not show how an applet will run within the confines of a real web setting.

WEB TECHNOLOGY LAB - II 2

Running Java Applets


Applet programs can be run mainly in two ways: i) Through command line: Using appletviewer Synopsis: Save the program as <filename.java> Compile as javac <filename> Then, launch the appletviewer as appletviewer <filename.java> Through web browser: Using HTML pages Synopsis: Save the program as <filename.java> Compile as javac <filename> Create an HTML file including the code below: <applet code=<filename>></applet> The <filename> should be same as java file name

ii)

Example:
//<applet code="Test" height=150 width=200> //</applet> import java.awt.*; import java.applet.*; public class Test extends Applet { public void paint(Graphics g) { g.drawString("Sample", 20,30); } }

Output:

WEB TECHNOLOGY LAB - II 3

More Examples
1. Java Applet Program to print sum of passed parameters The .java file import java.awt.*; import java.applet.*; //<applet code="param" height=200 width=200> //</applet> public class param extends Applet { String val1,val2,val3; int a,b,c; public void init() { val1=getParameter("param1"); val2=getParameter("param2"); a=Integer.parseInt(val1); b=Integer.parseInt(val2); c=a+b; val3=String.valueOf(c); } public void paint(Graphics g) { g.drawString("Parameter 1: "+val1, 20, 40); g.drawString("Parameter 2: "+val2, 20, 60); g.drawString("Sum: "+val1+" + "+val2+" = "+val3, 20, 90); } } The .html file <html> <body> <applet code="param" width=300 height=200> <param name="param1" value=22 /> <param name="param2" value=33 /> </applet> </body> <html> Output:

WEB TECHNOLOGY LAB - II 4 2. Java Applet Program to take input from user

//<applet code="AppletInput" height=400 width=600> //</applet> import java.awt.*; import java.applet.*; public class AppletInput extends Applet { TextField text1, text2; public void init() { text1 = new TextField(8); text2 = new TextField(8); add(text1); add(text2); text1.setText("0"); text2.setText("0"); } public void paint(Graphics g) { int x=0,y=0,z=0; String s1,s2,s; g.drawString("Input a number in each box try { s1 = text1.getText(); x = Integer.parseInt(s1); s2 = text2.getText(); y = Integer.parseInt(s2); } catch(Exception e) {} z = x + y; s = String.valueOf(z); g.drawString("The Sum is : ",20,75); g.drawString(s,100,75); } public boolean action(Event event, Object obj) { repaint(); return true; } } Output:

",10,20);

WEB TECHNOLOGY LAB - II 5 3. Java Applet Program to draw a joker (human face) import java.awt.*; import java.applet.*; //<applet code="joker" height=200 width=200> //</applet> public class joker extends Applet { int x[]={100,50,150}; int y[]={10,60,60}; public void paint(Graphics g) { g.drawOval(40,40,120,150); g.drawOval(57,75,30,20); g.drawOval(110,75,30,20); g.fillOval(68,81,10,10); g.fillOval(121,81,10,10); g.setColor(Color.pink); g.fillOval(85,100,30,30); g.setColor(Color.red); g.fillArc(60,125,80,40,180,180); g.setColor(Color.blue); g.drawOval(25,92,15,30); g.drawOval(160,92,15,30); g.setColor(Color.red); g.fillPolygon(x,y,3); } } Essential Code Review:
drawOval(int startx, int starty, int width, int height) // Draws an oval within the parameter ranges fillOval(int startx, int starty, int width, int height) //Fills an oval bounded by the specified rectangle with the current color. fillArc(int startx, int starty, int width, int height, int startAngle, int arcAngle) //Fills a circular or elliptical arc covering the specified rectangle. fillPolygon(int[] xPoints, int[] yPoints, int nPoints) //Fills a closed polygon defined by arrays of x and y coordinates. setColor(Color c) //Sets this graphics context's current color to the specified color.

// // // // //

head eyelid(l) eyelid(r) eyeball(l) eyeball(r)

// nose // mouth // ear(l) // ear(r) // hat

Output:

WEB TECHNOLOGY LAB - II 6 4. Java Applet Program to use buttons in actionListener //<applet code="applet_button" width=250 height=150> //</applet> import java.awt.*; import java.applet.*; import java.awt.event.*; public class applet_button extends Applet implements ActionListener { Button b1,b2; String s; public void init() { b1=new Button("one"); b2=new Button("two"); add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e) { s=e.getActionCommand(); if (s.equals("one")) s="one is pressed"; else if (s.equals("two")) s="two is pressed"; else s="any button is pressed"; repaint(); } public void paint(Graphics g) { g.drawString(s,20,100); } }

Output:

WEB TECHNOLOGY LAB - II 7 5. Java Applet Program to sum two numbers using three textboxes and a button

//<applet code="action_button" height=100 width=400> //</applet> import java.awt.*; import java.applet.*; import java.awt.event.*; public class action_button extends Applet implements ActionListener { TextField t1,t2,t3; Button b1; int n,x,sum; public void init() { t1=new TextField(10); add(t1); System.out.println(newLine); t2=new TextField(10); add(t2); System.out.println(newLine); t3=new TextField(10); b1=new Button("Add"); add(b1); b1.addActionListener(this); add(t3); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Add")) n=Integer.parseInt(t1.getText()); x=Integer.parseInt(t2.getText()); sum=n+x; String s=String.valueOf(sum); t3.setText(s); } } Output:

Das könnte Ihnen auch gefallen