1 /* Copyright (C) 2000, 2003 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 import java
.awt
.image
.WritableRaster
;
13 import java
.awt
.image
.Raster
;
14 import java
.awt
.image
.DataBuffer
;
15 import java
.awt
.image
.ColorModel
;
16 import java
.awt
.image
.ImageObserver
;
17 import java
.awt
.image
.BufferedImage
;
18 import gnu
.gcj
.xlib
.GC
;
19 import gnu
.gcj
.xlib
.XImage
;
20 import gnu
.gcj
.xlib
.Drawable
;
21 import gnu
.gcj
.xlib
.Window
;
22 import gnu
.gcj
.xlib
.Drawable
;
23 import gnu
.gcj
.xlib
.Visual
;
24 import gnu
.awt
.j2d
.DirectRasterGraphics
;
25 import gnu
.awt
.j2d
.MappedRaster
;
27 public class XGraphics
implements Cloneable
, DirectRasterGraphics
29 static class XRaster
extends MappedRaster
33 public XRaster(WritableRaster raster
, XImage ximage
, ColorModel cm
)
41 XGraphicsConfiguration config
;
51 XGraphics gfxCopy
= (XGraphics
) super.clone();
52 gfxCopy
.context
= context
.create();
56 catch (CloneNotSupportedException ex
)
58 // This should never happen.
59 throw new InternalError ();
65 GC lContext
= context
;
77 public XGraphics(Drawable drawable
, XGraphicsConfiguration config
)
79 context
= GC
.create(drawable
);
83 public void setColor(Color color
)
85 context
.setForeground(config
.getPixel(color
));
88 public void setPaintMode()
90 throw new UnsupportedOperationException("not implemented");
93 public void setXORMode(Color c1
)
95 throw new UnsupportedOperationException("not implemented");
98 public void setFont(Font font
)
100 if ((metrics
!= null) && font
.equals(metrics
.getFont())) return;
102 metrics
= config
.getXFontMetrics(font
);
103 context
.setFont(metrics
.xfont
);
106 public FontMetrics
getFontMetrics(Font font
)
108 if ((metrics
!= null) && font
.equals(metrics
.getFont()))
111 return config
.getXFontMetrics(font
);
114 public void setClip(int x
, int y
, int width
, int height
)
116 Rectangle
[] rects
= { new Rectangle(x
, y
, width
, height
) };
117 context
.setClipRectangles(rects
);
120 public void setClip(Shape clip
)
122 /* TODO: create a special RectangleUnion shape that can be
123 used to draw advantage of the GCs ability to set multiple
127 /* FIXME: creating all these objects is wasteful and can be
128 costly in the long run, since this code is run at every
130 Rectangle newClipBounds
= clip
.getBounds();
132 if ((clipBounds
!= null) && !clipBounds
.contains(newClipBounds
))
134 System
.err
.println("warning: old clip ("+ clipBounds
+") does " +
135 "not fully contain new clip (" +
136 newClipBounds
+ ")");
138 clipBounds
= newClipBounds
;
139 Rectangle
[] rects
= { clipBounds
};
140 context
.setClipRectangles(rects
);
143 public void copyArea(int x
, int y
, int width
, int height
, int
146 throw new UnsupportedOperationException("not implemented");
149 public void drawLine(int x1
, int y1
, int x2
, int y2
)
151 context
.drawLine(x1
, y1
, x2
, y2
);
154 public void drawRect(int x
, int y
, int width
, int height
)
156 throw new UnsupportedOperationException("not implemented yet");
159 public void fillRect(int x
, int y
, int width
, int height
)
161 context
.fillRectangle(x
, y
, width
, height
);
164 public void drawArc(int x
, int y
, int width
, int height
, int
165 startAngle
, int arcAngle
)
167 context
.drawArc (x
, y
, width
, height
, startAngle
, arcAngle
);
170 public void fillArc(int x
, int y
, int width
, int height
, int
171 startAngle
, int arcAngle
)
173 context
.fillArc (x
, y
, width
, height
, startAngle
, arcAngle
);
176 public void drawPolyline(int[] xPoints
, int[] yPoints
, int
179 throw new UnsupportedOperationException("not implemented");
182 public void drawPolygon(int[] xPoints
, int[] yPoints
, int
185 throw new UnsupportedOperationException("not implemented");
188 public void fillPolygon(int[] xPoints
, int[] yPoints
, int nPoints
,
189 int translateX
, int translateY
)
191 context
.fillPolygon(xPoints
, yPoints
, nPoints
, translateX
, translateY
);
194 public void drawString(String str
, int x
, int y
)
196 context
.drawString(str
, x
, y
);
199 public boolean drawImage(Image img
, int x
, int y
,
200 ImageObserver observer
)
202 if (clipBounds
== null)
203 return false; // ***FIXME***
205 if (!(img
instanceof BufferedImage
))
207 throw new AWTError("unknown image class");
210 BufferedImage bimg
= (BufferedImage
) img
;
212 XImage ximg
= (XImage
) bimg
.getProperty("gnu.gcj.xlib.XImage");
215 System
.err
.println("FIXME: skipping null XImage, should " +
216 "really do on the spot conversion");
233 int iLeft
= Math
.max(x
, clipBounds
.x
);
234 int iTop
= Math
.max(y
, clipBounds
.y
);
235 int iRight
= Math
.min(x
+ bimg
.getWidth(),
236 clipBounds
.x
+ clipBounds
.width
);
237 int iBottom
= Math
.min(y
+ bimg
.getHeight(),
238 clipBounds
.y
+ clipBounds
.height
);
240 int srcX
= iLeft
- x
;
243 int width
= iRight
- iLeft
;
244 int height
= iBottom
- iTop
;
246 if ((width
> 0) && (height
> 0))
247 context
.putImage(ximg
, srcX
, srcY
, iLeft
, iTop
, width
, height
);
252 public MappedRaster
mapRaster(Rectangle bounds
)
254 Visual visual
= config
.getVisual();
255 XImage ximage
= new XImage(visual
, bounds
.width
, bounds
.height
,
256 false // do not auto allocate memory
259 WritableRaster raster
=
260 config
.createRasterForXImage(ximage
,
261 new Point(bounds
.x
, bounds
.y
));
263 DataBuffer dataB
= raster
.getDataBuffer();
264 XGraphicsConfiguration
.attachData(ximage
, dataB
, 0);
266 Drawable drawable
= context
.getDrawable();
268 // TODO: restrict to clipping
270 Rectangle mBounds
= drawable
.copyIntoXImage(ximage
, bounds
, 0, 0);
272 return new XRaster(raster
, ximage
, config
.imageCM
);
276 public void unmapRaster(MappedRaster mappedRaster
)
278 XRaster xraster
= (XRaster
) mappedRaster
;
279 XImage ximage
= xraster
.ximage
;
280 Raster raster
= xraster
.getRaster();
281 int x
= raster
.getMinX();
282 int y
= raster
.getMinY();
283 int width
= raster
.getWidth();
284 int height
= raster
.getHeight();
286 context
.putImage(ximage
, 0, 0, x
, y
, width
, height
);