gdkgraphics2d.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,447 行 · 第 1/3 页

JAVA
1,447
字号
    return db.getData();  }  private void updateBufferedImage()  {    if (bimage != null && pixelConversionRequired)      {        int height = bimage.getHeight();        int width = bimage.getWidth();        for (int y = 0; y < height; ++y)          for (int x = 0; x < width; ++x)            bimage.setRGB(x, y, pixelBuffer[y*width+height]);      }  }  private boolean drawImage(Image img, AffineTransform xform,                            Color bgcolor, ImageObserver obs)  {    if (img == null)      return false;    if (img instanceof GtkOffScreenImage        && img.getGraphics() instanceof GdkGraphics2D        && (xform == null || xform.getType() == AffineTransform.TYPE_IDENTITY        || xform.getType() == AffineTransform.TYPE_TRANSLATION))      {	// we are being asked to flush a double buffer from Gdk	GdkGraphics2D g2 = (GdkGraphics2D) img.getGraphics();	gdkDrawDrawable(g2, (int) xform.getTranslateX(),	                (int) xform.getTranslateY());	updateBufferedImage();	return true;      }    else      {	// In this case, xform is an AffineTransform that transforms bounding	// box of the specified image from image space to user space. However	// when we pass this transform to cairo, cairo will use this transform	// to map "user coordinates" to "pixel" coordinates, which is the 	// other way around. Therefore to get the "user -> pixel" transform 	// that cairo wants from "image -> user" transform that we currently	// have, we will need to invert the transformation matrix.	AffineTransform invertedXform = new AffineTransform();	try	  {	    invertedXform = xform.createInverse();	    if (img instanceof BufferedImage)	      {		// draw an image which has actually been loaded 		// into memory fully		BufferedImage b = (BufferedImage) img;		return drawRaster(b.getColorModel(), b.getData(),		                  invertedXform, bgcolor);	      }	    else	      return this.drawImage(GdkPixbufDecoder.createBufferedImage(img	                                                                 .getSource()),	                            xform, bgcolor, obs);	  }	catch (NoninvertibleTransformException e)	  {	    throw new ImagingOpException("Unable to invert transform "	                                 + xform.toString());	  }      }  }  //////////////////////////////////////////////////  ////// Implementation of Graphics2D Methods //////  //////////////////////////////////////////////////  public void draw(Shape s)  {    if (stroke != null && ! (stroke instanceof BasicStroke))      {	fill(stroke.createStrokedShape(s));	return;      }    cairoNewPath();    if (s instanceof Rectangle2D)      {	Rectangle2D r = (Rectangle2D) s;	cairoRectangle(shifted(r.getX(), shiftDrawCalls),	               shifted(r.getY(), shiftDrawCalls), r.getWidth(),	               r.getHeight());      }    else      walkPath(s.getPathIterator(null), shiftDrawCalls);    cairoStroke();    updateBufferedImage();  }  public void fill(Shape s)  {    cairoNewPath();    if (s instanceof Rectangle2D)      {	Rectangle2D r = (Rectangle2D) s;	cairoRectangle(r.getX(), r.getY(), r.getWidth(), r.getHeight());      }    else      walkPath(s.getPathIterator(null), false);    cairoFill();    updateBufferedImage();  }  public void clip(Shape s)  {    // update it    if (clip == null || s == null)      clip = s;    else if (s instanceof Rectangle2D && clip instanceof Rectangle2D)      {	Rectangle2D r = (Rectangle2D) s;	Rectangle2D curr = (Rectangle2D) clip;	clip = curr.createIntersection(r);      }    else      throw new UnsupportedOperationException();    // draw it    if (clip != null)      {	cairoNewPath();	if (clip instanceof Rectangle2D)	  {	    Rectangle2D r = (Rectangle2D) clip;	    cairoRectangle(r.getX(), r.getY(), r.getWidth(), r.getHeight());	  }	else	  walkPath(clip.getPathIterator(null), false);	// cairoClosePath ();	cairoClip();      }  }  public Paint getPaint()  {    return paint;  }  public AffineTransform getTransform()  {    return (AffineTransform) transform.clone();  }  public void setPaint(Paint p)  {    if (paint == null)      return;    paint = p;    if (paint instanceof Color)      {        setColor((Color) paint);      }    else if (paint instanceof TexturePaint)      {	TexturePaint tp = (TexturePaint) paint;	BufferedImage img = tp.getImage();	// map the image to the anchor rectangle  	int width = (int) tp.getAnchorRect().getWidth();	int height = (int) tp.getAnchorRect().getHeight();	double scaleX = width / (double) img.getWidth();	double scaleY = width / (double) img.getHeight();	AffineTransform at = new AffineTransform(scaleX, 0, 0, scaleY, 0, 0);	AffineTransformOp op = new AffineTransformOp(at, getRenderingHints());	BufferedImage texture = op.filter(img, null);	int[] pixels = texture.getRGB(0, 0, width, height, null, 0, width);	setTexturePixels(pixels, width, height, width);      }    else if (paint instanceof GradientPaint)      {	GradientPaint gp = (GradientPaint) paint;	Point2D p1 = gp.getPoint1();	Point2D p2 = gp.getPoint2();	Color c1 = gp.getColor1();	Color c2 = gp.getColor2();	setGradient(p1.getX(), p1.getY(), p2.getX(), p2.getY(), c1.getRed(),	            c1.getGreen(), c1.getBlue(), c1.getAlpha(), c2.getRed(),	            c2.getGreen(), c2.getBlue(), c2.getAlpha(), gp.isCyclic());      }    else      throw new java.lang.UnsupportedOperationException();  }  public void setTransform(AffineTransform tx)  {    transform = tx;    if (transform != null)      {	double[] m = new double[6];	transform.getMatrix(m);	cairoSetMatrix(m);      }  }  public void transform(AffineTransform tx)  {    if (transform == null)      transform = new AffineTransform(tx);    else      transform.concatenate(tx);    setTransform(transform);    if (clip != null)      {	// FIXME: this should actuall try to transform the shape	// rather than degrade to bounds.	Rectangle2D r = clip.getBounds2D();	double[] coords = new double[]	                  {	                    r.getX(), r.getY(), r.getX() + r.getWidth(),	                    r.getY() + r.getHeight()	                  };	try	  {	    tx.createInverse().transform(coords, 0, coords, 0, 2);	    r.setRect(coords[0], coords[1], coords[2] - coords[0],	              coords[3] - coords[1]);	    clip = r;	  }	catch (java.awt.geom.NoninvertibleTransformException e)	  {	  }      }  }  public void rotate(double theta)  {    transform(AffineTransform.getRotateInstance(theta));  }  public void rotate(double theta, double x, double y)  {    transform(AffineTransform.getRotateInstance(theta, x, y));  }  public void scale(double sx, double sy)  {    transform(AffineTransform.getScaleInstance(sx, sy));  }  public void translate(double tx, double ty)  {    transform(AffineTransform.getTranslateInstance(tx, ty));  }  public void translate(int x, int y)  {    translate((double) x, (double) y);  }  public void shear(double shearX, double shearY)  {    transform(AffineTransform.getShearInstance(shearX, shearY));  }  public Stroke getStroke()  {    return stroke;  }  public void setStroke(Stroke st)  {    stroke = st;    if (stroke instanceof BasicStroke)      {	BasicStroke bs = (BasicStroke) stroke;	cairoSetLineCap(bs.getEndCap());	cairoSetLineWidth(bs.getLineWidth());	cairoSetLineJoin(bs.getLineJoin());	cairoSetMiterLimit(bs.getMiterLimit());	float[] dashes = bs.getDashArray();	if (dashes != null)	  {	    double[] double_dashes = new double[dashes.length];	    for (int i = 0; i < dashes.length; i++)	      double_dashes[i] = dashes[i];	    cairoSetDash(double_dashes, double_dashes.length,	                 (double) bs.getDashPhase());	  }      }  }  ////////////////////////////////////////////////  ////// Implementation of Graphics Methods //////  ////////////////////////////////////////////////  public void setPaintMode()  {    setComposite(java.awt.AlphaComposite.SrcOver);  }  public void setXORMode(Color c)  {    setComposite(new gnu.java.awt.BitwiseXORComposite(c));  }  public void setColor(Color c)  {    if (c == null)      c = Color.BLACK;    fg = c;    paint = c;    cairoSetRGBColor(fg.getRed() / 255.0, fg.getGreen() / 255.0,                     fg.getBlue() / 255.0);    cairoSetAlpha((fg.getAlpha() & 255) / 255.0);  }  public Color getColor()  {    return fg;  }  public void clipRect(int x, int y, int width, int height)  {    clip(new Rectangle(x, y, width, height));  }  public Shape getClip()  {    return clip.getBounds2D(); //getClipInDevSpace();  }  public Rectangle getClipBounds()  {    if (clip == null)      return null;    else      return clip.getBounds();  }  protected Rectangle2D getClipInDevSpace()  {    Rectangle2D uclip = clip.getBounds2D();    if (transform == null)      return uclip;    else      {	Point2D pos = transform.transform(new Point2D.Double(uclip.getX(),	                                                     uclip.getY()),	                                  (Point2D) null);	Point2D extent = transform.deltaTransform(new Point2D.Double(uclip	                                                             .getWidth(),	                                                             uclip	                                                             .getHeight()),	                                          (Point2D) null);	return new Rectangle2D.Double(pos.getX(), pos.getY(), extent.getX(),	                              extent.getY());      }  }  public void setClip(int x, int y, int width, int height)  {    setClip(new Rectangle2D.Double((double) x, (double) y, (double) width,                                   (double) height));  }  public void setClip(Shape s)  {    clip = s;    if (s != null)      {	cairoNewPath();	if (s instanceof Rectangle2D)	  {	    Rectangle2D r = (Rectangle2D) s;	    cairoRectangle(r.getX(), r.getY(), r.getWidth(), r.getHeight());	  }	else	  walkPath(s.getPathIterator(null), false);	// cairoClosePath ();	cairoClip();      }  }  private static BasicStroke draw3DRectStroke = new BasicStroke();  public void draw3DRect(int x, int y, int width, int height, boolean raised)  {    Stroke tmp = stroke;    setStroke(draw3DRectStroke);    super.draw3DRect(x, y, width, height, raised);    setStroke(tmp);    updateBufferedImage();  }  public void fill3DRect(int x, int y, int width, int height, boolean raised)  {    Stroke tmp = stroke;    setStroke(draw3DRectStroke);    super.fill3DRect(x, y, width, height, raised);    setStroke(tmp);    updateBufferedImage();  }  public void drawRect(int x, int y, int width, int height)  {    draw(new Rectangle(x, y, width, height));  }  public void fillRect(int x, int y, int width, int height)  {    cairoNewPath();    cairoRectangle(x, y, width, height);    cairoFill();  }  public void clearRect(int x, int y, int width, int height)  {    cairoSetRGBColor(bg.getRed() / 255.0, bg.getGreen() / 255.0,                     bg.getBlue() / 255.0);    cairoSetAlpha(1.0);    cairoNewPath();    cairoRectangle(x, y, width, height);    cairoFill();    setColor(fg);    updateBufferedImage();  }  public void setBackground(Color c)  {    bg = c;  }  public Color getBackground()  {    return bg;  }  private void doPolygon(int[] xPoints, int[] yPoints, int nPoints,                         boolean close, boolean fill)  {    if (nPoints < 1)      return;    GeneralPath gp = new GeneralPath(PathIterator.WIND_EVEN_ODD);    gp.moveTo((float) xPoints[0], (float) yPoints[0]);    for (int i = 1; i < nPoints; i++)      gp.lineTo((float) xPoints[i], (float) yPoints[i]);    if (close)      gp.closePath();    Shape sh = gp;    if (fill == false && stroke != null && ! (stroke instanceof BasicStroke))      {	sh = stroke.createStrokedShape(gp);	fill = true;      }    if (fill)      fill(sh);    else      draw(sh);  }  public void drawLine(int x1, int y1, int x2, int y2)  {    int[] xp = new int[2];    int[] yp = new int[2];    xp[0] = x1;    xp[1] = x2;    yp[0] = y1;    yp[1] = y2;    doPolygon(xp, yp, 2, false, false);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?