You are on page 1of 9

Applets

Applications Java programs which run stand alone Web-based Java Applications Applets Dynamic and interactive programs that can run inside a web page displayed by a browser enabled for Java Servlets Programs that run inside request/response oriented servers JavaServer Pages An extension to the servlet architecture. Allow for the separation of the display of web content and the generation of that content

Java applet can have any or all of the following advantages:[27] It is simple to make it work on Linux, Microsoft Windows and Mac OS X i.e. to make it cross platform. Applets are supported by most web browsers. The same applet can work on "all" installed versions of Java at the same time, rather than just the latest plug-in version only. However, if an applet requires a later version of the Java Runtime Environment (JRE) the client will be forced to wait during the large download. Most web browsers cache applets, so will be quick to load when returning to a web page. Applets also improve with use: after a first applet is run, the JVM is already running and starts quickly (the JVM will need to restart each time the browser starts afresh). It can move the work from the server to the client, making a web solution more scalable with the number of users/clients. If a standalone program (like Google Earth) talks to a web server, that server normally needs to support all prior versions for users which have not kept their client software updated. In contrast, a properly configured browser loads (and caches) the latest applet version, so there is no need to support legacy versions. The applet naturally supports the changing user state, such as figure positions on the chessboard. Developers can develop and debug an applet direct simply by creating a main routine (either in the applet's class or in a separate class) and calling init() and start() on the applet, thus allowing for development in their favorite Java SE development environment. All one has to do after that is re-test the applet in the AppletViewer program or a web browser to ensure it conforms to security restrictions. An untrusted applet has no access to the local machine and can only access the server it came from. This makes such an applet much safer to run than a standalone executable that it could replace. However, a signed applet can have full access to the machine it is running on if the user agrees. Java applets are fast - and can even have similar performance to native installed software.

[edit] Disadvantages

A Java applet may have any of the following disadvantages: It requires the Java plug-in. Some browsers, notably mobile browsers running Apple iOS or Android do not run Java applets at all.[28][29] Some organizations only allow software installed by the administrators. As a result, some users can only view applets that are important enough to justify contacting the administrator to request installation of the Java plug-in. As with any client-side scripting, security restrictions may make it difficult or even impossible for an untrusted applet to achieve the desired goals. Some applets require a specific JRE. This is discouraged. [30] If an applet requires a newer JRE than available on the system, or a specific JRE, the user running it the first time will need to wait for the large JRE download to complete. Java automatic installation or update may fail if a proxy server is used to access the web. This makes applets with specific requirements impossible to run unless Java is manually updated. The Java automatic updater that is part of a Java installation also may be complex to configure if it must work through a proxy. Unlike the older applet tag, the object tag needs workarounds to write a cross-browser HTML document. hapter 1 - Graphical Interface 1. Hello World Example

/* By Bavo Bruylandt (Http://www.realapplets.com") */ // and now The inevidable "Hello World" example :) // tell the compiler where to find the methods you will use. // required when you create an applet import java.applet.*; // required to paint on screen import java.awt.*;

// the start of an applet - HelloWorld will be the executable class // Extends applet means that you will build the code on the standard Applet class public class HelloWorld extends Applet { // The method that will be automatically called when the applet is started public void init() { // It is required but does not need anything. }

// This method gets called when the applet is terminated

// That's when the user goes to another page or exits the browser. public void stop() { // no actions needed here now. }

// The standard method that you have to use to paint things on screen // This overrides the empty Applet method, you can't called it "display" for example. public void paint(Graphics g) { //method to draw text on screen // String first, then x and y coordinate. g.drawString("Hey hey hey",20,20); g.drawString("Hellooow World",20,40); } }

// That's it. Next is drawing special shapes on screen and using fonts. // Go to DrawExample.java.

2. Drawing Shapes and using colors

/* Applet will paint special shapes and use colors and fonts Only new methods are explained */ import java.awt.*; import java.applet.*; public class DrawExample extends Applet { // Specify variables that will be needed everywhere, anytime here // The font variable Font bigFont; // The colors you will use Color redColor; Color weirdColor;

Color bgColor; public void init() { // Here we will define the varibles further // Will use Arial as type, 16 as size and bold as style // Italic and Plain are also available bigFont = new Font("Arial",Font.BOLD,16); // Standard colors can be named like this redColor = Color.red; // lesser known colors can be made with R(ed)G(reen)B(lue). weirdColor = new Color(60,60,122); bgColor = Color.blue; // this will set the backgroundcolor of the applet setBackground(bgColor); } public void stop() { } // now lets draw things on screen public void paint(Graphics g) { // tell g to use your font g.setFont(bigFont); g.drawString("Shapes and Colors",80,20); // Now we tell g to change the color g.setColor(redColor); // This will draw a rectangle (xco,yco,xwidth,height); g.drawRect(100,100,100,100); // This will fill a rectangle g.fillRect(110,110,80,80); // change colors again g.setColor(weirdColor); // a circle (int x, int y, int width, int height,int startAngle, int arcAngle); // ovals are also possible this way. g.fillArc(120,120,60,60,0,360);

g.setColor(Color.yellow); // Draw a line (int x1, int y1, int x2, int y2) g.drawLine(140,140,160,160); // reset the color to the standard color for the next time the applets paints // an applet is repainted when a part was'nt visible anymore // happens most often because of browser minimizing or scrolling. g.setColor(Color.black); } } // that's some basic drawing. // next is drawing images on screen // go to imageExample.java

1. Displaying Images

/* This applet will display a gif on screen for now the gif file must be in the same directory as this applet */ import java.awt.*; import java.applet.*; // These classes are for Url's. import java.net.*; public class ImageExample extends Applet { // Your image name; Image my_gif; // The applet base URL URL base; // This object will allow you to control loading MediaTracker mt; public void init() {

// initialize the MediaTracker mt = new MediaTracker(this); // The try-catch is necassary when the URL isn't valid // Ofcourse this one is valid, since it is generated by // Java itself. try { // getDocumentbase gets the applet path. base = getDocumentBase(); } catch (Exception e) {} // Here we load the image. // Only Gif and JPG are allowed. Transparant gif also. my_gif = getImage(base,"imageExample.gif"); // tell the MediaTracker to kep an eye on this image, and give it ID 1; mt.addImage(my_gif,1); // now tell the mediaTracker to stop the applet execution // (in this example don't paint) until the images are fully loaded. // must be in a try catch block. try { mt.waitForAll(); } catch (InterruptedException e) {} // when the applet gets here then the images is loaded. } public void paint(Graphics g) { // now we are going to draw the gif on the screen // (image name,x,y,observer); g.drawImage(my_gif,20,20,this); // you can resize the image easily g.drawImage(my_gif,20,140,30,40,this);

} } // That's all. Images can alos be drawn without the mediaTracker but then // the screen will flicker. // a base URL variable is also not necessary but when you have lots of images to

load // then it's shorter to type. // Next is the basic GUI (Grphical User Interface) components. // go to guiExample.java

Using basic components

/* Now the applet will have a GUI These are elements to interact with the user. In this example they will not perform any actions. We will use no Layout manager; */ import java.awt.*; import java.applet.*; public class GuiExample extends Applet { // A Button to click Button okButton; // A textField to get text input TextField nameField; // A group of radio buttons // necessary to only allow one radio button to be selected at the same time. CheckboxGroup radioGroup; // The radio buttons to be selected Checkbox radio1; Checkbox radio2; // An independant selection box Checkbox option;

public void init() { // Tell the applet not to use a layout manager. setLayout(null); // initialze the button and give it a text. okButton = new Button("A button"); // text and length of the field nameField = new TextField("A TextField",100); // initialize the radio buttons group radioGroup = new CheckboxGroup(); // first radio button. Gives the label text, tells to which // group it belongs and sets the default state (unselected) radio1 = new Checkbox("Radio1", radioGroup,false); // same but selected radio2 = new Checkbox("Radio2", radioGroup,true); // Label and state of the checkbox

option = new Checkbox("Option",false); // now we will specify the positions of the GUI components. // this is done by specifying the x and y coordinate and //the width and height. okButton.setBounds(20,20,100,30); nameField.setBounds(20,70,100,40); radio1.setBounds(20,120,100,30); radio2.setBounds(140,120,100,30); option.setBounds(20,170,100,30); // now that all is set we can add these components to the applet add(okButton); add(nameField); add(radio1); add(radio2); add(option); } } // Thats's it, you now have given the user visual options. // However there are no actions related to these comonents. // You will see that later, first an example of a layoutmanager // that makes it easier to place components. // Go to LayoutExample.

Home - Chapter 1 - Chapter2 - C Layout of GUI

/* This example demonstrated the use of a layout manager to place components The Layout manager here is BorderLayout */

import java.awt.*; import java.applet.*; public class LayoutExample extends Applet { Button okButton1; Button okButton2; Button okButton3; Button okButton4;

Button okButton5; public void init() { // sets the LayoutManager to BorderLayout setLayout(new BorderLayout()); okButton1 = new Button("Centered Button"); okButton2 = new Button("Cold North"); okButton3 = new Button("Go West"); okButton4 = new Button("At East"); okButton5 = new Button("Hot South"); // always says where the component should be placed when adding // Options are center,East,West,Nort and South add(okButton1,"Center"); add(okButton2,"North"); add(okButton3,"West"); add(okButton4,"East"); add(okButton5,"South"); } }

// There are more layout managers available. The easiest is // FlowLayout() which will kee adding components horizontally until // there is no more space, then it will continue a row lower. // Now that you are able to create a GUI let's bind actions to those // components. Wouldn't be handy? :) // Go to ActionExample http://www.realapplets.com/tutorial/HelloWorld.html

You might also like