1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61: public class XGraphics2D
62: extends AbstractGraphics2D
63: {
64:
65:
68: private Drawable xdrawable;
69:
70:
73: private GC xgc;
74:
75:
78: private boolean disposed;
79:
80:
83: private Color foreground;
84:
85: XGraphics2D(Drawable d)
86: {
87: super();
88: xdrawable = d;
89: xgc = new GC(d);
90: init();
91: disposed = false;
92:
93: }
94:
95: protected void rawDrawLine(int x0, int y0, int x1, int y1)
96: {
97: xdrawable.segment(xgc, x0, y0, x1, y1);
98: }
99:
100: protected void rawFillRect(int x, int y, int w, int h)
101: {
102: xdrawable.rectangle(xgc, x, y, w, h, true);
103: }
104:
105:
110: protected ColorModel getColorModel()
111: {
112: return Toolkit.getDefaultToolkit().getColorModel();
113: }
114:
115:
120: protected ColorModel getDestinationColorModel()
121: {
122: return Toolkit.getDefaultToolkit().getColorModel();
123: }
124:
125:
130: protected Rectangle getDeviceBounds()
131: {
132: return new Rectangle(0, 0, xdrawable.width, xdrawable.height);
133: }
134:
135: public GraphicsConfiguration getDeviceConfiguration()
136: {
137:
138: throw new UnsupportedOperationException("Not yet implemented");
139: }
140:
141: public void dispose()
142: {
143: if (!disposed)
144: {
145: xgc.free();
146: xdrawable.display.flush();
147: disposed = true;
148: }
149: }
150:
151: public Graphics create()
152: {
153:
154:
155: XGraphics2D copy = (XGraphics2D) super.create();
156: copy.xgc = xgc.copy();
157: return copy;
158: }
159:
160: public void setClip(Shape c)
161: {
162: super.setClip(c);
163: if (c instanceof Rectangle)
164: {
165: Rectangle r = (Rectangle) c;
166: AffineTransform t = getTransform();
167: int translateX = (int) t.getTranslateX();
168:
169: int translateY = (int) t.getTranslateY();
170:
171:
172: gnu.x11.Rectangle clip = new gnu.x11.Rectangle(r.x, r.y, r.width,
173: r.height);
174: xgc.set_clip_rectangles(translateX, translateY,
175: new gnu.x11.Rectangle[]{clip}, GC.UN_SORTED);
176: }
177: }
178:
179:
193: protected void updateRaster(Raster raster, int x, int y, int w, int h)
194: {
195: if (w > 0 && h > 0)
196: {
197: ZPixmap zPixmap = new ZPixmap(xdrawable.display, w, h,
198: xdrawable.display.default_pixmap_format);
199: int[] pixel = null;
200: int x1 = x + w;
201: int y1 = y + h;
202: for (int tx = x; tx < x1; tx++)
203: {
204: for (int ty = y; ty < y1; ty++)
205: {
206: pixel = raster.getPixel(tx, ty, pixel);
207:
208:
209:
210:
211: zPixmap.set_red(tx - x, ty - y, pixel[0]);
212: zPixmap.set_green(tx - x, ty - y, pixel[1]);
213: zPixmap.set_blue(tx - x, ty - y, pixel[2]);
214: }
215: }
216: xdrawable.put_image(xgc, zPixmap, x, y);
217: }
218: }
219:
220: public void renderScanline(int y, ScanlineCoverage c)
221: {
222: ScanlineCoverage.Iterator iter = c.iterate();
223: float coverageAlpha = 0;
224: int maxCoverage = c.getMaxCoverage();
225: Color old = getColor();
226: Color col = getColor();
227: if (col == null)
228: col = Color.BLACK;
229: while (iter.hasNext())
230: {
231: ScanlineCoverage.Range range = iter.next();
232:
233: coverageAlpha = range.getCoverage();
234: if (coverageAlpha > 0)
235: {
236: int red = col.getRed();
237: int green = col.getGreen();
238: int blue = col.getBlue();
239: if (coverageAlpha < c.getMaxCoverage())
240: {
241: float alpha = coverageAlpha / maxCoverage;
242: red = 255 - (int) ((255 - red) * alpha);
243: green = 255 - (int) ((255 - green) * alpha);
244: blue = 255 - (int) ((255 - blue) * alpha);
245: }
246: xgc.set_foreground(red << 16 | green << 8 | blue);
247: int x0 = range.getXPos();
248: int l = range.getLength();
249: xdrawable.fill_rectangle(xgc, x0, y, l, 1);
250: }
251: }
252: if (old != null)
253: xgc.set_foreground(old.getRGB());
254: }
255:
256: protected void fillScanline(int x0, int x1, int y)
257: {
258: xdrawable.segment(xgc, x0, y, x1, y);
259: }
260:
261: protected void fillScanlineAA(int x0, int x1, int y, int alpha)
262: {
263:
264:
265: Color c = getColor();
266: setColor(new Color(255-alpha, 255-alpha, 255-alpha));
267: xdrawable.segment(xgc, x0, y, x1, y);
268: setColor(c);
269: }
270:
271: protected void init()
272: {
273: super.init();
274: }
275:
276: public void setPaint(Paint p)
277: {
278: super.setPaint(p);
279: if (p instanceof Color)
280: {
281: Color c = (Color) p;
282: XToolkit tk = (XToolkit) Toolkit.getDefaultToolkit();
283: HashMap colorMap = tk.colorMap;
284: gnu.x11.Color col = (gnu.x11.Color) colorMap.get(c);
285: if (col == null)
286: {
287: Colormap map = xdrawable.display.default_colormap;
288: col = map.alloc_color (c.getRed() * 256,
289: c.getGreen() * 256,
290: c.getBlue() * 256);
291: colorMap.put(c, col);
292: }
293: xgc.set_foreground(col);
294: foreground = c;
295: }
296: }
297:
298: protected void fillShape(Shape s, boolean isFont)
299: {
300: synchronized (xdrawable.display) {
301: super.fillShape(s, isFont);
302: }
303: }
304:
305: protected boolean rawDrawImage(Image image, int x, int y, ImageObserver obs)
306: {
307: boolean ret;
308: if (image instanceof XImage)
309: {
310: XImage xImage = (XImage) image;
311: xdrawable.copy_area(xImage.pixmap, xgc, 0, 0, xImage.getWidth(obs),
312: xImage.getHeight(obs), x, y);
313: ret = true;
314: }
315: else if (image instanceof PixmapVolatileImage)
316: {
317: PixmapVolatileImage pvi = (PixmapVolatileImage) image;
318: xdrawable.copy_area(pvi.getPixmap(), xgc, 0, 0, pvi.getWidth(obs),
319: pvi.getHeight(obs), x, y);
320: ret = true;
321: }
322: else
323: {
324: ret = super.rawDrawImage(image, x, y, obs);
325: }
326: return ret;
327: }
328:
329:
330: }