MAndViewer/src/Item.java

120 lines
3.1 KiB
Java
Raw Normal View History

2014-10-07 21:04:58 -04:00
import java.awt.Color;
2014-10-08 22:40:09 -04:00
import java.awt.Font;
2014-10-07 21:04:58 -04:00
import java.awt.Graphics;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
2014-10-08 22:40:09 -04:00
import MAndEngine.ImageCreator;
2014-10-07 21:04:58 -04:00
public class Item {
private static int scaleToHeight;
2014-10-08 22:40:09 -04:00
//cropped to a thumb nail, of size 80*80
2014-10-07 21:04:58 -04:00
private final BufferedImage thumbnail;
2014-10-08 22:40:09 -04:00
//the original image, not cropped or anything.
2014-10-07 21:04:58 -04:00
private final BufferedImage image;
2014-10-08 22:40:09 -04:00
//Absolute path, i think.
2014-10-07 21:04:58 -04:00
private final String path;
private String name;
// we need this because im scared.
// if literally anything in the creating of an item
// goes MODERATELY wrong, you set this to FALSE.
// then we don't try and render something that
// makes no sense.
// set this to false by the end of the constructor
2014-10-08 22:40:09 -04:00
// and when it comes back to be added to the list,
// it'll get thrown out.
2014-10-07 21:04:58 -04:00
private boolean seemsLegit = true;
public Item(String path) {
BufferedImage thumbnail = null;
BufferedImage image = null;
try {
File file = new File(path);
if (file.isDirectory()) {
2014-10-08 22:40:09 -04:00
thumbnail = ImageCreator.creatImageWithStripes(Viewer.THUMBNAIL_SIZE, Viewer.THUMBNAIL_SIZE, Color.BLUE);
2014-10-07 21:04:58 -04:00
name = file.getName();
} else {
// try and do the image thing!
image = ImageIO.read(file);
2014-10-08 22:40:09 -04:00
thumbnail = (getScaledImage(image, 80, 80));
2014-10-07 21:04:58 -04:00
path = file.getAbsolutePath();
name = "";
}
} catch (Exception e) {
seemsLegit = false;
2014-10-08 22:40:09 -04:00
System.out.println("wat: " + e.getMessage());
System.out.println("wat was in " + path);
2014-10-07 21:04:58 -04:00
}
this.image = image;
this.path = path;
this.thumbnail = thumbnail;
}
public boolean getSeemsLegit() {
return seemsLegit;
}
2014-10-08 22:40:09 -04:00
private static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
2014-10-07 21:04:58 -04:00
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
2014-10-08 22:40:09 -04:00
2014-10-07 21:04:58 -04:00
double scaleY = (double) height / imageHeight;
2014-10-08 22:40:09 -04:00
double scaleX = (double) width / imageWidth ;
//fill or fit bit
if(scaleX > scaleY) scaleX = scaleY;
else scaleY = scaleX;
//give us the transform object thing
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
//then make the scaling algorithm thing.
2014-10-07 21:04:58 -04:00
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
2014-10-08 22:40:09 -04:00
//out new image that we need to crop onto the buffer with the right dimensions.
BufferedImage newImage = bilinearScaleOp.filter(image, new BufferedImage((int) (imageWidth * scaleX), (int) (imageWidth * scaleY), image.getType()));
//make the buffer
BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = buffer.getGraphics();
//do math, shove it on.
g.drawImage(newImage, (width / 2) + ((width - imageWidth) / 2), (height / 2) + ((height - imageHeight) / 2), null);
//return dat
return buffer;
2014-10-07 21:04:58 -04:00
}
public BufferedImage getImage() {
return image;
}
public BufferedImage getThumbnail() {
return thumbnail;
}
public String getPath() {
return path;
}
public String getName() {
return name;
}
}