From 3eb0962a59f0e054014a3da71beefcd13e5faf90 Mon Sep 17 00:00:00 2001 From: Marcus Gosselin Date: Wed, 8 Oct 2014 22:40:09 -0400 Subject: [PATCH] made things make more sense and such --- src/Item.java | 104 ++++++++++++++++++++++-------------------------- src/Viewer.java | 67 +++++++++++++++++-------------- 2 files changed, 85 insertions(+), 86 deletions(-) diff --git a/src/Item.java b/src/Item.java index 9891eb7..49ad6a4 100644 --- a/src/Item.java +++ b/src/Item.java @@ -1,8 +1,6 @@ - - import java.awt.Color; +import java.awt.Font; import java.awt.Graphics; -import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; @@ -11,13 +9,19 @@ import java.io.IOException; import javax.imageio.ImageIO; +import MAndEngine.ImageCreator; + public class Item { private static int scaleToHeight; + + //cropped to a thumb nail, of size 80*80 private final BufferedImage thumbnail; + + //the original image, not cropped or anything. private final BufferedImage image; - private final int yRange; - private final boolean isDir; + + //Absolute path, i think. private final String path; private String name; @@ -27,35 +31,24 @@ public class Item { // then we don't try and render something that // makes no sense. // set this to false by the end of the constructor - // and when it comes back to being added to the list, - // it'll be thrown out. + // and when it comes back to be added to the list, + // it'll get thrown out. private boolean seemsLegit = true; public Item(String path) { - BufferedImage thumbnail = null; BufferedImage image = null; - int yRange = 0; - boolean isDir = false; try { File file = new File(path); if (file.isDirectory()) { - // then lets do the directory thing! - yRange = 0; - - thumbnail = new BufferedImage(80, 80, BufferedImage.TYPE_INT_ARGB); - Graphics thumbnailGraphics = thumbnail.getGraphics(); - thumbnailGraphics.setColor(new Color(0, 0, 255)); - thumbnailGraphics.fillRect(0, 0, 80, 80); - - image = new BufferedImage((int)((scaleToHeight/3d)*4d), scaleToHeight, BufferedImage.TYPE_INT_ARGB); - Graphics imageGraphics = image.getGraphics(); - imageGraphics.fillRect(0, 0, (int)((scaleToHeight/3d)*4d), scaleToHeight); - - imageGraphics.setColor(Color.black); - imageGraphics.drawString("" + path, 100, 100); + 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(); @@ -63,55 +56,57 @@ public class Item { // try and do the image thing! image = ImageIO.read(file); - image = getScaledImage(image, scaleToHeight); - thumbnail = (getScaledImageFill(image, 80, 80)); + image = getScaledImage(image, 800, 600); + thumbnail = (getScaledImage(image, 80, 80)); path = file.getAbsolutePath(); - - name = ""; - - yRange = image.getHeight() - 600; } } catch (Exception e) { seemsLegit = false; + System.out.println("wat: " + e.getMessage()); + System.out.println("wat was in " + path); } this.image = image; - this.isDir = isDir; this.path = path; this.thumbnail = thumbnail; - this.yRange = yRange; - } public boolean getSeemsLegit() { return seemsLegit; } - private static BufferedImage getScaledImage(BufferedImage image, int height) throws IOException { + private static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException { + int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); - double scale = (double) height / imageHeight; - AffineTransform scaleTransform = AffineTransform.getScaleInstance(scale, scale); - AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR); - return bilinearScaleOp.filter(image, new BufferedImage((int) (imageWidth * scale), height, image.getType())); - - } - - private static BufferedImage getScaledImageFill(BufferedImage image, int width, int height) throws IOException { - int imageWidth = image.getWidth(); - int imageHeight = image.getHeight(); - double scaleX = (double) width / imageWidth; + double scaleY = (double) height / imageHeight; - double scale = scaleX > scaleY ? scaleX : scaleY; - AffineTransform scaleTransform = AffineTransform.getScaleInstance(scale, scale); + 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); - return bilinearScaleOp.filter(image, new BufferedImage(width, height, image.getType())); - } - - public double getYRange() { - return yRange; + + //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; } public BufferedImage getImage() { @@ -126,12 +121,7 @@ public class Item { return path; } - public static void setImageHeight(int i) { - scaleToHeight = i; - } - public String getName() { return name; } - } \ No newline at end of file diff --git a/src/Viewer.java b/src/Viewer.java index a301052..38a1f18 100644 --- a/src/Viewer.java +++ b/src/Viewer.java @@ -2,11 +2,11 @@ import static MAndEngine.Engine.ANIMATION_CONSTANT; -import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.event.KeyEvent; +import java.awt.image.BufferedImage; import java.io.File; import java.util.ArrayList; @@ -17,41 +17,39 @@ import MAndEngine.Variable; public class Viewer implements BasicApp { - private static final int WIDTH = 800, HEIGHT = 600; + public static final int THUMBNAIL_SIZE = 80; + + private static int WIDTH = 800, HEIGHT = 600; // we need to have a home in case all else fails. private static final String defaultSearchDirectory = System.getenv("APPDATA") + "\\MAndWorks\\MAndApps\\Backgrounds\\"; // current selection - private int selection = 0; + private static int selection = 0; // rendering animation number thing that // looks like selection but is rendered // horizontally and stuff. in pixels. BC ANIMATION - private double scroll; - - // TIME. pretty self explanitory. - private static double time = 0; + private static double scroll; // items list private ArrayList items; - private File currentDirectoryFile; - private Variable currentDirectoryVariable; + private static File currentDirectoryFile; + private static Variable currentDirectoryVariable; - final int THUMB_WIDTH = 80; - final int THUMB_MARGIN = 30; - final int FULL_WIDTH = THUMB_WIDTH + THUMB_MARGIN; - final int Y_OFFSET = 470; + public static final int THUMB_MARGIN = 30; + public static final int FULL_WIDTH = THUMBNAIL_SIZE + THUMB_MARGIN; + public static final int Y_OFFSET = 470; - 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)); - final int LEFT_BAR_WIDTH = FULL_WIDTH + THUMB_MARGIN; - final int INNER_FRAME_HEIGHT = (int)(((WIDTH - LEFT_BAR_WIDTH) / 4d) * 3d); - final int TOP_BAR_HEIGHT = HEIGHT - INNER_FRAME_HEIGHT; - final int INNER_FRAME_WIDTH = WIDTH - LEFT_BAR_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; - //YES IT WORKED + private static Thread populationThread; @Override public Dimension getResolution() { @@ -65,11 +63,10 @@ public class Viewer implements BasicApp { @Override public void initialize() { - Item.setImageHeight(INNER_FRAME_HEIGHT); //NOW WITH MODULAR GOODIES // make sure we have our variable and file // we can explicitly set them as we are trying to pick up where we left // off - currentDirectoryVariable = new Variable("MAndWorks\\MAndApps\\Settings", "WallpaperSearchDirectory", defaultSearchDirectory, false); + currentDirectoryVariable = new Variable("MAndWorks\\MAndApps\\Settings", "WallpaperSearchDirectory", defaultSearchDirectory, true); currentDirectoryFile = new File(currentDirectoryVariable.getValue()); // which we use, be it variable or file, is completely arbitrary here. @@ -82,29 +79,42 @@ public class Viewer implements BasicApp { items = new ArrayList(); selection = 0; - new Thread(new Runnable() { + try { + populationThread.stop(); + } catch (Exception e) { + e.printStackTrace(); + } + populationThread = new Thread(new Runnable() { public void run() { repopulate(); } - }).start(); + }); + populationThread.start(); + } private void repopulate() { if (currentDirectoryFile.isDirectory()) { + System.out.println("adding?"); // parent? String parent = currentDirectoryFile.getParent(); if (parent != null) { + System.out.println("adding parent?"); Item item = new Item(parent); - if (item.getSeemsLegit()) + if (item.getSeemsLegit()) { + System.out.println("added."); items.add(item); + } } + System.out.println("k?"); // folderssss for (String path : currentDirectoryFile.list()) { + System.out.println("folders..."); File file = new File(currentDirectoryFile.getAbsolutePath() + "\\" + path); if (file.isDirectory()) { Item item = new Item(file.getAbsolutePath()); @@ -115,6 +125,7 @@ public class Viewer implements BasicApp { // filezzzz for (String path : currentDirectoryFile.list()) { + System.out.println("files..."); File file = new File(currentDirectoryFile.getAbsolutePath() + "\\" + path); if (!file.isDirectory()) { Item item = new Item(file.getAbsolutePath()); @@ -151,8 +162,6 @@ public class Viewer implements BasicApp { scroll -= (scroll - desiredScroll) / (ANIMATION_CONSTANT); - time += 1 / 60d; - time = Math.PI + 1; // PI IS TOP, 0/2PI is bottom } @Override @@ -185,7 +194,6 @@ public class Viewer implements BasicApp { g.setColor(new Color(35, 35, 35)); //g.setFont(Main.largerFont); - int dirWidth = g.getFontMetrics().stringWidth(currentDirectoryVariable.getValue()); g.drawString(currentDirectoryVariable.getValue(), LEFT_BAR_WIDTH + THUMB_MARGIN, TOP_BAR_HEIGHT / 2 + 5); @@ -241,7 +249,8 @@ public class Viewer implements BasicApp { if(e.getKeyCode() == KeyEvent.VK_ESCAPE) { if(selection == 0) { - Engine.switchApps(AppHelper.getIDbyClass("MainMenu")); + setCurrentDir(items.get(selection).getPath()); + reload(); } else { selection = 0; } @@ -271,7 +280,7 @@ public class Viewer implements BasicApp { @Override public boolean getResizable() { - return false; + return true; } @Override