Classe Applet |
Une applet est un programme qui s'execute dans un navigateur. C'est un objet qui etend la classe Applet du package java.applet.Applet.
Il existe quatre methodes importantes dans une applet :
import java.applet.* ;
import java.awt.Graphics ; // necessaire pour afficher
le texte dans le browser public class mon_applet extends Applet { protected StringBuffer ou ;
{ ou = new StringBuffer("Init ok. ") ; } public void start() { ou.append("Start ok. ") ; } public void stop() public void paint(Graphics g) { g.drawString(ou.toString(), 50,10) ; } } |
Resultat de l'applet (redimensionner la fenetre du browser pour voir des changements) :
Cette applet a ete appelee dans cette page HTML par :
<APPLET code="mon_applet.class" width=500 height=12></APPLET>
Voici une modification du programme precedent ou l'on recupere des clics de la souris .
NB : La classe Applet herite entre autres de la classe Component. C'est dans cette classe qu'est definie la methode paint(). Nous la surchargeons ici pour afficher notre chaine de caracteres.
public void paint(Graphics
g)
Paints the component. This method is called when the contents of the component should be painted in response to the component first being shown or damage needing repair. The clip rectangle in the Graphics parameter will be set to the area which needs to be painted. Parameters: g - the specified Graphics window See Also: update
Updates the component. This method is called in response to a call to repaint. You can assume that the background is not cleared. Parameters: g - the specified Graphics window See Also: paint, repaint
public void paintAll(Graphics g) Paints the component and its subcomponents. Parameters: g - the specified Graphics window See Also: paint
public void repaint() Repaints the component. This will result in a call to update as soon as possible. See Also: paint
Repaints the component. This will result in a call to update within tm milliseconds. Parameters: tm - maximum time in milliseconds before update See Also: paint public void repaint(int x, int y, int width, int height) Repaints part of the component. This will result in a call to update as soon as possible. Parameters: x - the x coordinate
y - the y coordinate width - the width height - the height public void repaint(long tm, int x, int y, int width, int height) Repaints part of the component. This will result in a call to update width tm millseconds. Parameters: tm - maximum time in milliseconds before update x - the x coordinate y - the y coordinate width - the width height - the height |
Ph. RIS 1997