CHANGED THINGS RELATING TO RESIZING

master
marcus13345 2014-10-10 14:11:17 -04:00
parent 260cf8bcd0
commit dbd91b52ed
2 changed files with 94 additions and 52 deletions

View File

@ -9,19 +9,20 @@ import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import MAndEngine.Engine;
import MAndEngine.ImageCreator; import MAndEngine.ImageCreator;
public class Item { public class Item {
private static int scaleToHeight; private static int scaleToHeight;
//cropped to a thumb nail, of size 80*80 // cropped to a thumb nail, of size 80*80
private final BufferedImage thumbnail; private final BufferedImage thumbnail;
//the original image, not cropped or anything. // the original image, not cropped or anything.
private final BufferedImage image; private final BufferedImage[] image;
//Absolute path, i think. // Absolute path, i think.
private final String path; private final String path;
private String name; private String name;
@ -38,20 +39,24 @@ public class Item {
public Item(String path) { public Item(String path) {
BufferedImage thumbnail = null; BufferedImage thumbnail = null;
BufferedImage image = null; BufferedImage[] images = null;
try { try {
File file = new File(path); File file = new File(path);
if (file.isDirectory()) { if (file.isDirectory()) {
thumbnail = ImageCreator.creatImageWithStripes(Viewer.THUMBNAIL_SIZE, Viewer.THUMBNAIL_SIZE, Color.BLUE); thumbnail = ImageCreator.creatImageWithStripes(Viewer.THUMBNAIL_SIZE, Viewer.THUMBNAIL_SIZE, Color.BLUE);
name = file.getName();
} else {
// try and do the image thing! name = file.getName();
image = ImageIO.read(file);
thumbnail = (getScaledImage(image, 80, 80)); } else {
if (!path.endsWith(".gif")) {
// try and do the image thing!
images = new BufferedImage[] { ImageIO.read(file) };
} else {
//images = ImageIO.
seemsLegit = false;
}
thumbnail = (getScaledImage(images[0], 80, 80));
path = file.getAbsolutePath(); path = file.getAbsolutePath();
name = ""; name = "";
@ -61,49 +66,62 @@ public class Item {
System.out.println("wat: " + e.getMessage()); System.out.println("wat: " + e.getMessage());
System.out.println("wat was in " + path); System.out.println("wat was in " + path);
} }
this.image = image; this.image = images;
this.path = path; this.path = path;
this.thumbnail = thumbnail; this.thumbnail = thumbnail;
} }
public boolean getSeemsLegit() { public boolean getSeemsLegit() {
return seemsLegit; return seemsLegit;
} }
private static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException { private static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
int imageWidth = image.getWidth(); int imageWidth = image.getWidth();
int imageHeight = image.getHeight(); int imageHeight = image.getHeight();
double scaleY = (double) height / imageHeight; double scaleY = (double) height / imageHeight;
double scaleX = (double) width / imageWidth ; double scaleX = (double) width / imageWidth;
//fill or fit bit // fill or fit bit
if(scaleX > scaleY) scaleX = scaleY; if (scaleX < scaleY)
else scaleY = scaleX; scaleX = scaleY;
else
//give us the transform object thing scaleY = scaleX;
// give us the transform object thing
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY); AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
//then make the scaling algorithm thing. // then make the scaling algorithm thing.
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR); AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
//out new image that we need to crop onto the buffer with the right dimensions. // out new image that we need to crop onto the buffer with the right
BufferedImage newImage = bilinearScaleOp.filter(image, new BufferedImage((int) (imageWidth * scaleX), (int) (imageWidth * scaleY), image.getType())); // dimensions.
BufferedImage newImage = bilinearScaleOp.filter(image, new BufferedImage((int) (imageWidth * scaleX), (int) (imageHeight * scaleY), image.getType()));
//make the buffer // Image newImage = image.getScaledInstance((int) (imageWidth * scaleX),
// (int) (imageWidth * scaleY), Image.SCALE_SMOOTH);
// make the buffer
BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = buffer.getGraphics(); Graphics g = buffer.getGraphics();
//do math, shove it on. int newImageWidth = newImage.getWidth(null);
g.drawImage(newImage, (width / 2) + ((width - imageWidth) / 2), (height / 2) + ((height - imageHeight) / 2), null); int newImageHeight = newImage.getHeight(null);
//return dat Engine.log("original: " + imageWidth + " x " + imageHeight);
Engine.log("new: " + width + " x " + height);
Engine.log("after: " + newImageWidth + " x " + newImageHeight);
// do math, shove it on.
g.drawImage(newImage, (width - newImageWidth) / 2, (height - newImageHeight) / 2, null);
// return dat
return buffer; return buffer;
} }
public BufferedImage getImage() { public BufferedImage getImage() {
return image; return image[0];
} }
public BufferedImage getThumbnail() { public BufferedImage getThumbnail() {

View File

@ -41,16 +41,16 @@ public class Viewer implements BasicApp {
private static File currentDirectoryFile; private static File currentDirectoryFile;
private static Variable currentDirectoryVariable; private static Variable currentDirectoryVariable;
public static final int THUMB_MARGIN = 30; public static int THUMB_MARGIN = 30;
public static final int FULL_WIDTH = THUMBNAIL_SIZE + THUMB_MARGIN; public static int FULL_WIDTH = THUMBNAIL_SIZE + THUMB_MARGIN;
public static final int Y_OFFSET = 470; public static int Y_OFFSET = 470;
public static final int X_OFFSET_SELECTION = (int) (THUMB_MARGIN + (selection * FULL_WIDTH) - (scroll * FULL_WIDTH)); public static int X_OFFSET_SELECTION = (int) (THUMB_MARGIN + (selection * FULL_WIDTH) - (scroll * FULL_WIDTH));
public static final int LEFT_BAR_WIDTH = FULL_WIDTH + THUMB_MARGIN; public static int LEFT_BAR_WIDTH = FULL_WIDTH + THUMB_MARGIN;
public static final int INNER_FRAME_HEIGHT = (int) (((WIDTH - LEFT_BAR_WIDTH) / 4d) * 3d); public static int INNER_FRAME_HEIGHT = (int) (((WIDTH - LEFT_BAR_WIDTH) / 4d) * 3d);
public static final int TOP_BAR_HEIGHT = HEIGHT - INNER_FRAME_HEIGHT; public static int TOP_BAR_HEIGHT = HEIGHT - INNER_FRAME_HEIGHT;
public static final int INNER_FRAME_WIDTH = WIDTH - LEFT_BAR_WIDTH; public static int INNER_FRAME_WIDTH = WIDTH - LEFT_BAR_WIDTH;
private static Thread populationThread; private static Thread populationThread;
@ -201,7 +201,6 @@ public class Viewer implements BasicApp {
g.drawImage(items.get(i).getThumbnail(), THUMB_MARGIN, -1 + (int) (THUMB_MARGIN + (i * FULL_WIDTH) - (scroll * FULL_WIDTH)), null); g.drawImage(items.get(i).getThumbnail(), THUMB_MARGIN, -1 + (int) (THUMB_MARGIN + (i * FULL_WIDTH) - (scroll * FULL_WIDTH)), null);
/* /*
* g.setColor(Color.WHITE); g.drawRect(THUMB_MARGIN - 2, Y_OFFSET - * g.setColor(Color.WHITE); g.drawRect(THUMB_MARGIN - 2, Y_OFFSET -
* 2, THUMB_WIDTH + 3, THUMB_WIDTH + 3); * 2, THUMB_WIDTH + 3, THUMB_WIDTH + 3);
@ -260,9 +259,15 @@ public class Viewer implements BasicApp {
} }
} }
/**
* gets called every tick because of scaling, we dont particularly know the res...
* @param image
* @param width
* @param height
* @return
* @throws IOException
*/
private static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException { private static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
int imageWidth = image.getWidth(); int imageWidth = image.getWidth();
@ -298,7 +303,7 @@ public class Viewer implements BasicApp {
Engine.log("after: " + newImageWidth + " x " + newImageHeight); Engine.log("after: " + newImageWidth + " x " + newImageHeight);
//do math, shove it on. //do math, shove it on.
g.drawImage(newImage, 0, 0, null); g.drawImage(newImage, (width - newImageWidth) / 2, (height - newImageHeight) / 2, null);
//return dat //return dat
return buffer; return buffer;
@ -334,4 +339,23 @@ public class Viewer implements BasicApp {
return true; return true;
} }
@Override
public void resized(int width, int height) {
WIDTH = width;
HEIGHT = height;
THUMB_MARGIN = 30;
FULL_WIDTH = THUMBNAIL_SIZE + THUMB_MARGIN;
Y_OFFSET = 470;
X_OFFSET_SELECTION = (int) (THUMB_MARGIN + (selection * FULL_WIDTH) - (scroll * FULL_WIDTH));
LEFT_BAR_WIDTH = FULL_WIDTH + THUMB_MARGIN;
TOP_BAR_HEIGHT = 50;
INNER_FRAME_HEIGHT = HEIGHT - TOP_BAR_HEIGHT;
INNER_FRAME_WIDTH = WIDTH - LEFT_BAR_WIDTH;
}
} }