/Users/lyon/j4p/src/graphics/raytracers/rmiRaytracer/raytracer/ImageCanvas.java
|
1 package graphics.raytracers.rmiRaytracer.raytracer;
2
3 import java.awt.*;
4 import java.awt.image.ColorModel;
5 import java.awt.image.MemoryImageSource;
6
7 class ImageCanvas extends Canvas {
8
9 Image img;
10
11 DoImageClientManager dicm = new DoImageClientManager();
12
13 public ImageCanvas(
14 Dimension band, int size) {
15 DoImageInterface di = dicm.getNextProxy();
16 // DoImageClient.getNextInterface();
17
18 try {
19 computeTheImage(di, size, band);
20 } catch (java.rmi.RemoteException e) {
21 e.printStackTrace();
22 }
23 }
24
25 private void computeTheImage(DoImageInterface di, int size, Dimension band)
26 throws java.rmi.RemoteException {
27 if (di == null) {
28 System.out.println("ERROR:ImageCanvas di==null");
29 return;
30 }
31 di.setSize(new Dimension(size, size));
32 di.setBand(band);
33 di.doTheWork();
34 int pix[][] = di.getSubPixels();
35 img = int2Image(pix);
36 }
37
38 public static Image int2Image(int i[][]) {
39 Toolkit tk = Toolkit.getDefaultToolkit();
40 int width = i.length;
41 int height = i[0].length;
42 int pels[] = new int[width * height];
43 for (int x = 0; x < width; x++)
44 for (int y = 0; y < height; y++)
45 pels[x + y * width] = i[x][y];
46 return tk.createImage(
47 new MemoryImageSource(width, height,
48 ColorModel.getRGBdefault(),
49 pels, 0, width));
50 }
51
52 public ImageCanvas(Image _img) {
53 img = _img;
54 }
55
56 public void paint(Graphics g) {
57 Dimension d = getSize();
58 if (img != null)
59 g.drawImage(img, 0, 0,
60 d.width, d.height, this);
61 }
62
63 public Image getImage() {
64 return img;
65 }
66
67 public void setImage(Image img) {
68 this.img = img;
69 repaint();
70 }
71 }
72