redoing all of the scaling methods...

master
marcus13345 2014-10-09 23:21:16 -04:00
parent ed5880ade6
commit 260cf8bcd0
2 changed files with 100 additions and 61 deletions

View File

@ -44,11 +44,6 @@ public class Item {
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);
image = ImageCreator.creatImageWithStripes(800, 600, Color.BLUE);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.setFont(new Font("Courier", Font.BOLD, 15));
g.drawString("" + path, 100, 100);
name = file.getName(); name = file.getName();
@ -56,7 +51,6 @@ public class Item {
// try and do the image thing! // try and do the image thing!
image = ImageIO.read(file); image = ImageIO.read(file);
image = getScaledImage(image, 800, 600);
thumbnail = (getScaledImage(image, 80, 80)); thumbnail = (getScaledImage(image, 80, 80));
path = file.getAbsolutePath(); path = file.getAbsolutePath();
@ -108,7 +102,6 @@ public class Item {
//return dat //return dat
return buffer; return buffer;
} }
public BufferedImage getImage() { public BufferedImage getImage() {
return image; return image;
} }

View File

@ -1,13 +1,16 @@
import static MAndEngine.Engine.ANIMATION_CONSTANT; import static MAndEngine.Engine.ANIMATION_CONSTANT;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import MAndEngine.AppHelper; import MAndEngine.AppHelper;
@ -18,9 +21,9 @@ import MAndEngine.Variable;
public class Viewer implements BasicApp { public class Viewer implements BasicApp {
public static final int THUMBNAIL_SIZE = 80; public static final int THUMBNAIL_SIZE = 80;
private static int WIDTH = 800, HEIGHT = 600; private static int WIDTH = 800, HEIGHT = 600;
// we need to have a home in case all else fails. // we need to have a home in case all else fails.
private static final String defaultSearchDirectory = System.getenv("APPDATA") + "\\MAndWorks\\MAndApps\\Backgrounds\\"; private static final String defaultSearchDirectory = System.getenv("APPDATA") + "\\MAndWorks\\MAndApps\\Backgrounds\\";
@ -41,16 +44,16 @@ public class Viewer implements BasicApp {
public static final int THUMB_MARGIN = 30; public static final int THUMB_MARGIN = 30;
public static final int FULL_WIDTH = THUMBNAIL_SIZE + THUMB_MARGIN; public static final int FULL_WIDTH = THUMBNAIL_SIZE + THUMB_MARGIN;
public static final int Y_OFFSET = 470; public static final int Y_OFFSET = 470;
public static final int X_OFFSET_SELECTION = (int)(THUMB_MARGIN + (selection * FULL_WIDTH) - (scroll * FULL_WIDTH)); public static final 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 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 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 TOP_BAR_HEIGHT = HEIGHT - INNER_FRAME_HEIGHT;
public static final int INNER_FRAME_WIDTH = WIDTH - LEFT_BAR_WIDTH; public static final int INNER_FRAME_WIDTH = WIDTH - LEFT_BAR_WIDTH;
private static Thread populationThread; private static Thread populationThread;
@Override @Override
public Dimension getResolution() { public Dimension getResolution() {
return new Dimension(WIDTH, HEIGHT); return new Dimension(WIDTH, HEIGHT);
@ -92,7 +95,7 @@ public class Viewer implements BasicApp {
} }
}); });
populationThread.start(); populationThread.start();
} }
private void repopulate() { private void repopulate() {
@ -114,9 +117,9 @@ public class Viewer implements BasicApp {
// folderssss // folderssss
for (String path : currentDirectoryFile.list()) { for (String path : currentDirectoryFile.list()) {
System.out.println("folders...");
File file = new File(currentDirectoryFile.getAbsolutePath() + "\\" + path); File file = new File(currentDirectoryFile.getAbsolutePath() + "\\" + path);
if (file.isDirectory()) { if (file.isDirectory()) {
System.out.println("folder: " + file.getAbsolutePath());
Item item = new Item(file.getAbsolutePath()); Item item = new Item(file.getAbsolutePath());
if (item.getSeemsLegit()) if (item.getSeemsLegit())
items.add(item); items.add(item);
@ -125,9 +128,9 @@ public class Viewer implements BasicApp {
// filezzzz // filezzzz
for (String path : currentDirectoryFile.list()) { for (String path : currentDirectoryFile.list()) {
System.out.println("files...");
File file = new File(currentDirectoryFile.getAbsolutePath() + "\\" + path); File file = new File(currentDirectoryFile.getAbsolutePath() + "\\" + path);
if (!file.isDirectory()) { if (!file.isDirectory()) {
System.out.println("file: " + file.getAbsolutePath());
Item item = new Item(file.getAbsolutePath()); Item item = new Item(file.getAbsolutePath());
if (item.getSeemsLegit()) if (item.getSeemsLegit())
items.add(item); items.add(item);
@ -157,59 +160,56 @@ public class Viewer implements BasicApp {
@Override @Override
public void tick() { public void tick() {
int desiredScroll = selection; int desiredScroll = selection;
scroll -= (scroll - desiredScroll) / (ANIMATION_CONSTANT); scroll -= (scroll - desiredScroll) / (ANIMATION_CONSTANT);
} }
@Override @Override
public void render(Graphics2D g) { public void render(Graphics2D g) {
try { try {
// this is the black box
//this is the black box // NOW IN GREY
//NOW IN GREY
g.setColor(new Color(200, 200, 200)); g.setColor(new Color(200, 200, 200));
g.fillRect(0, 0, WIDTH, HEIGHT); g.fillRect(0, 0, WIDTH, HEIGHT);
try{ try {
g.drawImage(items.get(selection).getImage(), WIDTH - items.get(selection).getImage().getWidth()/2 - INNER_FRAME_WIDTH/2, TOP_BAR_HEIGHT, null); g.drawImage(getScaledImage(items.get(selection).getImage(), INNER_FRAME_WIDTH, INNER_FRAME_HEIGHT), LEFT_BAR_WIDTH, TOP_BAR_HEIGHT, null);
}catch(Exception e) { } catch (Exception e) {
//between ticks, nothing really big // e.printStackTrace();
// between ticks, nothing really big
} }
g.setColor(new Color(225, 225, 225)); g.setColor(new Color(225, 225, 225));
g.fillRect(0, 0, WIDTH, TOP_BAR_HEIGHT); g.fillRect(0, 0, WIDTH, TOP_BAR_HEIGHT);
g.setColor(new Color(255, 127, 0)); g.setColor(new Color(255, 127, 0));
g.drawLine(0, TOP_BAR_HEIGHT - 1, WIDTH, TOP_BAR_HEIGHT - 1); g.drawLine(0, TOP_BAR_HEIGHT - 1, WIDTH, TOP_BAR_HEIGHT - 1);
g.setColor(new Color(240, 240, 240)); g.setColor(new Color(240, 240, 240));
g.fillRect(0, 0, FULL_WIDTH + THUMB_MARGIN, HEIGHT); g.fillRect(0, 0, FULL_WIDTH + THUMB_MARGIN, HEIGHT);
g.setColor(new Color(255, 127, 0)); g.setColor(new Color(255, 127, 0));
g.drawLine(LEFT_BAR_WIDTH - 1, 0, LEFT_BAR_WIDTH - 1, HEIGHT); g.drawLine(LEFT_BAR_WIDTH - 1, 0, LEFT_BAR_WIDTH - 1, HEIGHT);
g.setColor(new Color(35, 35, 35));
//g.setFont(Main.largerFont);
g.drawString(currentDirectoryVariable.getValue(), LEFT_BAR_WIDTH + THUMB_MARGIN, TOP_BAR_HEIGHT / 2 + 5);
for (int i = 0; i < items.size(); i++)
g.drawImage(items.get(i).getThumbnail(), THUMB_MARGIN, -1 + (int)(THUMB_MARGIN + (i * FULL_WIDTH) - (scroll * FULL_WIDTH)), null);
/* g.setColor(new Color(35, 35, 35));
g.setColor(Color.WHITE); // g.setFont(Main.largerFont);
g.drawRect(THUMB_MARGIN - 2, Y_OFFSET - 2, THUMB_WIDTH + 3, THUMB_WIDTH + 3);
g.drawString(currentDirectoryVariable.getValue(), LEFT_BAR_WIDTH + THUMB_MARGIN, TOP_BAR_HEIGHT / 2 + 5);
for (int i = 0; i < items.size(); i++)
g.drawImage(items.get(i).getThumbnail(), THUMB_MARGIN, -1 + (int) (THUMB_MARGIN + (i * FULL_WIDTH) - (scroll * FULL_WIDTH)), null);
g.setColor(Color.BLACK);
g.drawRect(THUMB_MARGIN - 1, Y_OFFSET - 1, THUMB_WIDTH + 1, THUMB_WIDTH + 1); /*
g.drawRect(THUMB_MARGIN - 3, Y_OFFSET - 3, THUMB_WIDTH + 5, THUMB_WIDTH + 5); * g.setColor(Color.WHITE); g.drawRect(THUMB_MARGIN - 2, Y_OFFSET -
*/ * 2, THUMB_WIDTH + 3, THUMB_WIDTH + 3);
*
* g.setColor(Color.BLACK); g.drawRect(THUMB_MARGIN - 1, Y_OFFSET -
* 1, THUMB_WIDTH + 1, THUMB_WIDTH + 1); g.drawRect(THUMB_MARGIN -
* 3, Y_OFFSET - 3, THUMB_WIDTH + 5, THUMB_WIDTH + 5);
*/
} catch (Exception e) { } catch (Exception e) {
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 600); g.fillRect(0, 0, 800, 600);
@ -224,16 +224,14 @@ public class Viewer implements BasicApp {
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
Engine.log("" + e.getKeyCode()); Engine.log("" + e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_D || if (e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_S) {
e.getKeyCode() == KeyEvent.VK_S) {
if (selection == items.size() - 1) if (selection == items.size() - 1)
selection = 0; selection = 0;
else else
selection++; selection++;
} }
if (e.getKeyCode() == KeyEvent.VK_A || if (e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_W) {
e.getKeyCode() == KeyEvent.VK_W) {
selection--; selection--;
if (selection < 0) if (selection < 0)
selection = items.size() - 1; selection = items.size() - 1;
@ -246,16 +244,64 @@ public class Viewer implements BasicApp {
Engine.switchApps(AppHelper.getIDbyClass("MainMenu")); Engine.switchApps(AppHelper.getIDbyClass("MainMenu"));
} }
} }
if(e.getKeyCode() == KeyEvent.VK_ESCAPE) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
if(selection == 0) { if (selection == 0) {
setCurrentDir(items.get(selection).getPath()); setCurrentDir(items.get(selection).getPath());
reload(); reload();
} else { } else {
selection = 0; selection = 0;
} }
} }
if (e.getKeyCode() == KeyEvent.VK_F5) {
reload();
}
}
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
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
//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) (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();
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, 0, 0, null);
//return dat
return buffer;
} }
@Override @Override