01: package jlib;
02:
03: import jsint.*;
04: import java.awt.*;
05:
06: /**
07: * This class represents a panel which executes closures to handle
08: * events in the Java 1.0 event model.
09: * @author Timothy J. Hickey, tim@cs.brandeis.edu http://www.cs.brandeis.edu/~tim
10: **/
11:
12: public class SchemeCanvas extends Panel {
13: public Image buffer;
14: public Graphics bufferg;
15: public int width, height;
16: int bufferwidth, bufferheight;
17: public Procedure paintHandler;
18: Dimension size;
19:
20: public SchemeCanvas(int w, int h) {
21: super ();
22: width = bufferwidth = (w > 0) ? w : 1;
23: height = bufferheight = (h > 0) ? h : 1;
24: size = new Dimension(w, h);
25: }
26:
27: public Dimension preferredSize() {
28: return size;
29: }
30:
31: public void update(Graphics g) {
32: paint(g);
33: } // don't let Java blank the screen
34:
35: /* if new window needs larger buffer then expand image buffer */
36: void resizeSchemeCanvas(Dimension s) {
37: int w = s.width;
38: int h = s.height;
39: boolean resize = ((w > bufferwidth) || (h > bufferheight));
40: width = w;
41: height = h;
42: bufferwidth = (w < bufferwidth) ? bufferwidth : w;
43: bufferheight = (h < bufferheight) ? bufferheight : h;
44: if (resize) {
45: Image buffer2 = buffer;
46: // Graphics bufferg2 = bufferg;
47: buffer = this .createImage(bufferwidth, bufferheight);
48: bufferg = buffer.getGraphics();
49: bufferg.drawImage(buffer2, 0, 0, this );
50: }
51: }
52:
53: public void paint(Graphics g) {
54: if (buffer == null)
55: buffer = this .createImage(width, height);
56: if (bufferg == null)
57: bufferg = buffer.getGraphics();
58: if (!(size.equals(this .size())))
59: resizeSchemeCanvas(this .size());
60:
61: if (paintHandler != null) {
62: Object x = paintHandler.apply(U.list(bufferg));
63: if (buffer != null)
64: g.drawImage(buffer, 0, 0, this );
65: } else {
66: if (buffer != null)
67: g.drawImage(buffer, 0, 0, this);
68: }
69: }
70:
71: }
|