A Java applet is an applet delivered to the 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 were introduced in the first version of the Java language in 1995. Java applets are usually written in the Java programming language but they can also be written in other languages that compile to Java bytecode such as Jython or Eiffel
- There is no main method.
- Two methods that are called automatically- init() and paint()
- The init method initializes variables and objects; if you don't have one you will inherit one from the JApplet class.
- Use paint to draw screen
- A lot of methods exist in JApplet class so the "extends" keyword inherits everything that the class has. In the above example JApplet is parent class and shellapplet is the subclass so use the keyword "extends" to create inheritance.
- You have to import JApplet and java.awt.Graphics (abstract windowing toolkit) to get Graphics to paint.
- All applets must inherit JApplet
- Use the "super" keyword in subclass to invoke method in the superclass.
- super.paint( g ); this says uses the paint method from JApplet in my paint class and I'm not adding anything to it.
Example of Applet
/* Java applet to demonstrate the use of classes. This is the exmample from Chapter 10 of Bell & Parr */ import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class PlayBalloon extends Applet implements ActionListener { private Button grow, shrink, left, right; private ColoredBalloon myRedBalloon; public void init() { makeGui(); myRedBalloon = new ColoredBalloon(255, 0, 0); } // init public void paint(Graphics g) { myRedBalloon.display(g); } // paint public void actionPerformed(ActionEvent e) { if (e.getSource() == grow) myRedBalloon.grow(); if (e.getSource() == shrink) myRedBalloon.shrink(); if (e.getSource() == left) myRedBalloon.left(); if (e.getSource() == right) myRedBalloon.right(); repaint(); } // actionPerformed private void makeGui() { grow = new Button("Grow"); add(grow); grow.addActionListener(this); shrink = new Button("Shrink"); add(shrink); shrink.addActionListener(this); left = new Button("Left"); add(left); left.addActionListener(this); right = new Button("Right"); add(right); right.addActionListener(this); } // makeGui } // PlayBaloon //---contents of Balloon.java------ /* The Balloon class is defined here */ import java.awt.*; class Balloon { int diameter = 10; int xCoord = 20, yCoord = 50; public void display(Graphics g) { g.drawOval(xCoord, yCoord, diameter, diameter); } // display public void left() { xCoord = xCoord - 10; } // left public void right() { xCoord = xCoord + 10; } // right public void grow() { diameter = diameter + 5; } // grow public void shrink() { diameter = diameter - 5; } // shrink } // Balloon class ColoredBalloon extends Balloon { private Color color = new Color(200, 200, 200); // some default color public ColoredBalloon(int r, int g, int b) { color = new Color(r, g, b); } public void display(Graphics g) { g.setColor(color); g.fillOval(xCoord, yCoord, diameter, diameter); } // display } // ColoredBalloon Advantages
A Java applet can have any or all of the following advantages:
- It is simple to make it work on Linux, Windows and Mac OS 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 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 (JVM will need to restart each time the browser starts fresh).
- It can move the work from the server to the client, making a web solution more scalable with the number of users/clients
- If standalone program (like Google Earth) talks to the web server, that server normally needs to support also previous versions as the user may not keep it always updated. Differently, the browser updates the applet so there is no need to support the legacy versions. Only due configuration mistakes the applet may get stuck in the cache and have issues when new versions come out.
- The applet naturally supports the changing user state like 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 call init() and start() on the applet, thus allowing for development in their favorite J2SE 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 applet much safer to run than standalone executable that it could replace. However signed applet can have full access to the machine it is running on if the user agrees.
Disadvantages
A Java applet may have any of the following disadvantages:
- It requires the Java plug-in that may not available on some less popular web browser or operating systems.
- Some organizations only allow software installed by the administrators. As a result, some users can only view applets that are important enough to contact the administrator asking to install Java plug-in.
- As with any client side scripting, security restrictions may make difficult or even impossible for untrusted applet to achieve the desired goals.
- Some more badly designed code may require a specific JRE.
0 comments:
Post a Comment