1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
package appletdemo;
import java.io.*;
import java.awt.Graphics;
import javax.swing.JApplet;
import java.net.*;
import java.util.Enumeration;
/**
* Applet qui affiche la propriété java.version //Javadoc
* et lit le contenu d'un fichier distant.
*/
public class Applet extends JApplet {
/**
* //sérialisation pour la classe Demonstration
*/
private static final long serialVersionUID = 1L;
private int positionVerticale; //pour l'affichage
private URL urlFichier; //url du fichier à afficher
private void afficherPropriete(String nomPropriete, Graphics g) { //g représente l'emplacement de la pageWeb où l'applet s'affichera
try {
g.drawString("Propriété : " + nomPropriete + " = "
+ System.getProperty(nomPropriete),
0, positionVerticale);
}
catch(SecurityException e) {
g.drawString("Propriété : " + nomPropriete
+ " ne peut être affichée",
0, positionVerticale);
}
positionVerticale += 20;
}
public void init() {//la méthode init() est héritée de la classe Applet
//la méthode init() est exécutée seulement quand l'applet est lancé pour la première fois
URL urlDocBase = getDocumentBase();
System.out.println("URL de base du document : " + urlDocBase);
String nomFichier = getParameter("fichier");
try {
urlFichier = new URL(urlDocBase, nomFichier);
System.out.println("URL du fichier à lire : " + urlFichier);
}
catch(MalformedURLException e) {
System.err.println(e);
}
}
public void paint(Graphics g) { //la méthode paint() est héritée de la classe Applet
//la méthode paint dessine l'applet dans la page Web
positionVerticale = 20;
afficherPropriete("java.version", g);
//afficherPropriete("java.home", g);
//afficherPropriete("user.name", g);
//afficherPropriete("user.home", g);
//Affichage de toutes les propriétés
try {
Enumeration enumProprietes = System.getProperties().propertyNames();
while(enumProprietes.hasMoreElements()) {
String nomPropriete = (String)enumProprietes.nextElement();
System.out.println("Propriété : " + nomPropriete);
afficherPropriete(nomPropriete, g);
}
}
catch(SecurityException e) {
System.out.println("Impossible de lire la liste des propriétés");
}
//Affichage du contenu du fichier
String ligne;
g.drawString("3 premières lignes de " + urlFichier + " :",
0, positionVerticale); //Pixel où commencera l'affichage (0,position verticale)
positionVerticale += 20;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(urlFichier.openStream()));
try {
for (int i = 0; i < 3; i++) {
ligne = br.readLine();
if (ligne == null) break;
g.drawString(ligne, 0, positionVerticale);
positionVerticale += 20;
}
}
catch (IOException e) {
System.err.println("Erreur pendant la lecture des lignes");
}
finally {
try {
br.close();
}
catch(IOException e) {}
}
}
catch(IOException e) {
System.err.println("Problème pour ouvrir le fichier : "
+ urlFichier);
}
}
} |
Partager