Source for gnu.java.awt.peer.x.XGraphics2D

   1: /* XGraphics2D.java -- A Java based Graphics2D impl for X
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.java.awt.peer.x;
  39: 
  40: import java.awt.Graphics;
  41: import java.awt.GraphicsConfiguration;
  42: import java.awt.Rectangle;
  43: import java.awt.Shape;
  44: import java.awt.Toolkit;
  45: import java.awt.geom.AffineTransform;
  46: import java.awt.image.ColorModel;
  47: import java.awt.image.Raster;
  48: 
  49: import gnu.java.awt.java2d.AbstractGraphics2D;
  50: import gnu.x11.Drawable;
  51: import gnu.x11.GC;
  52: import gnu.x11.image.ZPixmap;
  53: 
  54: public class XGraphics2D
  55:   extends AbstractGraphics2D
  56: {
  57: 
  58:   /**
  59:    * The X Drawable to draw on.
  60:    */
  61:   private Drawable xdrawable;
  62: 
  63:   /**
  64:    * The X graphics context (GC).
  65:    */
  66:   private GC xgc;
  67: 
  68:   /**
  69:    * Indicates if this graphics has already been disposed.
  70:    */
  71:   private boolean disposed;
  72: 
  73:   XGraphics2D(Drawable d)
  74:   {
  75:     super();
  76:     xdrawable = d;
  77:     xgc = new GC(d);
  78:     init();
  79:     disposed = false;
  80:     //setClip(new Rectangle(0, 0, xdrawable.width, xdrawable.height));
  81:   }
  82: 
  83:   /**
  84:    * Draws a pixel in the target coordinate space using the specified color.
  85:    * 
  86:    * @param x the x coordinate
  87:    * @param y the y coordinate
  88:    */
  89:   protected void rawSetPixel(int x, int y)
  90:   {
  91:     xdrawable.point(xgc, x, y);
  92:   }
  93: 
  94: //  protected void rawFillPolygon(double[] xpoints, double[] ypoints, int npoints)
  95: //  {
  96: //    Point[] points = new Point[npoints];
  97: //    for (int n = 0; n < npoints; n++)
  98: //      {
  99: //        points[n] = new Point((int) xpoints[n], (int) ypoints[n]);
 100: //      }
 101: //    xdrawable.fill_poly(xgc, points, Drawable.COMPLEX, Drawable.ORIGIN);
 102: //    xdrawable.display.flush();
 103: //  }
 104: 
 105:   protected void rawDrawLine(int x0, int y0, int x1, int y1)
 106:   {
 107:     xdrawable.line(xgc, x0, y0, x1, y1);
 108:   }
 109: 
 110:   protected void rawFillRect(int x, int y, int w, int h)
 111:   {
 112:     xdrawable.rectangle(xgc, x, y, w, h, true);
 113:   }
 114: 
 115:   protected void rawSetForeground(java.awt.Color c)
 116:   {
 117:     if (c != null)
 118:       xgc.set_foreground(c.getRGB());
 119:   }
 120: 
 121:   protected void rawSetForeground(int r, int g, int b)
 122:   {
 123:     xgc.set_foreground( r << 16 | g << 8 | b );
 124:   }
 125: 
 126:   /**
 127:    * Returns the color model of this Graphics object.
 128:    *
 129:    * @return the color model of this Graphics object
 130:    */
 131:   protected ColorModel getColorModel()
 132:   {
 133:     return Toolkit.getDefaultToolkit().getColorModel();
 134:   }
 135: 
 136:   /**
 137:    * Returns the color model of the target device.
 138:    *
 139:    * @return the color model of the target device
 140:    */
 141:   protected ColorModel getDestinationColorModel()
 142:   {
 143:     return Toolkit.getDefaultToolkit().getColorModel();
 144:   }
 145: 
 146:   /**
 147:    * Returns the bounds of the target.
 148:    *
 149:    * @return the bounds of the target
 150:    */
 151:   protected Rectangle getDeviceBounds()
 152:   {
 153:     return new Rectangle(0, 0, xdrawable.width, xdrawable.height);
 154:   }
 155: 
 156:   public GraphicsConfiguration getDeviceConfiguration()
 157:   {
 158:     // FIXME: Implement this.
 159:     throw new UnsupportedOperationException("Not yet implemented");
 160:   }
 161: 
 162:   public void dispose()
 163:   {
 164:     if (!disposed)
 165:       {
 166:         xgc.free();
 167:         xdrawable.display.flush();
 168:         disposed = true;
 169:       }
 170:   }
 171: 
 172:   public Graphics create()
 173:   {
 174:     // super.create() returns a copy created by clone(), so it should
 175:     // be a XGraphics2D.
 176:     XGraphics2D copy = (XGraphics2D) super.create();
 177:     copy.xgc = xgc.copy();
 178:     return copy;
 179:   }
 180: 
 181: //  /**
 182: //   * Draws the specified image on the drawable at position (x,y).
 183: //   */
 184: //
 185: //  public boolean drawImage(Image image, int x, int y, ImageObserver observer)
 186: //  {
 187: //    AffineTransform transform = getTransform();
 188: //    int translateX = (int) transform.getTranslateX();
 189: //    int translateY = (int) transform.getTranslateY();
 190: //    if (image instanceof XImage)
 191: //      {
 192: //        XImage xim = (XImage) image;
 193: //        Pixmap pm = xim.pixmap;
 194: //        xdrawable.copy_area(pm, xgc, 0, 0, pm.width, pm.height,
 195: //                            x + translateX, y + translateY);
 196: //      }
 197: //    else if (image instanceof BufferedImage)
 198: //      {
 199: //        BufferedImage bufferedImage = (BufferedImage) image;
 200: //        Raster raster = bufferedImage.getData();
 201: //        int w = bufferedImage.getWidth();
 202: //        int h = bufferedImage.getHeight();
 203: //        // Push data to X server.
 204: //        ZPixmap zPixmap = new ZPixmap(xdrawable.display, w, h,
 205: //                                      xdrawable.display.default_pixmap_format);
 206: //        System.err.println("data buffer length: " + zPixmap.data.length);
 207: //        int[] pixel = new int[4];
 208: //        for (int tx = 0; tx < w; tx++)
 209: //          {
 210: //            for (int ty = 0; ty < h; ty++)
 211: //              {
 212: //                pixel = raster.getPixel(tx, ty, pixel);
 213: ////                System.err.print("r: " + pixel[0]);
 214: ////                System.err.print(", g: " + pixel[1]);
 215: ////                System.err.println(", b: " + pixel[2]);
 216: //                zPixmap.set_red(tx, ty, pixel[0]);
 217: //                zPixmap.set_green(tx, ty, pixel[1]);
 218: //                zPixmap.set_blue(tx, ty, pixel[2]);
 219: //              }
 220: //          }
 221: //        xdrawable.put_image(xgc, zPixmap, x, y);
 222: //      }
 223: //    else
 224: //      {
 225: //        throw new UnsupportedOperationException("Not yet implemented.");
 226: //      }
 227: //    return true;
 228: //  }
 229: //
 230:   public void setClip(Shape c)
 231:   {
 232:     super.setClip(c);
 233:     if (c instanceof Rectangle)
 234:       {
 235:         Rectangle r = (Rectangle) c;
 236:         AffineTransform t = getTransform();
 237:         int translateX = (int) t.getTranslateX();
 238:         //System.err.println("translateX: " + translateX);
 239:         int translateY = (int) t.getTranslateY();
 240:         //System.err.println("translateY: " + translateY);
 241:         //System.err.println("clip: " + c);
 242:         gnu.x11.Rectangle clip = new gnu.x11.Rectangle(r.x, r.y, r.width,
 243:                                                        r.height);
 244:         xgc.set_clip_rectangles(translateX, translateY,
 245:                                 new gnu.x11.Rectangle[]{clip}, GC.UN_SORTED);
 246:       }
 247:   }
 248: 
 249:   /**
 250:    * Notifies the backend that the raster has changed in the specified
 251:    * rectangular area. The raster that is provided in this method is always
 252:    * the same as the one returned in {@link #getDestinationRaster}.
 253:    * Backends that reflect changes to this raster directly don't need to do
 254:    * anything here.
 255:    *
 256:    * @param raster the updated raster, identical to the raster returned
 257:    *        by {@link #getDestinationRaster()}
 258:    * @param x the upper left corner of the updated region, X coordinate
 259:    * @param y the upper lef corner of the updated region, Y coordinate
 260:    * @param w the width of the updated region
 261:    * @param h the height of the updated region
 262:    */
 263:   protected void updateRaster(Raster raster, int x, int y, int w, int h)
 264:   {
 265:     if (w > 0 && h > 0)
 266:       {
 267:         ZPixmap zPixmap = new ZPixmap(xdrawable.display, w, h,
 268:                                       xdrawable.display.default_pixmap_format);
 269:         int[] pixel = null;
 270:         int x1 = x + w;
 271:         int y1 = y + h;
 272:         for (int tx = x; tx < x1; tx++)
 273:           {
 274:             for (int ty = y; ty < y1; ty++)
 275:               {
 276:                 pixel = raster.getPixel(tx, ty, pixel);
 277:                 //System.err.println("tx: " + tx + ", ty: " + ty + ", pixel: " + pixel[0] + ", " + pixel[1] + ", " + pixel[2]);
 278: //              System.err.print("r: " + pixel[0]);
 279: //              System.err.print(", g: " + pixel[1]);
 280: //              System.err.println(", b: " + pixel[2]);
 281:                 zPixmap.set_red(tx - x, ty - y, pixel[0]);
 282:                 zPixmap.set_green(tx - x, ty - y, pixel[1]);
 283:                 zPixmap.set_blue(tx - x, ty - y, pixel[2]);
 284:               }
 285:           }
 286:         xdrawable.put_image(xgc, zPixmap, x, y);
 287:       }
 288:   }
 289: 
 290: 
 291:   protected void init()
 292:   {
 293:     super.init();
 294:   }
 295: }