View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2004 Kevin Rogers
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
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.event.WindowAdapter;
28  import java.awt.event.WindowEvent;
29  import java.io.IOException;
30  import java.io.OutputStream;
31  
32  import javax.swing.JFrame;
33  import javax.swing.WindowConstants;
34  
35  /**
36   * Abstract super class of images in MillScript, intended to abstract the
37   * different types of image. In particular we want to abstract the difference
38   * between raster and vector graphics, so that they can be treated in the same
39   * way.
40   */
41  public abstract class Image implements Cloneable {
42  
43      /**
44       * The frame we will use to display the image, when asked via the
45       * <code>showImage</code> method.
46       */
47      JFrame frame;
48  
49      /**
50       * Flag to indicate when this image is being displayed. This is used to
51       * stop the image being displayed multiple times.
52       */
53      boolean isShown = false;
54  
55      /**
56       * @see java.lang.Object#clone()
57       */
58      @Override
59      protected Object clone() throws CloneNotSupportedException {
60          final Image copy = (Image) super.clone();
61          copy.frame = null;
62          copy.isShown = false;
63          return copy;
64      }
65  
66      /**
67       * Draws a line in the current colour, between the points ( x1, y1 ) and ( x2, y2 ).
68       *
69       * @param x1    the first point's x coordinate
70       * @param y1    the first point's y coordinate
71       * @param x2    the second point's x coordinate
72       * @param y2    the second point's y coordinate
73       */
74      public abstract void drawLine( final int x1, final int y1, final int x2, final int y2 );
75  
76      /**
77       * Draws the outline of an oval in the current color. The oval fits in the
78       * rectangle specified by the starting point ( x, y ) coordinate and the
79       * rectangles <code>width</code> and <code>height</code>.
80       *
81       * @param x the starting point x coordinate
82       * @param y the starting point y coordinate
83       * @param width the width of the rectangle the oval fits in
84       * @param height    the height of the rectangle the oval fits in
85       */
86      public abstract void drawOval( final int x, final int y, final int width, final int height );
87  
88      /**
89       * Draws the outline of a rectangle in the current color. The rectangle is
90       * specified by the starting point ( x, y ) coordinate, the
91       * <code>width</code> and <code>height</code>.
92       *
93       * @param x the starting point x coordinate
94       * @param y the starting point y coordinate
95       * @param width the width of the rectangle
96       * @param height    the height of the rectangle
97       */
98      public abstract void drawRect( final int x, final int y, final int width, final int height );
99  
100     /**
101      * Draws the text using the current font, in the current color, at the
102      * specified position.
103      *
104      * @param s the string to draw
105      * @param x the starting point x coordinate
106      * @param y the starting point y coordinate
107      */
108     public abstract void drawString( final String s, final int x, final int y );
109 
110     /**
111      * Draws a filled oval in the current color. The oval fits in the rectangle
112      * specified by the starting point ( x, y ) coordinate and the rectangles
113      * <code>width</code> and <code>height</code>.
114      *
115      * @param x the starting point x coordinate
116      * @param y the starting point y coordinate
117      * @param width the width of the rectangle the oval fits in
118      * @param height    the height of the rectangle the oval fits in
119      */
120     public abstract void fillOval( final int x, final int y, final int width, final int height );
121 
122     /**
123      * Draws a filled rectangle in the current color. The rectangle is
124      * specified by the starting point ( x, y ) coordinate, the
125      * <code>width</code> and <code>height</code>.
126      *
127      * @param x the starting point x coordinate
128      * @param y the starting point y coordinate
129      * @param width the width of the rectangle
130      * @param height    the height of the rectangle
131      */
132     public abstract void fillRect( final int x, final int y, final int width, final int height );
133 
134     /**
135      * Returns a Graphics2D object to draw on this image.
136      *
137      * @return  a Graphics2D object to draw on this image.
138      */
139     public abstract Graphics2D getGraphics2D();
140 
141     /**
142      * Returns the height of this image, in pixels.
143      *
144      * @return  the height of this image
145      */
146     public abstract int height();
147 
148     /**
149      * Renders this image to the specified stream as a raster image.
150      *
151      * @param ostream   the stream to render to
152      * @param render    the image renderer to use for rasterising the image
153      * @throws IOException  thrown if problems occur with the stream
154      */
155     public abstract void renderAsRasterToStream( final OutputStream ostream, final ImageRender render ) throws IOException;
156 
157     /**
158      * Sets the current color to the specified value.
159      *
160      * @param color the new color to draw in
161      */
162     public abstract void setColor( final Color color );
163 
164     /**
165      * Entry point for showing an image. This contains the basic frame setup
166      * code that is relevant to all image types.
167      */
168     public void showImage() {
169         // Check if the image is already shown
170         if ( this.isShown ) {
171             // It is, so just try to give it focus.
172             this.frame.requestFocusInWindow();
173         } else {
174             // It isn't, so make a new frame.
175             this.frame = new JFrame( "showImage" );
176             // We will handle the close operation
177             this.frame.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
178             // Add a window listener for the close event
179             this.frame.addWindowListener(
180                 new WindowAdapter() {
181                     @Override
182                     public void windowClosing( final WindowEvent e ) {
183                         // Perform any other required actions.
184                         super.windowClosing( e );
185                         // Make the frame invisible
186                         frame.setVisible( false );
187                         // Dispose of the frame
188                         frame.dispose();
189                         // Clear our pointer to the frame, so we can garbage
190                         // collect
191                         frame = null;
192                         // The image is no longer shown
193                         isShown = false;
194                     }
195                 }
196             );
197             // Call the more specific code to display the image in the frame.
198             this.showImageInFrame( frame );
199             // Pack & show
200             this.frame.pack();
201             this.frame.setVisible( true );
202             // The image is now shown
203             this.isShown = true;
204         }
205     }
206 
207     /**
208      * This is the image type specific code that should display the image in
209      * the specified frame.
210      *
211      * @param f the frame to show the image in.
212      */
213     abstract void showImageInFrame( final JFrame f );
214 
215     /**
216      * Returns the width of this image, in pixels.
217      *
218      * @return  the width of this image
219      */
220     public abstract int width();
221 
222 }