1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.millscript.datatypes;
22
23 import org.millscript.millscript.render.ImageRender;
24
25 import java.awt.Color;
26 import java.awt.Graphics2D;
27 import java.awt.image.BufferedImage;
28 import java.io.IOException;
29 import java.io.OutputStream;
30
31 import javax.swing.ImageIcon;
32 import javax.swing.JFrame;
33 import javax.swing.JLabel;
34
35 /**
36 * This class represents a raster image in MillScript.
37 */
38 public final class RasterImage extends Image implements Cloneable {
39
40 /**
41 * The buffered image this raster image is held in.
42 */
43 private BufferedImage bufferedImage;
44
45 /**
46 * The graphics object we can use to draw on this raster image.
47 */
48 private Graphics2D graphics2D = null;
49
50 /**
51 * Constructs a new raster image for the specified buffered image.
52 *
53 * @param image the buffered image to wrap as a raster image
54 */
55 public RasterImage( final BufferedImage image ) {
56 this.bufferedImage = image;
57
58
59
60 this.graphics2D = bufferedImage.createGraphics();
61 }
62
63 /**
64 * @see java.lang.Object#clone()
65 */
66 @Override
67 public Object clone() throws CloneNotSupportedException {
68 final RasterImage copy = (RasterImage) super.clone();
69 copy.bufferedImage = new BufferedImage(
70 this.bufferedImage.getColorModel(),
71 copy.bufferedImage.copyData( null ),
72 false,
73 null
74 );
75 copy.graphics2D = copy.bufferedImage.createGraphics();
76 return copy;
77 }
78
79 /**
80 * @see org.millscript.millscript.datatypes.Image#drawLine(int, int, int, int)
81 */
82 @Override
83 public void drawLine( final int x1, final int y1, final int x2, final int y2 ) {
84 this.graphics2D.drawLine( x1, y1, x2, y2 );
85 }
86
87 /**
88 * @see org.millscript.millscript.datatypes.Image#drawOval(int, int, int, int)
89 */
90 @Override
91 public void drawOval( final int x, final int y, final int width, final int height ) {
92 this.graphics2D.drawOval( x, y, width, height );
93 }
94
95 /**
96 * @see org.millscript.millscript.datatypes.Image#drawRect(int, int, int, int)
97 */
98 @Override
99 public void drawRect( final int x, final int y, final int width, final int height ) {
100 this.graphics2D.drawRect( x, y, width, height );
101 }
102
103 /**
104 * @see org.millscript.millscript.datatypes.Image#drawString(java.lang.String, int, int)
105 */
106 @Override
107 public void drawString( final String str, final int x, final int y ) {
108 this.graphics2D.drawString( str, x, y );
109 }
110
111 /**
112 * @see org.millscript.millscript.datatypes.Image#fillOval(int, int, int, int)
113 */
114 @Override
115 public void fillOval( final int x, final int y, final int width, final int height ) {
116 this.graphics2D.fillOval( x, y, width, height );
117 }
118
119 /**
120 * @see org.millscript.millscript.datatypes.Image#fillRect(int, int, int, int)
121 */
122 @Override
123 public void fillRect( final int x, final int y, final int width, final int height ) {
124 this.graphics2D.fillRect( x, y, width, height );
125 }
126
127 /**
128 * @see org.millscript.millscript.datatypes.Image#getGraphics2D()
129 */
130 @Override
131 public Graphics2D getGraphics2D() {
132 return graphics2D;
133 }
134
135 /**
136 * @see org.millscript.millscript.datatypes.Image#height()
137 */
138 @Override
139 public int height() {
140 return this.bufferedImage.getHeight();
141 }
142
143 /**
144 * @see org.millscript.millscript.datatypes.Image#renderAsRasterToStream(java.io.OutputStream, org.millscript.millscript.render.ImageRender)
145 */
146 @Override
147 public void renderAsRasterToStream( final OutputStream ostream, final ImageRender render ) throws IOException {
148 render.write( this.bufferedImage, ostream );
149 }
150
151 /**
152 * @see org.millscript.millscript.datatypes.Image#setColor(java.awt.Color)
153 */
154 @Override
155 public void setColor( final Color color ) {
156 this.graphics2D.setColor( color );
157 }
158
159 /**
160 * @see org.millscript.millscript.datatypes.Image#showImageInFrame(javax.swing.JFrame)
161 */
162 @Override
163 void showImageInFrame( final JFrame f ) {
164 final ImageIcon icon = new ImageIcon( bufferedImage );
165 final JLabel label = new JLabel( icon );
166 f.getContentPane().add( label );
167 }
168
169 /**
170 * Returns the string representation of this raster image.
171 *
172 * @return a String holding a representation of this raster image
173 */
174 @Override
175 public String toString() {
176 return "<image:raster>";
177 }
178
179 /**
180 * @see org.millscript.millscript.datatypes.Image#width()
181 */
182 @Override
183 public int width() {
184 return this.bufferedImage.getWidth();
185 }
186
187 }