package imagetools;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* A singleton instance with our image so that it can be loaded and manipulated once.
* The proxy pattern offers consistancy throughout the project
*/
public class Image{
private BufferedImage image;
private String path;
static volatile Image object;
private int height=-1;
private int width=-1;
protected void Image(){
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public static Image getInstance(){
if(object ==null){
object=new Image();
}
return object;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
setHeight(image.getHeight());
setWidth(image.getWidth());
}
public void delete(){
this.image=null;
}
public void save() {
// TODO save the image
if (path != null) {
if (path.lastIndexOf(".") > -1) {
// the file type string
String filetype = path.substring((path.lastIndexOf(".") + 1));
try {
// write the image to the path
ImageIO.write(image, filetype.toUpperCase().trim(),
new File(path));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("No Path specified");
e.printStackTrace();
}
} else {
try {
throw new FileTypeException();
} catch (FileTypeException e) {
// no file type specified
e.printStackTrace();
}
}
} else {
try {
throw new FileTypeException();
} catch (FileTypeException e) {
// no file type specified
e.printStackTrace();
}
}
}
}