12

Afficher un texte avec une bordure

  1. // <applet code="Text3D" width="300" height="100">
  2. // <param name="text" value="Java is so much fun!"/>
  3. // </applet>
  4.  
  5. import java.applet.Applet;
  6. import java.awt.*;
  7.  
  8. public class Text3D extends Applet {
  9.     protected String text = "?";
  10.  
  11.     public void init() {
  12.         String p = getParameter("text");
  13.         if (p != null)
  14.             text = p;
  15.         setBackground(Color.CYAN);
  16.         setFont(new Font("Serif", Font.BOLD, 20));
  17.     }
  18.  
  19.     public void paint(Graphics g) {
  20.         Dimension size = getSize();
  21.         FontMetrics fm = g.getFontMetrics();
  22.  
  23.         int w = fm.stringWidth(text);
  24.         int h = fm.getHeight();
  25.         int x = (size.width - w) / 2;
  26.         int y = (size.height - h) / 2;
  27.  
  28.         g.setColor(Color.PINK);
  29.         g.fill3DRect(x - 8, y - 8, w + 16, h + 16, true);
  30.         g.draw3DRect(x - 8, y - 8, w + 16, h + 16, true);
  31.         g.draw3DRect(x - 6, y - 6, w + 12, h + 12, false);
  32.  
  33.         // MUST write on baseline
  34.         g.setColor(Color.GRAY);
  35.         g.drawString(text, x, y + fm.getAscent());
  36.     }
  37. }
$ javac Text3D.java 
$ appletviewer Text3D.java

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].