| 1 |
/* |
| 2 |
* DiTAA - Diagrams Through Ascii Art |
| 3 |
* |
| 4 |
* Copyright (C) 2004 Efstathios Sideris |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.stathissideris.ascii2image.graphics; |
| 22 |
|
| 23 |
import java.awt.Image; |
| 24 |
import java.awt.MediaTracker; |
| 25 |
import java.awt.Toolkit; |
| 26 |
import java.net.URL; |
| 27 |
|
| 28 |
import javax.swing.JLabel; |
| 29 |
|
| 30 |
public class ImageHandler { |
| 31 |
|
| 32 |
// private static OffScreenSVGRenderer svgRenderer = |
| 33 |
// new OffScreenSVGRenderer(); |
| 34 |
|
| 35 |
private static ImageHandler instance = new ImageHandler(); |
| 36 |
|
| 37 |
public static ImageHandler instance(){ |
| 38 |
return instance; |
| 39 |
} |
| 40 |
|
| 41 |
private static final MediaTracker tracker = new MediaTracker(new JLabel()); |
| 42 |
|
| 43 |
public Image loadImage(String filename){ |
| 44 |
URL url = ClassLoader.getSystemResource(filename); |
| 45 |
Image result = null; |
| 46 |
if(url != null) |
| 47 |
result = Toolkit.getDefaultToolkit().getImage(url); |
| 48 |
else |
| 49 |
result = Toolkit.getDefaultToolkit().getImage(filename); |
| 50 |
// result = null; |
| 51 |
|
| 52 |
//wait for the image to load before returning |
| 53 |
tracker.addImage(result, 0); |
| 54 |
try { |
| 55 |
tracker.waitForID(0); |
| 56 |
} catch (InterruptedException e) { |
| 57 |
System.err.println("Failed to load image "+filename); |
| 58 |
e.printStackTrace(); |
| 59 |
} |
| 60 |
tracker.removeImage(result, 0); |
| 61 |
|
| 62 |
return result; |
| 63 |
} |
| 64 |
|
| 65 |
// public BufferedImage renderSVG(String filename, int width, int height, boolean stretch) throws IOException { |
| 66 |
// File file = new File(filename); |
| 67 |
// URI uri = file.toURI(); |
| 68 |
// return svgRenderer.renderToImage(uri.toString(), width, height, stretch, null, null); |
| 69 |
// } |
| 70 |
// |
| 71 |
// public BufferedImage renderSVG(String filename, int width, int height, boolean stretch, String idRegex, Color color) throws IOException { |
| 72 |
// File file = new File(filename); |
| 73 |
// URI uri = file.toURI(); |
| 74 |
// return svgRenderer.renderToImage(uri.toString(), width, height, stretch, idRegex, color); |
| 75 |
// } |
| 76 |
|
| 77 |
|
| 78 |
} |