IntegerGraphicsState.java 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/* Copyright (C) 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package gnu.awt.j2d;

import java.awt.Color;
import java.awt.Image;
import java.awt.Shape;
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.awt.image.ColorModel;

/**
 * IntegerGraphicsState is one of several graphics state
 * implementations.  This graphics state is used when the graphics
 * object has simple properties, (coordinate translation only, no
 * transform) and the backend supports integer coordinates (pixel
 * based). For primitive paint operations, this object translates the
 * coordinates and forwards the request to the backend. For requests
 * to draw arbitrary shapes and paths, this object translates the
 * requests to primitive drawing operations supported by the
 * backend. IntegerGraphicsState is meant to support the most common
 * state of an graphics object. The degree of functionality is roughly
 * equivalent with the old java.awt.Graphics API.
 */
public class IntegerGraphicsState extends AbstractGraphicsState
{
  int tx;
  int ty;
  
  DirectRasterGraphics directGfx;
  Shape clip;
  
  public IntegerGraphicsState(DirectRasterGraphics directGfx)
  {
    this.directGfx = directGfx;
  }
  
  public Object clone()
  {
    IntegerGraphicsState clone = (IntegerGraphicsState) super.clone();
    clone.directGfx = (DirectRasterGraphics) directGfx.clone();
    
    return clone;
  }

  public void dispose()
  {
    DirectRasterGraphics lDeviceGfx = directGfx;
    
    directGfx = null;
    
    if (lDeviceGfx != null)
      lDeviceGfx.dispose();
    
    super.dispose();
  }
  
  // -------- Graphics methods:
  
  public void setColor(Color color)
  {
    directGfx.setColor(color);
  }
  
  public void setPaintMode()
  {
    directGfx.setPaintMode();
  }

  public void setXORMode(Color altColor)
  {
    directGfx.setXORMode(altColor);
  }
  
  public void setFont(Font font)
  {
    directGfx.setFont(font);
  }
  
  public FontMetrics getFontMetrics(Font font)
  {
    return directGfx.getFontMetrics(font);
  }

  public void setClip(Shape clip)
  {
    if (clip instanceof Rectangle)
      {
103
	Rectangle clipRect = (Rectangle) ((Rectangle) clip).clone();
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	clipRect.x += tx;
	clipRect.y += ty;
	
	this.clip = clipRect;
	
	directGfx.setClip(clipRect);
	return;
      }
    
    String msg =
      "translating clip shape " + clip + " into device " +
      "coordinate space has not been implemented yet";
    
    throw new UnsupportedOperationException(msg);
  }

  public Shape getClip()
  {
    if (clip instanceof Rectangle)
      {
	Rectangle clipRect = (Rectangle) clip;
	clipRect.x -= tx;
	clipRect.y -= ty;
	return clipRect;
      }

    String msg =
      "translating clip shape " + clip + " into user " +
      "coordinate space has not been implemented yet";
    
    throw new UnsupportedOperationException(msg);
  }

  public Rectangle getClipBounds()
  {
    Rectangle clipRect = clip.getBounds();
    
    clipRect.x -= tx;
    clipRect.y -= ty;
    return clipRect;
  }

  public void copyArea(int x, int y, 
		       int width, int height,
		       int dx, int dy)
  {
    directGfx.copyArea(x+tx, y+ty, width, height, dx, dy);
  }

  public void drawLine(int x1, int y1,
		       int x2, int y2)
  {
    directGfx.drawLine(x1+tx, y1+ty, x2+tx, y2+ty);
  }
    
  public void fillRect(int x, int y,
		       int width, int height)
  {
    directGfx.fillRect(x+tx, y+ty, width, height);
  }
    
  public void clearRect(int x, int y,
			int width, int height)
  {
    directGfx.setColor(frontend.getBackground());
    directGfx.fillRect(x+tx, y+ty, width, height);
    directGfx.setColor(frontend.getColor());
  }

  public void drawRoundRect(int x, int y,
			    int width, int height,
			    int arcWidth, int arcHeight)
  {
    throw new UnsupportedOperationException("not implemented yet");
  }
  
  public void fillRoundRect(int x, int y,
			    int width, int height,
			    int arcWidth, int arcHeight)
  {
    throw new UnsupportedOperationException("not implemented yet");
  }

  public void drawOval(int x, int y,
		       int width, int height)
  {
    throw new UnsupportedOperationException("not implemented yet");
  }
  
  public void fillOval(int x, int y,
		       int width, int height)
  {
    throw new UnsupportedOperationException("not implemented yet");
  }

  public void drawArc(int x, int y,
		      int width, int height,
		      int startAngle, int arcAngle)
  {
    directGfx.drawArc(x+tx, y+ty, width, height, startAngle, arcAngle);
  }    

  public void fillArc(int x, int y,
		      int width, int height,
		      int startAngle, int arcAngle)
  {
    directGfx.fillArc(x+tx, y+ty, width, height, startAngle, arcAngle);
  }
  
