1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package coras.representations;
22
23 import java.awt.Component;
24 import java.awt.image.BufferedImage;
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27
28 import javax.imageio.ImageIO;
29
30 import no.sintef.assetrepository.Representation;
31
32
33
34 /***
35 * @author fvr
36 *
37 * TODO To change the template for this generated type comment go to
38 * Window - Preferences - Java - Code Style - Code Templates
39 */
40 public class BitmapImage extends DiagramRepresentation implements BinaryRepresentation {
41
42 private ImagePanel panel = null;
43 /***
44 * @param representation
45 */
46 protected BitmapImage(Representation representation) {
47 super(representation);
48 }
49
50
51
52
53 public synchronized Component getUIComponent() {
54 if (panel == null) {
55 byte[] imageBytes = getBinaryContent();
56 panel = createUIComponent(imageBytes);
57 }
58 return panel;
59 }
60
61
62
63
64 public byte[] getBinaryContent() {
65 Object content = getContent();
66 if (content == null || content instanceof byte[]) {
67 return (byte[]) content;
68 } else {
69 return null;
70 }
71 }
72
73 public static ImagePanel createUIComponent(byte[] imageBytes) {
74 ImagePanel c = new ImagePanel();
75 try {
76 BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
77 c.setImage(image);
78 } catch (IOException e) {
79
80 e.printStackTrace();
81 }
82 return c;
83 }
84
85 }