Source for gnu.gcj.xlib.GC

   1: /* Copyright (C) 2000, 2003  Free Software Foundation
   2: 
   3:    This file is part of libgcj.
   4: 
   5: This software is copyrighted work licensed under the terms of the
   6: Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   7: details.  */
   8: 
   9: package gnu.gcj.xlib;
  10: 
  11: import gnu.gcj.RawData;
  12: import java.awt.Rectangle;
  13: 
  14: /**
  15:  * An X11 graphics context.  Unlike a traditional X11 graphics
  16:  * context, the target drawable is part of the GC state.
  17:  *
  18:  * Implementation notes: There is no need to do coalescing of changes
  19:  * since Xlib will do this for us.  The implementation relies on the
  20:  * Xlib GC cache and will not try to be clever.
  21:  *
  22:  * @author Rolf W. Rasmussen <rolfwr@ii.uib.no>
  23:  */
  24: public class GC implements Cloneable
  25: {
  26:   /** Protected constructor, because GC.create(target) should be used instead.
  27:    */
  28:   protected GC(Drawable target)
  29:   {
  30:     this.target = target;
  31:     initStructure(null);
  32:   }
  33: 
  34:   /** Try to get a suitable GC from the drawable's cache.
  35:    * If there isn't one, create one.
  36:    */
  37:   public Object clone()
  38:   {
  39:     try
  40:       {
  41:     GC gcClone = target.getGCFromCache ();
  42:     if (gcClone==null)
  43:       {
  44:         gcClone = (GC) super.clone();
  45:         gcClone.structure = null;
  46:       }
  47:     gcClone.initStructure(this);
  48:     gcClone.updateClip(clipRectangles);
  49:     return gcClone;
  50:       } 
  51:     catch (CloneNotSupportedException ex)
  52:       {
  53:     // This should never happen.
  54:     throw new InternalError ();
  55:       }
  56:   }
  57: 
  58:   private native void initStructure(GC copyFrom);
  59: 
  60:   public GC create()
  61:   {
  62:     return (GC) clone();
  63:   }
  64:   
  65:   /** Create a GC, or if one is already cached for target, return that.
  66:    * @param target The Drawable for which a GC is needed
  67:    * @return The new or retrieved GC
  68:    */
  69:   static public GC create (Drawable target)
  70:   {
  71:     GC returnValue = target.getGCFromCache ();
  72:     if (returnValue == null)
  73:       returnValue = new GC (target);
  74:     return returnValue;
  75:   }
  76: 
  77:   public void finalize()
  78:   {
  79:     disposeImpl();
  80:   }
  81: 
  82:   /** Save this GC in the drawable's cache.
  83:    *  The "real" dispose (disposeImpl) is called when the
  84:    *  drawable is finialized, to free X server resources.
  85:    */
  86:   public void dispose()
  87:   {
  88:     target.putGCInCache (this);
  89:   }
  90: 
  91:   public synchronized native void disposeImpl();
  92: 
  93:   public native void setForeground(long pixel);
  94:   public native void setFont(gnu.gcj.xlib.Font font);
  95: 
  96:   /**
  97:    * Set the clip region for the graphics operations performed by the
  98:    * GC.
  99:    *
 100:    * This is one of the few costly operations of this class.  It is
 101:    * suggested that the clip is only set or changed if really
 102:    * necessary.  Higher level APIs can make such optimizations
 103:    * transparent.
 104:    *
 105:    * @param rectangles the union of these rectangles describe the clip
 106:    * region.
 107:    */
 108:   public void setClipRectangles(Rectangle[] rectangles)
 109:   {
 110:     clipRectangles = rectangles;
 111:     updateClip(clipRectangles);
 112:   }
 113: 
 114:   public native void drawString(String text, int x, int y);
 115:   public native void drawLine(int x1, int y1, int x2, int y2);
 116:   public native void drawRectangle(int x, int y, int w, int h);
 117: 
 118:   public native void fillRectangle(int x, int y, int w, int h);
 119:   public native void fillPolygon(int[] xPoints, int[] yPoints, int nPoints,
 120:                  int translateX, int translateY);
 121:   
 122:   public native void drawArc(int x, int y, int w, int h,
 123:                  int startAngle, int arcAngle);
 124:   public native void fillArc(int x, int y, int w, int h,
 125:                  int startAngle, int arcAngle);
 126: 
 127:   /** 
 128:    * 
 129:    * Clear area using the background pixel or pixmap of the drawable.
 130:    * Note that this operation does not adhere to the current clip.
 131:    */
 132:   public native void clearArea(int x, int y, int w, int h,
 133:                    boolean exposures);
 134: 
 135:   /** Draw a point using the current foreground color
 136:    * @param x The x coordinate at which to draw
 137:    * @param t The y coordinate at which to draw
 138:    */
 139:   public native void drawPoint (int x, int y);
 140: 
 141:   public native void putImage(XImage image,
 142:                   int srcX, int srcY,
 143:                   int destX, int destY,
 144:                   int width, int height);
 145: 
 146:   public native void copyArea (Drawable source,
 147:                                int srcX, int srcY,
 148:                                int destX, int destY,
 149:                                int width, int height);
 150:   
 151:   public Drawable getDrawable()
 152:   {
 153:     return target;
 154:   }
 155: 
 156:   private native void updateClip(Rectangle[] rectangles);
 157: 
 158:   private Drawable target;
 159:   private RawData structure;
 160:   private Rectangle[] clipRectangles;
 161: }