7

SimpleSpot

  1. // <applet code="SimpleSpot1" width="200" height="100"></applet>
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.event.*;
  7.  
  8. public class SimpleSpot1 extends Applet implements MouseListener {
  9.     private Spot spot = null;
  10.  
  11.     private static final int RADIUS = 15;
  12.  
  13.     private Color spotColor = new Color(107, 45, 107);
  14.  
  15.     public void init() {
  16.         addMouseListener(this);
  17.     }
  18.  
  19.     public void paint(Graphics g) {
  20.         if (spot != null) {
  21.             int radius = spot.radius;
  22.             g.setColor(spotColor);
  23.             g.fillOval(spot.x - radius, spot.y - radius, radius * 2, radius * 2);
  24.         }
  25.     }
  26.  
  27.     public void mousePressed(MouseEvent e) {
  28.         if (spot == null)
  29.             spot = new Spot(RADIUS);
  30.         spot.x = e.getX();
  31.         spot.y = e.getY();
  32.         repaint();
  33.     }
  34.  
  35.     public void mouseClicked(MouseEvent e) {
  36.     }
  37.  
  38.     public void mouseReleased(MouseEvent e) {
  39.     }
  40.  
  41.     public void mouseEntered(MouseEvent e) {
  42.     }
  43.  
  44.     public void mouseExited(MouseEvent e) {
  45.     }
  46.  
  47.     private class Spot {
  48.         private int radius;
  49.  
  50.         private int x, y;
  51.  
  52.         Spot(int x, int y, int r) {
  53.             this.radius = r;
  54.             this.x = x;
  55.             this.y = y;
  56.         }
  57.  
  58.         Spot(int radius) {
  59.             this(0, 0, radius);
  60.         }
  61.     }
  62. }
$ javac SimpleSpot1.java 
$ appletviewer SimpleSpot1.java

  1. // <applet code="SimpleSpot2" width="200" height="100"></applet>
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.*;
  9.  
  10. public class SimpleSpot2 extends Applet implements MouseListener {
  11.     private Spot spot = null;
  12.  
  13.     private static final int RADIUS = 15;
  14.  
  15.     private Color spotColor = new Color(230, 182, 95);
  16.  
  17.     public void init() {
  18.         addMouseListener(this);
  19.     }
  20.  
  21.     public void paint(Graphics g) {
  22.         if (spot != null)
  23.             spot.draw(g);
  24.     }
  25.  
  26.     public void mousePressed(MouseEvent e) {
  27.         if (e.getButton() != MouseEvent.BUTTON1)
  28.             return;
  29.         if (spot == null)
  30.             spot = new Spot(0, 0, RADIUS);
  31.         // outer has access to inner class
  32.         spot.x = e.getX();
  33.         spot.y = e.getY();
  34.         // NEVER call paint()
  35.         repaint();
  36.     }
  37.  
  38.     public void mouseClicked(MouseEvent e) {
  39.     }
  40.  
  41.     public void mouseReleased(MouseEvent e) {
  42.     }
  43.  
  44.     public void mouseEntered(MouseEvent e) {
  45.     }
  46.  
  47.     public void mouseExited(MouseEvent e) {
  48.     }
  49.  
  50.     private class Spot {
  51.         private int radius;
  52.  
  53.         private int x, y;
  54.  
  55.         Spot(int x, int y, int r) {
  56.             this.radius = r;
  57.             this.x = x;
  58.             this.y = y;
  59.         }
  60.  
  61.         void draw(Graphics g) {
  62.             // duplicate Graphics and turn it into a Graphics2D
  63.             Graphics2D g2d = (Graphics2D) g.create();
  64.             // turn on antialiasing for smooth painting
  65.             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  66.             // inner class has access to outer class
  67.             g2d.setColor(spotColor);
  68.             g2d.fillOval(x - radius, y - radius, radius * 2, radius * 2);
  69.         }
  70.     }
  71. }
$ javac SimpleSpot2.java 
$ appletviewer SimpleSpot2.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].