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 MAndEngine.Engine;
import MAndEngine.ImageCreator;
public class Item {
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;
//the original image, not cropped or anything.
private final BufferedImage image;
//Absolute path, i think.
// the original image, not cropped or anything.
private final BufferedImage[] image;
// Absolute path, i think.
private final String path;
private String name;
@ -38,20 +39,24 @@ public class Item {
public Item(String path) {
BufferedImage thumbnail = null;
BufferedImage image = null;
BufferedImage[] images = null;
try {
File file = new File(path);
if (file.isDirectory()) {
thumbnail = ImageCreator.creatImageWithStripes(Viewer.THUMBNAIL_SIZE, Viewer.THUMBNAIL_SIZE, Color.BLUE);
name = file.getName();
} else {
// try and do the image thing!
image = ImageIO.read(file);
thumbnail = (getScaledImage(image, 80, 80));
name = file.getName();
} 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();
name = "";
@ -61,49 +66,62 @@ public class Item {
System.out.println("wat: " + e.getMessage());
System.out.println("wat was in " + path);
}
this.image = image;
this.image = images;
this.path = path;
this.thumbnail = thumbnail;
}
public boolean getSeemsLegit() {
return seemsLegit;
}
private static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
double scaleY = (double) height / imageHeight;
double scaleX = (double) width / imageWidth ;
//fill or fit bit
if(scaleX > scaleY) scaleX = scaleY;
else scaleY = scaleX;
//give us the transform object thing
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.
// then make the scaling algorithm thing.
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
//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
// 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) (imageHeight * scaleY), image.getType()));
// 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);
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
int newImageWidth = newImage.getWidth(null);
int newImageHeight = newImage.getHeight(null);
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;
}
public BufferedImage getImage() {
return image;
return image[0];
}
public BufferedImage getThumbnail() {

View File

@ -41,16 +41,16 @@ public class Viewer implements BasicApp {
private static File currentDirectoryFile;
private static Variable currentDirectoryVariable;
public static final int THUMB_MARGIN = 30;
public static final int FULL_WIDTH = THUMBNAIL_SIZE + THUMB_MARGIN;
public static final int Y_OFFSET = 470;
public static int THUMB_MARGIN = 30;
public static int FULL_WIDTH = THUMBNAIL_SIZE + THUMB_MARGIN;
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 final int INNER_FRAME_HEIGHT = (int) (((WIDTH - LEFT_BAR_WIDTH) / 4d) * 3d);
public static final int TOP_BAR_HEIGHT = HEIGHT - INNER_FRAME_HEIGHT;
public static final int INNER_FRAME_WIDTH = WIDTH - LEFT_BAR_WIDTH;
public static int LEFT_BAR_WIDTH = FULL_WIDTH + THUMB_MARGIN;
public static int INNER_FRAME_HEIGHT = (int) (((WIDTH - LEFT_BAR_WIDTH) / 4d) * 3d);
public static int TOP_BAR_HEIGHT = HEIGHT - INNER_FRAME_HEIGHT;
public static int INNER_FRAME_WIDTH = WIDTH - LEFT_BAR_WIDTH;
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.setColor(Color.WHITE); g.drawRect(THUMB_MARGIN - 2, Y_OFFSET -
* 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 {
int imageWidth = image.getWidth();
@ -298,7 +303,7 @@ public class Viewer implements BasicApp {
Engine.log("after: " + newImageWidth + " x " + newImageHeight);
//do math, shove it on.
g.drawImage(newImage, 0, 0, null);
g.drawImage(newImage, (width - newImageWidth) / 2, (height - newImageHeight) / 2, null);
//return dat
return buffer;
@ -334,4 +339,23 @@ public class Viewer implements BasicApp {
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;
}
}