/** FCercle.java tracés graphiques (un cercle) Fenêtre avec un bouton et un cercle, de dimension prédéfinie et à un emplacement défini également. La classe implante les interfaces ActionListener et WindowListener Adaptation du tracé à la taille de la fenêtre (le tracè, appelé par la méthode paint(), se fait après avoir demandé les dimensions courantes de la fenêtre. Une fenêtre est créée avec une dimension prédéfinie et à un emplacement défini également, contenant un cercle. Un bouton permet de faire touner le cercle: dans le réflexe associé au bouton clic, on appelle la méthode tourner_points avant repaint(). Tracés graphiques de droites et cercle: voir la méthode tracer_fen(); Cette fenêtre est sensible aux événements WindowEvent: elle n'est fermée qu'à la deuxième demande: voir le champ n_d_fermer */ import java.awt.*; import java.awt.event.*; public class FCercle extends Frame implements WindowListener, ActionListener { //Construire une fenêtre int n_d_fermer; // nombre de demandes de fermeture Button clic = new Button("clic"); final int nbp=100; // nombre de points pour tracer la boule double teta0; // angle initial double ptsX[], ptsY[]; // Construction initiale de la fenêtre public FCercle() { BorderLayout gg = new BorderLayout(); setLayout(gg); setSize(new Dimension(400, 300)); setLocation(100,100); setTitle("Une fenêtre de 400x300 (FCercle)"); // Recevoir les évènements 'Window' addWindowListener(this); // Un bouton add(clic,"North"); clic.addActionListener(this); // Les points teta0=0; ptsX = new double[nbp]; ptsY = new double[nbp]; } public void tourner_points() { int i,j; double teta, pas = 2 * Math.PI / nbp; teta0 += Math.PI / 32; if(teta0 > pas) teta0 -= pas; for( i=0, teta=teta0; i<nbp; i++, teta+=pas) { ptsX[i] = Math.cos(teta); ptsY[i] = Math.sin(teta); teta += pas; } } public void tracer_fen(Graphics cg) { int i,j, x1,y1, xc, yc; int hc = clic.getSize().height; Dimension dim = this.getSize(); int r = (Math.min(dim.height-2*hc,dim.width)/2)-5; xc=5+r; yc=5+2*hc+r; cg.drawOval(xc-r,yc-r,2*r,2*r); for( i=0; i<nbp; i++) { x1 = xc+r*(int)ptsX[i]; y1=yc+r*(int)ptsY[i]; for(j =i+1; j<nbp; j++) { cg.drawLine( x1, y1, xc+(int)(r*ptsX[j]), yc+(int)(r*ptsY[j])); } } } public void paint(Graphics cg) { cg.clearRect(0,0,2000,2000); tracer_fen(cg); } // Demande fermeture de la fenêtre public void windowClosing( WindowEvent e ) { n_d_fermer++; Point p = new Point(e.getComponent().getLocation()); if( n_d_fermer==2) System.exit(0); else if( n_d_fermer==1) { System.out.println( e.toString() + " essai 1"); System.out.println("Position: " + p.x + ", " + p.y); } } public void windowActivated( WindowEvent e ) { } public void windowClosed( WindowEvent e ) { } public void windowDeactivated( WindowEvent e ) { } public void windowDeiconified( WindowEvent e ) { } public void windowIconified( WindowEvent e ) { } public void windowOpened( WindowEvent e ) { Point p = new Point(e.getComponent().getLocation()); n_d_fermer=0; System.out.println( e.toString() + " fenetre ouverte"); System.out.println("Position: " + p.x + ", " + p.y); } // traitement des évenements ActionEvent public void actionPerformed( ActionEvent e) { Button b = (Button) e.getSource(); Container p = b.getParent(); System.out.println( "source: " + b.getLabel() + "; parent: " + p.getClass()); tourner_points(); this.repaint(); } public static void main(String[] args) { FCercle fen = new FCercle(); fen.setVisible(true); } } // fin FCercle.java