  public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
  {
    if ((tx == 0) || (ty == 0))
      {
	directGfx.drawPolyline(xPoints, yPoints, nPoints);
	return;
      }
	    
    throw new UnsupportedOperationException("translate not implemented");
  }

  public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
  {
    if ((tx == 0) || (ty == 0))
      {
	directGfx.drawPolygon(xPoints, yPoints, nPoints);
	return;
      }

    throw new UnsupportedOperationException("translate not implemented");
  }
  
  public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
  {
    if ((tx == 0) || (ty == 0))
      {
	directGfx.fillPolygon(xPoints, yPoints, nPoints);
	return;
      }
    
    throw new UnsupportedOperationException("translate not implemented");
  }

  public boolean drawImage(Image image, int x, int y,
			   ImageObserver observer)
  {
    x += tx;
    y += ty;
    
    if (image instanceof BufferedImage)
      {
	BufferedImage bImage = (BufferedImage) image;
	Object config =
	  bImage.getProperty("java.awt.GraphicsConfiguration");
	
	if (config == frontend.config)
	  return directGfx.drawImage(image, x, y, observer);
	
	int width = image.getWidth(null);
	int height = image.getHeight(null);	
	
	Rectangle bounds = new Rectangle(x, y, width, height);
	
	MappedRaster mr = directGfx.mapRaster(bounds);
	
	// manipulate raster here...
	ColorModel colorModel = mr.getColorModel();
	WritableRaster raster = mr.getRaster();
	
	int xEnd = x + width;
	int yEnd = y + height;
	
	// FIXME: Use the following code only as a fallback. It's SLOW!

	Object rgbElem = null;
	for (int yy=0; yy<height; yy++)
	  {
	    for (int xx=0; xx<width; xx++)
	      {
		int srgb = bImage.getRGB(xx, yy);
		int sa = ((srgb >>> 24) & 0xff) + 1;
		int sr = ((srgb >>> 16) & 0xff) + 1;
		int sg = ((srgb >>> 8) & 0xff) + 1;
		int sb = (srgb & 0xff) + 1;
		
		rgbElem = raster.getDataElements(xx+x, yy+y, rgbElem);
		int drgb = colorModel.getRGB(rgbElem);
		int dr = ((drgb >>> 16) & 0xff) + 1;
		int dg = ((drgb >>> 8) & 0xff) + 1;
		int db = (drgb & 0xff) + 1;		    
		int da = 256 - sa;
		
		dr = ((sr*sa + dr*da) >>> 8) - 1;
		dg = ((sg*sa + dg*da) >>> 8) - 1;
		db = ((sb*sa + db*da) >>> 8) - 1;
		
		drgb = (dr<<16) | (dg<<8) | db;
		
		rgbElem = colorModel.getDataElements(drgb, rgbElem);
		
		raster.setDataElements(xx+x, yy+y, rgbElem);
	      }
	  }
	directGfx.unmapRaster(mr);
	return true;
	
      }
    throw new UnsupportedOperationException("drawing image " + image +
					    "not implemented");
  }
  

  // -------- Graphics2D methods:
  
  public void draw(Shape shape)
  {
    if (shape instanceof Rectangle)
      {
	Rectangle rect = (Rectangle) shape;
	directGfx.drawRect(rect.x+tx, rect.y+ty, rect.width, rect.height);
	return;
      } 
    
    throw new UnsupportedOperationException("shape not implemented");
  }

  public void fill(Shape shape)
  {
    if (shape instanceof Rectangle)
      {
	Rectangle rect = (Rectangle) shape;
	directGfx.fillRect(rect.x+tx, rect.y+ty, rect.width, rect.height);
	return;
      }
    
    throw new UnsupportedOperationException("not implemented");
  }
    
  public boolean hit(Rectangle rect, Shape text,
		     boolean onStroke)
  {
    throw new UnsupportedOperationException("not implemented");
  }

  public void drawString(String text, int x, int y)
  {
    directGfx.drawString(text, x+tx, y+ty);
  }

  public void drawString(String text, float x, float y)
  {
    drawString(text, (int) x, (int) y);
  }
  
  public void translate(int x, int y)
  {
    tx += x;
    ty += y;
  }
    
  public void translate(double tx, double ty)
  {
    if ((tx == 0) && (ty == 0))
      return;
    
    needAffineTransform();
  }

  public void rotate(double theta)
  {
    if (theta == 0)
      return;
    
    needAffineTransform();
  }

  public void rotate(double theta, double x, double y)
  {
    if (theta == 0)
      return;
    
    needAffineTransform();
  }

  public void scale(double scaleX, double scaleY)
  {
    if ((scaleX == 1) && (scaleY == 1))
      return;

    needAffineTransform();
  }
  
  public void shear(double shearX, double shearY)
  {
    if ((shearX == 0) && (shearY == 0))
      return;
    
    needAffineTransform();
  }   
  
  private void needAffineTransform()
  {
    throw new UnsupportedOperationException("state with affine " +
					    "transform not implemented");
  }
}