ENSDF++ 1.1
An easy, fast and simple way to run querys towards the ENSDF database, written in C++.
EpsGraphics2D.java
Go to the documentation of this file.
00001 /*
00002 Copyright Paul James Mutton, 2001-2004, http://www.jibble.org/
00003 
00004 This file is part of EpsGraphics2D.
00005 
00006 This software is dual-licensed, allowing you to choose between the GNU
00007 General Public License (GPL) and the www.jibble.org Commercial License.
00008 Since the GPL may be too restrictive for use in a proprietary application,
00009 a commercial license is also provided. Full license information can be
00010 found at http://www.jibble.org/licenses/
00011 
00012 $Author: pjm2 $
00013 $Id: EpsGraphics2D.java,v 1.43 2004/07/20 17:42:29 pjm2 Exp $
00014 
00015 */
00016 
00017 import java.awt.*;
00018 import java.awt.image.*;
00019 import java.awt.image.renderable.*;
00020 import java.awt.geom.*;
00021 import java.io.*;
00022 import java.text.*;
00023 import java.awt.font.*;
00024 import java.util.*;
00025 
00026 
00076 public class EpsGraphics2D extends java.awt.Graphics2D {
00077 
00078 
00079     public static final String VERSION = "0.9.0";
00080     
00081     public static final int BLACK_AND_WHITE = 1;
00082     public static final int GRAYSCALE = 2;
00083     public static final int RGB = 3;  // Default
00084 
00085 
00091     public EpsGraphics2D() {
00092         this("Untitled");
00093     }
00094 
00095 
00101     public EpsGraphics2D(String title) {
00102         _document = new EpsDocument(title);
00103         _backgroundColor = Color.white;
00104         _clip = null;
00105         _transform = new AffineTransform();
00106         _clipTransform = new AffineTransform();
00107         _accurateTextMode = true;
00108         _colorDepth = EpsGraphics2D.RGB;
00109         setColor(Color.black);
00110         setPaint(Color.black);
00111         setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
00112         setFont(Font.decode(null));
00113         setStroke(new BasicStroke());
00114     }
00115 
00116 
00125     public EpsGraphics2D(String title, File file, int minX, int minY, int maxX, int maxY) throws IOException {
00126         this(title, new FileOutputStream(file), minX, minY, maxX, maxY);
00127     }
00128 
00129 
00138     public EpsGraphics2D(String title, OutputStream outputStream, int minX, int minY, int maxX, int maxY) throws IOException {
00139         this(title);
00140         _document = new EpsDocument(title, outputStream, minX, minY, maxX, maxY);
00141     }
00142 
00143 
00148     protected EpsGraphics2D(EpsGraphics2D g) {
00149         _document = g._document;
00150         _backgroundColor = g._backgroundColor;
00151         _clip = g._clip;
00152         _clipTransform = (AffineTransform) g._clipTransform.clone();
00153         _transform = (AffineTransform) g._transform.clone();
00154         _color = g._color;
00155         _paint = g._paint;
00156         _composite = g._composite;
00157         _font = g._font;
00158         _stroke = g._stroke;
00159         _accurateTextMode = g._accurateTextMode;
00160         _colorDepth = g._colorDepth;
00161     }
00162 
00163 
00168     private void methodNotSupported() {
00169         EpsException e = new EpsException("Method not currently supported by EpsGraphics2D version " + VERSION);
00170         e.printStackTrace(System.err);
00171     }
00172 
00173 
00175 
00176 
00191     public void setAccurateTextMode(boolean b) {
00192         _accurateTextMode = b;
00193         
00194         if (!getAccurateTextMode()) {
00195             setFont(getFont());
00196         }
00197     }
00198 
00199 
00203     public boolean getAccurateTextMode() {
00204         return _accurateTextMode;
00205     }
00206 
00207 
00213     public void setColorDepth(int c) {
00214         if (c == RGB || c == GRAYSCALE || c == BLACK_AND_WHITE) {
00215             _colorDepth = c;
00216         }
00217     }
00218 
00219 
00224     public int getColorDepth() {
00225         return _colorDepth;
00226     }
00227     
00228 
00233     public void flush() throws IOException {
00234         _document.flush();
00235     }
00236 
00237 
00243     public void close() throws IOException {
00244         flush();
00245         _document.close();
00246     }
00247 
00248 
00252     private void append(String line) {
00253         _document.append(this, line);
00254     }
00255 
00256 
00260     private Point2D transform(float x, float y) {
00261         Point2D result = new Point2D.Float(x, y);
00262         result = _transform.transform(result, result);
00263         result.setLocation(result.getX(), -result.getY());
00264         return result;
00265     }
00266 
00267 
00271     private void draw(Shape s, String action) {
00272 
00273         if (s != null) {
00274 
00275             Rectangle2D userBounds = s.getBounds2D();
00276             if (!_transform.isIdentity()) {
00277                 s = _transform.createTransformedShape(s);
00278             }
00279 
00280             // Update the bounds.
00281             if (!action.equals("clip")) {
00282                 Rectangle2D shapeBounds = s.getBounds2D();
00283                 Rectangle2D visibleBounds = shapeBounds;
00284                 if (_clip != null) {
00285                     Rectangle2D clipBounds = _clip.getBounds2D();
00286                     visibleBounds = shapeBounds.createIntersection(clipBounds);
00287                 }
00288                 float lineRadius = _stroke.getLineWidth() / 2;
00289                 float minX = (float) visibleBounds.getMinX() - lineRadius;
00290                 float minY = (float) visibleBounds.getMinY() - lineRadius;
00291                 float maxX = (float) visibleBounds.getMaxX() + lineRadius;
00292                 float maxY = (float) visibleBounds.getMaxY() + lineRadius;
00293                 _document.updateBounds(minX, -minY);
00294                 _document.updateBounds(maxX, -maxY);
00295             }
00296 
00297             append("newpath");
00298             int type = 0;
00299             float[] coords = new float[6];
00300             PathIterator it = s.getPathIterator(null);
00301             float x0 = 0;
00302             float y0 = 0;
00303             int count = 0;
00304             while (!it.isDone()) {
00305                 type = it.currentSegment(coords);
00306                 float x1 = coords[0];
00307                 float y1 = -coords[1];
00308                 float x2 = coords[2];
00309                 float y2 = -coords[3];
00310                 float x3 = coords[4];
00311                 float y3 = -coords[5];
00312 
00313                 if (type == PathIterator.SEG_CLOSE) {
00314                     append("closepath");
00315                     count++;
00316                 }
00317                 else if (type == PathIterator.SEG_CUBICTO) {
00318                     append(x1 + " " + y1 + " " + x2 + " " + y2 + " " + x3 + " " + y3 + " curveto");
00319                     count++;
00320                     x0 = x3;
00321                     y0 = y3;
00322                 }
00323                 else if (type == PathIterator.SEG_LINETO) {
00324                     append(x1 + " " + y1 + " lineto");
00325                     count++;
00326                     x0 = x1;
00327                     y0 = y1;
00328                 }
00329                 else if (type == PathIterator.SEG_MOVETO) {
00330                     append(x1 + " " + y1 + " moveto");
00331                     count++;
00332                     x0 = x1;
00333                     y0 = y1;
00334                 }
00335                 else if (type == PathIterator.SEG_QUADTO) {
00336                     // Convert the quad curve into a cubic.
00337                     float _x1 = x0 + 2 / 3f * (x1 - x0);
00338                     float _y1 = y0 + 2 / 3f * (y1 - y0);
00339                     float _x2 = x1 + 1 / 3f * (x2 - x1);
00340                     float _y2 = y1 + 1 / 3f * (y2 - y1);
00341                     float _x3 = x2;
00342                     float _y3 = y2;
00343                     append(_x1 + " " + _y1 + " " + _x2 + " " + _y2 + " " + _x3 + " " + _y3 + " curveto");
00344                     count++;
00345                     x0 = _x3;
00346                     y0 = _y3;
00347                 }
00348                 else if (type == PathIterator.WIND_EVEN_ODD) {
00349                     // Ignore.
00350                 }
00351                 else if (type == PathIterator.WIND_NON_ZERO) {
00352                     // Ignore.
00353                 }
00354                 it.next();
00355             }
00356             append(action);
00357             append("newpath");
00358         }
00359     }
00360 
00361 
00365     private String toHexString(int n) {
00366         String result = Integer.toString(n, 16);
00367         while (result.length() < 2) {
00368             result = "0" + result;
00369         }
00370         return result;
00371     }
00372 
00373 
00375 
00376 
00381     public void draw3DRect(int x, int y, int width, int height, boolean raised) {
00382         Color originalColor = getColor();
00383         Stroke originalStroke = getStroke();
00384 
00385         setStroke(new BasicStroke(1.0f));
00386 
00387         if (raised) {
00388             setColor(originalColor.brighter());
00389         }
00390         else {
00391             setColor(originalColor.darker());
00392         }
00393 
00394         drawLine(x, y, x + width, y);
00395         drawLine(x, y, x, y + height);
00396 
00397         if (raised) {
00398             setColor(originalColor.darker());
00399         }
00400         else {
00401             setColor(originalColor.brighter());
00402         }
00403 
00404         drawLine(x + width, y + height, x, y + height);
00405         drawLine(x + width, y + height, x + width, y);
00406 
00407         setColor(originalColor);
00408         setStroke(originalStroke);
00409     }
00410 
00411 
00416     public void fill3DRect(int x, int y, int width, int height, boolean raised) {
00417         Color originalColor = getColor();
00418 
00419         if (raised) {
00420             setColor(originalColor.brighter());
00421         }
00422         else {
00423             setColor(originalColor.darker());
00424         }
00425         draw(new Rectangle(x, y, width, height), "fill");
00426         setColor(originalColor);
00427         draw3DRect(x, y, width, height, raised);
00428     }
00429 
00430 
00434     public void draw(Shape s) {
00435         draw(s, "stroke");
00436     }
00437 
00438 
00442     public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
00443         AffineTransform at = getTransform();
00444         transform(xform);
00445         boolean st = drawImage(img, 0, 0, obs);
00446         setTransform(at);
00447         return st;
00448     }
00449 
00450 
00454     public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
00455         BufferedImage img1 = op.filter(img, null);
00456         drawImage(img1, new AffineTransform(1f, 0f, 0f, 1f, x, y), null);
00457     }
00458 
00459 
00463     public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
00464         Hashtable properties = new Hashtable();
00465         String[] names = img.getPropertyNames();
00466         for (int i = 0; i < names.length; i++) {
00467             properties.put(names[i], img.getProperty(names[i]));
00468         }
00469 
00470         ColorModel cm = img.getColorModel();
00471         WritableRaster wr = img.copyData(null);
00472         BufferedImage img1 = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), properties);
00473         AffineTransform at = AffineTransform.getTranslateInstance(img.getMinX(), img.getMinY());
00474         at.preConcatenate(xform);
00475         drawImage(img1, at, null);
00476     }
00477 
00478 
00482     public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
00483         drawRenderedImage(img.createDefaultRendering(), xform);
00484     }
00485 
00486 
00490     public void drawString(String str, int x, int y) {
00491         drawString(str, (float) x, (float) y);
00492     }
00493 
00494 
00498     public void drawString(String s, float x, float y) {
00499         if (s != null && s.length() > 0) {
00500             AttributedString as = new AttributedString(s);
00501             as.addAttribute(TextAttribute.FONT, getFont());
00502             drawString(as.getIterator(), x, y);
00503         }
00504     }
00505 
00506 
00511     public void drawString(AttributedCharacterIterator iterator, int x, int y) {
00512         drawString(iterator, (float) x, (float) y);
00513     }
00514 
00515 
00520     public void drawString(AttributedCharacterIterator iterator, float x, float y) {
00521         if (getAccurateTextMode()) {
00522             TextLayout layout = new TextLayout(iterator, getFontRenderContext());
00523             Shape shape = layout.getOutline(AffineTransform.getTranslateInstance(x, y));
00524             draw(shape, "fill");
00525         }
00526         else {
00527             append("newpath");
00528             Point2D location = transform(x, y);
00529             append(location.getX() + " " + location.getY() + " moveto");
00530             StringBuffer buffer = new StringBuffer();
00531             for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
00532                 if (ch == '(' || ch == ')') {
00533                     buffer.append('\\');
00534                 }
00535                 buffer.append(ch);
00536             }
00537             append("(" + buffer.toString() + ") show");
00538         }
00539     }
00540 
00541 
00545     public void drawGlyphVector(GlyphVector g, float x, float y) {
00546         Shape shape = g.getOutline(x, y);
00547         draw(shape, "fill");
00548     }
00549 
00550 
00554     public void fill(Shape s) {
00555         draw(s, "fill");
00556     }
00557 
00558 
00563     public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
00564         return s.intersects(rect);
00565     }
00566 
00567 
00572     public GraphicsConfiguration getDeviceConfiguration() {
00573         GraphicsConfiguration gc = null;
00574         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
00575         GraphicsDevice[] gds = ge.getScreenDevices();
00576         for (int i = 0; i < gds.length; i++) {
00577             GraphicsDevice gd = gds[i];
00578             GraphicsConfiguration[] gcs = gd.getConfigurations();
00579             if (gcs.length > 0) {
00580                 return gcs[0];
00581             }
00582         }
00583         return gc;
00584     }
00585 
00586 
00591     public void setComposite(Composite comp) {
00592         _composite = comp;
00593     }
00594 
00595 
00600     public void setPaint(Paint paint) {
00601         _paint = paint;
00602         if (paint instanceof Color) {
00603             setColor((Color) paint);
00604         }
00605     }
00606 
00607 
00612     public void setStroke(Stroke s) {
00613         if (s instanceof BasicStroke) {
00614             _stroke = (BasicStroke) s;
00615 
00616             append(_stroke.getLineWidth() + " setlinewidth");
00617             float miterLimit = _stroke.getMiterLimit();
00618             if (miterLimit < 1.0f) {
00619                 miterLimit = 1;
00620             }
00621             append(miterLimit + " setmiterlimit");
00622             append(_stroke.getLineJoin() + " setlinejoin");
00623             append(_stroke.getEndCap() + " setlinecap");
00624 
00625             StringBuffer dashes = new StringBuffer();
00626             dashes.append("[ ");
00627             float[] dashArray = _stroke.getDashArray();
00628             if (dashArray != null) {
00629                 for (int i = 0; i < dashArray.length; i++) {
00630                     dashes.append((dashArray[i]) + " ");
00631                 }
00632             }
00633             dashes.append("]");
00634             append(dashes.toString() + " 0 setdash");
00635         }
00636     }
00637 
00638 
00642     public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) {
00643         // Do nothing.
00644     }
00645 
00646 
00651     public Object getRenderingHint(RenderingHints.Key hintKey) {
00652         return null;
00653     }
00654 
00655 
00659     public void setRenderingHints(Map hints) {
00660         // Do nothing.
00661     }
00662 
00663 
00667     public void addRenderingHints(Map hints) {
00668         // Do nothing.
00669     }
00670 
00671 
00675     public RenderingHints getRenderingHints() {
00676         return new RenderingHints(null);
00677     }
00678 
00679 
00684     public void translate(int x, int y) {
00685         translate((double) x, (double) y);
00686     }
00687 
00688 
00693     public void translate(double tx, double ty) {
00694         transform(AffineTransform.getTranslateInstance(tx, ty));
00695     }
00696 
00697 
00702     public void rotate(double theta) {
00703         rotate(theta, 0, 0);
00704     }
00705 
00706 
00711     public void rotate(double theta, double x, double y) {
00712         transform(AffineTransform.getRotateInstance(theta, x, y));
00713     }
00714 
00715 
00720     public void scale(double sx, double sy) {
00721         transform(AffineTransform.getScaleInstance(sx, sy));
00722     }
00723 
00724 
00729     public void shear(double shx, double shy) {
00730         transform(AffineTransform.getShearInstance(shx, shy));
00731     }
00732 
00733 
00738     public void transform(AffineTransform Tx) {
00739         _transform.concatenate(Tx);
00740         setTransform(getTransform());
00741     }
00742 
00743 
00747     public void setTransform(AffineTransform Tx) {
00748         if (Tx == null) {
00749             _transform = new AffineTransform();
00750         }
00751         else {
00752             _transform = new AffineTransform(Tx);
00753         }
00754         // Need to update the stroke and font so they know the scale changed
00755         setStroke(getStroke());
00756         setFont(getFont());
00757     }
00758 
00759 
00763     public AffineTransform getTransform() {
00764         return new AffineTransform(_transform);
00765     }
00766 
00767 
00771     public Paint getPaint() {
00772         return _paint;
00773     }
00774 
00775 
00779     public Composite getComposite() {
00780         return _composite;
00781     }
00782 
00783 
00787     public void setBackground(Color color) {
00788         if (color == null) {
00789             color = Color.black;
00790         }
00791         _backgroundColor = color;
00792     }
00793 
00794 
00798     public Color getBackground() {
00799         return _backgroundColor;
00800     }
00801 
00802 
00807     public Stroke getStroke() {
00808         return _stroke;
00809     }
00810 
00811 
00816     public void clip(Shape s) {
00817         if (_clip == null) {
00818             setClip(s);
00819         }
00820         else {
00821             Area area = new Area(_clip);
00822             area.intersect(new Area(s));
00823             setClip(area);
00824         }
00825     }
00826 
00827 
00831     public FontRenderContext getFontRenderContext() {
00832         return _fontRenderContext;
00833     }
00834 
00835 
00837 
00838 
00842     public Graphics create() {
00843         return new EpsGraphics2D(this);
00844     }
00845 
00846 
00852     public Graphics create(int x, int y, int width, int height) {
00853         Graphics g = create();
00854         g.translate(x, y);
00855         g.clipRect(0, 0, width, height);
00856         return g;
00857     }
00858 
00859 
00864     public Color getColor() {
00865         return _color;
00866     }
00867 
00868 
00872     public void setColor(Color c) {
00873         if (c == null) {
00874             c = Color.black;
00875         }
00876         _color = c;
00877         
00878         if (getColorDepth() == BLACK_AND_WHITE) {
00879             float value = 0;
00880             if (c.getRed() + c.getGreen() + c.getBlue() > 255 * 1.5 - 1) {
00881                 value = 1;
00882             }
00883             append(value + " setgray");
00884         }
00885         else if (getColorDepth() == GRAYSCALE) {
00886             float value = ((c.getRed() + c.getGreen() + c.getBlue()) / (3 * 255f));
00887             append(value + " setgray");
00888         }
00889         else {
00890             append((c.getRed() / 255f) + " " + (c.getGreen() / 255f) + " " + (c.getBlue() / 255f) + " setrgbcolor");
00891         }
00892         
00893     }
00894 
00895 
00900     public void setPaintMode() {
00901         // Do nothing - paint mode is the only method supported anyway.
00902     }
00903 
00904 
00908     public void setXORMode(Color c1) {
00909         methodNotSupported();
00910     }
00911 
00912 
00916     public Font getFont() {
00917         return _font;
00918     }
00919 
00920 
00924     public void setFont(Font font) {
00925         if (font == null) {
00926             font = Font.decode(null);
00927         }
00928         _font = font;
00929         if (!getAccurateTextMode()) {
00930             append("/" + _font.getPSName() + " findfont " + ((int) _font.getSize()) + " scalefont setfont");
00931         }
00932     }
00933 
00934 
00938     public FontMetrics getFontMetrics() {
00939         return getFontMetrics(getFont());
00940     }
00941 
00942 
00946     public FontMetrics getFontMetrics(Font f) {
00947         BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
00948         Graphics g = image.getGraphics();
00949         return g.getFontMetrics(f);
00950     }
00951 
00952 
00956     public Rectangle getClipBounds() {
00957         if (_clip == null) {
00958             return null;
00959         }
00960         Rectangle rect = getClip().getBounds();
00961         return rect;
00962     }
00963 
00964 
00968     public void clipRect(int x, int y, int width, int height) {
00969         clip(new Rectangle(x, y, width, height));
00970     }
00971 
00972 
00977     public void setClip(int x, int y, int width, int height) {
00978         setClip(new Rectangle(x, y, width, height));
00979     }
00980 
00981 
00985     public Shape getClip() {
00986         if (_clip == null) {
00987             return null;
00988         }
00989         else {
00990             try {
00991                 AffineTransform t = _transform.createInverse();
00992                 t.concatenate(_clipTransform);
00993                 return t.createTransformedShape(_clip);
00994             }
00995             catch (Exception e) {
00996                 throw new EpsException("Unable to get inverse of matrix: " + _transform);
00997             }
00998         }
00999     }
01000 
01001 
01005     public void setClip(Shape clip) {
01006         if (clip != null) {
01007             if (_document.isClipSet()) {
01008                 append("grestore");
01009                 append("gsave");
01010             }
01011             else {
01012                 _document.setClipSet(true);
01013                 append("gsave");
01014             }
01015             draw(clip, "clip");
01016             _clip = clip;
01017             _clipTransform = (AffineTransform) _transform.clone();
01018         }
01019         else {
01020             if (_document.isClipSet()) {
01021                 append("grestore");
01022                 _document.setClipSet(false);
01023             }
01024             _clip = null;
01025         }
01026     }
01027 
01028 
01032     public void copyArea(int x, int y, int width, int height, int dx, int dy) {
01033         methodNotSupported();
01034     }
01035 
01036 
01040     public void drawLine(int x1, int y1, int x2, int y2) {
01041         Shape shape = new Line2D.Float(x1, y1, x2, y2);
01042         draw(shape);
01043     }
01044 
01045 
01049     public void fillRect(int x, int y, int width, int height) {
01050         Shape shape = new Rectangle(x, y, width, height);
01051         draw(shape, "fill");
01052     }
01053 
01054 
01058     public void drawRect(int x, int y, int width, int height) {
01059         Shape shape = new Rectangle(x, y, width, height);
01060         draw(shape);
01061     }
01062 
01063 
01068     public void clearRect(int x, int y, int width, int height) {
01069         Color originalColor = getColor();
01070 
01071         setColor(getBackground());
01072         Shape shape = new Rectangle(x, y, width, height);
01073         draw(shape, "fill");
01074 
01075         setColor(originalColor);
01076     }
01077 
01078 
01082     public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
01083         Shape shape = new RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight);
01084         draw(shape);
01085     }
01086 
01087 
01091     public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
01092         Shape shape = new RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight);
01093         draw(shape, "fill");
01094     }
01095 
01096 
01100     public void drawOval(int x, int y, int width, int height) {
01101         Shape shape = new Ellipse2D.Float(x, y, width, height);
01102         draw(shape);
01103     }
01104 
01105 
01109     public void fillOval(int x, int y, int width, int height) {
01110         Shape shape = new Ellipse2D.Float(x, y, width, height);
01111         draw(shape, "fill");
01112     }
01113 
01114 
01118     public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
01119         Shape shape = new Arc2D.Float(x, y, width, height, startAngle, arcAngle, Arc2D.OPEN);
01120         draw(shape);
01121     }
01122 
01123 
01127     public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
01128         Shape shape = new Arc2D.Float(x, y, width, height, startAngle, arcAngle, Arc2D.PIE);
01129         draw(shape, "fill");
01130     }
01131 
01132 
01136     public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
01137         if (nPoints > 0) {
01138             GeneralPath path = new GeneralPath();
01139             path.moveTo(xPoints[0], yPoints[0]);
01140             for (int i = 1; i < nPoints; i++) {
01141                 path.lineTo(xPoints[i], yPoints[i]);
01142             }
01143             draw(path);
01144         }
01145     }
01146 
01147 
01151     public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
01152         Shape shape = new Polygon(xPoints, yPoints, nPoints);
01153         draw(shape);
01154     }
01155 
01156 
01160     public void drawPolygon(Polygon p) {
01161         draw(p);
01162     }
01163 
01164 
01168     public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
01169         Shape shape = new Polygon(xPoints, yPoints, nPoints);
01170         draw(shape, "fill");
01171     }
01172 
01173 
01177     public void fillPolygon(Polygon p) {
01178         draw(p, "fill");
01179     }
01180 
01181 
01185     public void drawChars(char[] data, int offset, int length, int x, int y) {
01186         String string = new String(data, offset, length);
01187         drawString(string, x, y);
01188     }
01189 
01190 
01194     public void drawBytes(byte[] data, int offset, int length, int x, int y) {
01195         String string = new String(data, offset, length);
01196         drawString(string, x, y);
01197     }
01198 
01199 
01203     public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
01204         return drawImage(img, x, y, Color.white, observer);
01205     }
01206 
01207 
01211     public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
01212         return drawImage(img, x, y, width, height, Color.white, observer);
01213     }
01214 
01215 
01219     public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
01220         int width = img.getWidth(null);
01221         int height = img.getHeight(null);
01222         return drawImage(img, x, y, width, height, bgcolor, observer);
01223     }
01224 
01225 
01229     public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
01230         return drawImage(img, x, y, x + width, y + height, 0, 0, width, height, bgcolor, observer);
01231     }
01232 
01233 
01237     public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
01238         return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, Color.white, observer);
01239     }
01240 
01241 
01245     public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
01246         if (dx1 >= dx2) {
01247             throw new IllegalArgumentException("dx1 >= dx2");
01248         }
01249         if (sx1 >= sx2) {
01250             throw new IllegalArgumentException("sx1 >= sx2");
01251         }
01252         if (dy1 >= dy2) {
01253             throw new IllegalArgumentException("dy1 >= dy2");
01254         }
01255         if (sy1 >= sy2) {
01256             throw new IllegalArgumentException("sy1 >= sy2");
01257         }
01258 
01259         append("gsave");
01260 
01261         int width = sx2 - sx1;
01262         int height = sy2 - sy1;
01263         int destWidth = dx2 - dx1;
01264         int destHeight = dy2 - dy1;
01265 
01266         int[] pixels = new int[width * height];
01267         PixelGrabber pg = new PixelGrabber(img, sx1, sy1, sx2 - sx1, sy2 - sy1, pixels, 0, width);
01268         try {
01269             pg.grabPixels();
01270         }
01271         catch(InterruptedException e) {
01272             return false;
01273         }
01274 
01275         AffineTransform matrix = new AffineTransform(_transform);
01276         matrix.translate(dx1, dy1);
01277         matrix.scale(destWidth / (double) width, destHeight / (double) height);
01278         double[] m = new double[6];
01279         try {
01280             matrix = matrix.createInverse();
01281         }
01282         catch (Exception e) {
01283             throw new EpsException("Unable to get inverse of matrix: " + matrix);
01284         }
01285         matrix.scale(1, -1);
01286         matrix.getMatrix(m);
01287         String bitsPerSample = "8";
01288         // Not using proper imagemask function yet
01289         //if (getColorDepth() == BLACK_AND_WHITE) {
01290         //    bitsPerSample = "true";
01291         //}
01292         append(width + " " + height + " " + bitsPerSample + " [" + m[0] + " " + m[1] + " " + m[2] + " " +  m[3] + " " + m[4] + " " + m[5] + "]");
01293         // Fill the background to update the bounding box.
01294         Color oldColor = getColor();
01295         setColor(getBackground());
01296         fillRect(dx1, dy1, destWidth, destHeight);
01297         setColor(oldColor);
01298         
01299         if (getColorDepth() == BLACK_AND_WHITE) {
01300             // Should really use imagemask.
01301             append("{currentfile " + width + " string readhexstring pop} bind");
01302             append("image");
01303         }
01304         else if (getColorDepth() == GRAYSCALE) {
01305             append("{currentfile " + width + " string readhexstring pop} bind");
01306             append("image");
01307         }
01308         else {
01309             append("{currentfile 3 " + width + " mul string readhexstring pop} bind");
01310             append("false 3 colorimage");
01311         }
01312         
01313         System.err.println(getColorDepth());
01314         
01315         StringBuffer line = new StringBuffer();
01316         for (int y = 0; y < height; y++) {
01317             for (int x = 0; x < width; x++) {
01318                 Color color = new Color(pixels[x + width * y]);
01319                 
01320                 if (getColorDepth() == BLACK_AND_WHITE) {
01321                     if (color.getRed() + color.getGreen() + color.getBlue() > 255 * 1.5 - 1) {
01322                         line.append("ff");
01323                     }
01324                     else {
01325                         line.append("00");
01326                     }
01327                 }
01328                 else if (getColorDepth() == GRAYSCALE) {
01329                     line.append(toHexString((color.getRed() + color.getGreen() + color.getBlue()) / 3));
01330                 }
01331                 else {
01332                     line.append(toHexString(color.getRed()) + toHexString(color.getGreen()) + toHexString(color.getBlue()));
01333                 }
01334                 
01335                 if (line.length() > 64) {
01336                     append(line.toString());
01337                     line = new StringBuffer();
01338                 }
01339             }
01340         }
01341         if (line.length() > 0) {
01342             append(line.toString());
01343         }
01344 
01345         append("grestore");
01346 
01347         return true;
01348     }
01349 
01350 
01357     public void dispose() {
01358         _document = null;
01359     }
01360 
01361 
01365     public void finalize() {
01366         super.finalize();
01367     }
01368 
01369 
01375     public String toString() {
01376         StringWriter writer = new StringWriter();
01377         try {
01378             _document.write(writer);
01379             _document.flush();
01380             _document.close();
01381         }
01382         catch (IOException e) {
01383             throw new EpsException(e.toString());
01384         }
01385         return writer.toString();
01386     }
01387 
01388 
01393     public boolean hitClip(int x, int y, int width, int height) {
01394         if (_clip == null) {
01395             return true;
01396         }
01397         Rectangle rect = new Rectangle(x, y, width, height);
01398         return hit(rect, _clip, true);
01399     }
01400 
01401 
01405     public Rectangle getClipBounds(Rectangle r) {
01406         if (_clip == null) {
01407             return r;
01408         }
01409         Rectangle rect = getClipBounds();
01410         r.setLocation((int) rect.getX(), (int) rect.getY());
01411         r.setSize((int) rect.getWidth(), (int) rect.getHeight());
01412         return r;
01413     }
01414 
01415 
01416     private Color _color;
01417     private Color _backgroundColor;
01418     private Paint _paint;
01419     private Composite _composite;
01420     private BasicStroke _stroke;
01421     private Font _font;
01422     private Shape _clip;
01423     private AffineTransform _clipTransform;
01424     private AffineTransform _transform;
01425     private boolean _accurateTextMode;
01426     private int _colorDepth;
01427 
01428     private EpsDocument _document;
01429 
01430     private static FontRenderContext _fontRenderContext = new FontRenderContext(null, false, true);
01431 }
 All Classes Files Functions Variables Enumerations Enumerator Defines

Back to the main page of the Precalibrated Ion Beam Identification Detector project

Created by Rikard Lundmark