Troisième séance

Désormais, tout le monde devrait avoir les objets graphiques qui s'affichent et que l'on peut Drag & Dropper...

Récupération de code

Pensez à enlever les caractères insécables que votre Emacs semble ne pas comprendre !

Ajoutez à votre classe jdraw.graphicobjects.TextArea l'attribut d'instance et la méthode suivants (pour gérer l'alignement du texte et mettre à jour la variable bounding_box notamment utilisée lors des détections d'objets les plus proches) :
     /** The ascent of this text. */
     private float ascent= 0;

     /**
      * Updates the size of this bounding box area.
      */
     public void updateCachedData()
     {
       super.updateCachedData();

       final BufferedImage      dummy_image= new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB );
       final java.awt.Graphics  g= dummy_image.createGraphics();

       final FontMetrics        font_metrics= g.getFontMetrics( font );
       final LineMetrics        line_metrics= font_metrics.getLineMetrics( text, g );

       g.dispose();
       dummy_image.flush();

       ascent= line_metrics.getAscent();
       bounding_box.width= font_metrics.stringWidth( text );
       bounding_box.height= (int)Math.ceil( ascent ) + (int)Math.ceil( line_metrics.getDescent() );

       bounding_box.x= ((   (alignment == SwingConstants.WEST)
                         || (alignment == SwingConstants.NORTH_WEST)
                         || (alignment == SwingConstants.SOUTH_WEST))
                        ? getX()
                        : ((   (alignment == SwingConstants.EAST)
                            || (alignment == SwingConstants.NORTH_EAST)
                            || (alignment == SwingConstants.SOUTH_EAST))
                           ? getX() - bounding_box.width
                           : getX() - bounding_box.width/2));
       bounding_box.y= ((   (alignment == SwingConstants.NORTH)
                         || (alignment == SwingConstants.NORTH_WEST)
                         || (alignment == SwingConstants.NORTH_EAST))
                        ? getY()
                        : ((   (alignment == SwingConstants.SOUTH)
                            || (alignment == SwingConstants.SOUTH_WEST)
                            || (alignment == SwingConstants.SOUTH_EAST))
                           ? getY() - bounding_box.height
                           : getY() - bounding_box.height/2));
     }

Et simplifiez la méthode paint à :
     /**
      * Paints this text area.
      */
     public void paint( final java.awt.Graphics g )
     {
       g.setColor( getColor() );
       g.setFont( font );
       g.drawString( text, bounding_box.x, (int)(bounding_box.y+ascent) );
     }

Enfin, remplacez votre fichier jdraw/ui/Misc.java par celui-ciLinks to a file to be downloaded : cela ajoute une méthode statique buildIconImage...

Au boulot !

  1. Ajoutez à votre classe jdraw.graphicobjects.GraphicObject les méthodes suivantes :
         /**
          * Returns a dialog box used to set the properties of this object.
          * This dialog box is packed and ready to be set visible.
          */
         public GraphicObjectProperties getPropertyDialog( final Frame owner_frame )
         {
           return new GraphicObjectProperties( owner_frame, this );
         }

         /**
          * Opens a dialog box used to set the properties of this object.
          */
         public void openPropertyDialog( final Frame owner_frame )
         {
           final GraphicObjectProperties object_properties= getPropertyDialog( owner_frame );

           object_properties.pack();
           Misc.centerWindow( object_properties );
           object_properties.setVisible( true );
         }
  2. Comme vous le sentez venir, il vous faut implémenter la boîte de dialogue de base : jdraw.ui.GraphicObjectProperties, dont voici une source de départLinks to a file to be downloaded.
  3. Vous devez aussi faire le nécessaire pour que la méthode openPropertyDialog soit invoquée lorsque l'on clique avec le bouton droit de la souris près d'un objet graphique.


Dernière mise à jour : 02/04/2009 à 23:00
Valid XHTML 1.0!