made UI look nice

and variables make sense
and resizable a thing
master
Marcus Gosselin 2014-11-18 09:40:18 -08:00
parent b0ec5f9b18
commit 2678822b15
1 changed files with 134 additions and 72 deletions

View File

@ -1,5 +1,6 @@
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@ -24,15 +25,51 @@ import MAndEngine.Engine;
* @author Marcus * @author Marcus
* *
*/ */
public class ImgurDownloader implements BasicApp, ImageListener{ public class ImgurDownloader implements BasicApp, ImageListener {
private ArrayList<GraphicalImgurRequest> requests; private ArrayList<GraphicalImgurRequest> requests;
private String textBox = ""; private String textBox = "";
private BufferedImage image; private BufferedImage image;
/**
* base dimensions, changes with resize.
*/
private int WIDTH = 800, HEIGHT = 600;
/**
* height of the bottom bar that houses the requests.
*/
private final int BOTTOM_HEIGHT = 80;
/**
* height of the top bar that houses the textbox for command entering.
*/
private final int TOP_HEIGHT = 35;
/**
* the bit of space above the requests for padding.
*/
private final int TOP_MARGIN = 25;
/**
* Width of the image to be displayed in the center
*/
private int INNER_FRAME_WIDTH;
/**
* height of the image to be displayed in the center.
*/
private int INNER_FRAME_HEIGHT;
/**
* Top font.<br/>
* Top kek.
*/
private final Font font = new Font("Arial", Font.BOLD, 30);
@Override @Override
public Dimension getResolution() { public Dimension getResolution() {
return new Dimension(800, 600); return new Dimension(WIDTH, HEIGHT);
} }
@Override @Override
@ -40,132 +77,148 @@ public class ImgurDownloader implements BasicApp, ImageListener{
requests = new ArrayList<GraphicalImgurRequest>(); requests = new ArrayList<GraphicalImgurRequest>();
try { try {
image = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("start.png")); image = ImageIO.read(this.getClass().getClassLoader()
.getResourceAsStream("start.png"));
} catch (Exception e) { } catch (Exception e) {
image = null; image = null;
System.out.println("eakljrfgskldhfg"); System.out.println("eakljrfgskldhfg");
} }
} }
@Override @Override
public void resumeApp() { public void resumeApp() {
// TODO Auto-generated method stub
} }
@Override @Override
public void pauseApp() { public void pauseApp() {
// TODO Auto-generated method stub
} }
final int LEFT_MARGIN = 20; private final int LEFT_MARGIN = 20;
@Override @Override
public void tick() { public void tick() {
int GAP = GraphicalImgurRequest.WIDTH + LEFT_MARGIN; int GAP = GraphicalImgurRequest.WIDTH + LEFT_MARGIN;
for (int i = 0; i < requests.size(); i++) { for (int i = 0; i < requests.size(); i++) {
requests.get(i).setDestination(LEFT_MARGIN + (GAP * i), INNER_FRAME_HEIGHT + 25); requests.get(i).setDestination(LEFT_MARGIN + (GAP * i),
HEIGHT - BOTTOM_HEIGHT + TOP_MARGIN);
requests.get(i).tick(); requests.get(i).tick();
} }
Iterator<GraphicalImgurRequest> iterator = requests.iterator(); Iterator<GraphicalImgurRequest> iterator = requests.iterator();
while(iterator.hasNext()) { while (iterator.hasNext()) {
GraphicalImgurRequest req = iterator.next(); GraphicalImgurRequest req = iterator.next();
if(!req.isBusy()) { if (!req.isBusy()) {
iterator.remove(); iterator.remove();
} }
} }
if (Engine.keys[KeyEvent.VK_ENTER] && textBox != "") { if (Engine.keys[KeyEvent.VK_ENTER] && textBox != "") {
execute(textBox); execute(textBox);
textBox = ""; textBox = "";
} }
} }
private void execute(String str) { private void execute(String str) {
String[] parts = str.split(" "); String[] parts = str.split(" ");
if(parts[0].equalsIgnoreCase("load")) { if (parts[0].equalsIgnoreCase("load")) {
loadScript(parts[1]); loadScript(parts[1]);
}else{ } else if (parts[0].equalsIgnoreCase("user")) {
//assume subreddit GraphicalImgurRequest request = new GraphicalImgurRequest(
GraphicalImgurRequest request = new GraphicalImgurRequest(0, 0, 0, 0, this); (int) (WIDTH * 1.2d), HEIGHT - BOTTOM_HEIGHT + TOP_MARGIN,
0, 0, this);
int pages = 0; int pages = 0;
try{ try {
pages = Integer.parseInt(parts[2]);
} catch (Exception e) {
}
request.saveSubmitted(parts[1], pages == 0 ? 10 : pages);
requests.add(request);
} else {
// assume subreddit
GraphicalImgurRequest request = new GraphicalImgurRequest(
(int) (WIDTH * 1.2d), HEIGHT - BOTTOM_HEIGHT + TOP_MARGIN,
0, 0, this);
int pages = 0;
try {
pages = Integer.parseInt(parts[1]); pages = Integer.parseInt(parts[1]);
}catch (Exception e) {} } catch (Exception e) {
}
request.saveSubreddit(parts[0], pages == 0 ? 10 : pages, "time"); request.saveSubreddit(parts[0], pages == 0 ? 10 : pages, "time");
requests.add(request); requests.add(request);
} }
} }
private void loadScript(String script) { private void loadScript(String script) {
File file = new File(System.getenv("APPDATA") + "\\MAndWorks\\ImgurDownloader\\scripts\\" + script + ".lst"); File file = new File(System.getenv("APPDATA")
+ "\\MAndWorks\\ImgurDownloader\\scripts\\" + script + ".lst");
try { try {
Scanner scan = new Scanner(file); Scanner scan = new Scanner(file);
while(scan.hasNextLine()) { while (scan.hasNextLine()) {
String line = scan.nextLine(); String line = scan.nextLine();
execute(line); execute(line);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
//TODO NAME THESE RIGHT
final int INNER_FRAME_WIDTH = 800;
final int INNER_FRAME_HEIGHT = 520;
//yeah, image scaling!
final int IMAGE_WIDTH = 800;
final int IMAGE_HEIGHT = 520 - 35;
@Override @Override
public void render(Graphics2D g) { public void render(Graphics2D g) {
g.setColor(Color.WHITE); // background stuff
g.fillRect(0, 0, 800, 600);
g.setColor(new Color(230, 230, 230));
g.fillRect(0, 0, 800, 600);
g.setColor(new Color(210, 210, 210)); g.setColor(new Color(210, 210, 210));
g.fillRect(0, 0, INNER_FRAME_WIDTH, INNER_FRAME_HEIGHT); g.fillRect(0, 0, WIDTH, HEIGHT);
// draw the image
g.drawImage(image, 0, TOP_HEIGHT, null);
// fill the top bar
g.setColor(new Color(230, 230, 230)); g.setColor(new Color(230, 230, 230));
g.fillRect(0, 0, 800, 35); g.fillRect(0, 0, WIDTH, TOP_HEIGHT);
// draw the top line
g.setColor(rgb(255, 154, 0));
g.drawLine(0, TOP_HEIGHT, WIDTH, TOP_HEIGHT);
// fill the bottom bar
g.setColor(new Color(230, 230, 230));
g.fillRect(0, HEIGHT - BOTTOM_HEIGHT, WIDTH, BOTTOM_HEIGHT);
// drop the bottom line
g.setColor(rgb(255, 154, 0));
g.drawLine(0, HEIGHT - BOTTOM_HEIGHT, WIDTH, HEIGHT - BOTTOM_HEIGHT);
// requests?
g.setColor(new Color(50, 50, 50));
// g.drawString("Requests: " + requests.size(), 0, HEIGHT);
// draw the requests on top of the bottom bar.
for (int i = 0; i < requests.size(); i++) { for (int i = 0; i < requests.size(); i++) {
requests.get(i).draw(g); requests.get(i).draw(g);
} }
g.setColor(new Color(70, 70, 70));
g.drawString("save: " + textBox, 100, 20);
g.drawImage(image, 0, 35, null); g.setFont(font);
g.setColor(rgb(255,154,0)); g.setColor(new Color(70, 70, 70));
g.drawLine(0, INNER_FRAME_HEIGHT, 800, INNER_FRAME_HEIGHT); // TODO filepath variable and stufffffff ya
g.setColor(rgb(255,154,0)); g.drawString("> " + textBox
g.drawLine(0, 35, 800, 35); + ((System.currentTimeMillis() / 1000) % 2 == 0 ? "I" : ""),
80, 28);
g.setColor(new Color(50, 50, 50));
g.drawString("Requests: " + requests.size(), 0, 600);
} }
private Color rgb (int r, int g, int b) { private Color rgb(int r, int g, int b) {
return new Color(r, g, b); return new Color(r, g, b);
} }
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
char c = ("" + e.getKeyChar()).toLowerCase().charAt(0); char c = ("" + e.getKeyChar()).toLowerCase().charAt(0);
if ((c >= 97 && c < 123) || c == ' ') { if ((c >= 97 && c < 123) || c == ' ' || (c >= 48 && c < 58)) {
textBox += c; textBox += c;
@ -179,7 +232,6 @@ public class ImgurDownloader implements BasicApp, ImageListener{
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
} }
@ -210,27 +262,31 @@ public class ImgurDownloader implements BasicApp, ImageListener{
@Override @Override
public void resized(int width, int height) { public void resized(int width, int height) {
// not resizable WIDTH = width;
HEIGHT = height;
INNER_FRAME_WIDTH = WIDTH;
INNER_FRAME_HEIGHT = HEIGHT - BOTTOM_HEIGHT - TOP_HEIGHT;
} }
@Override @Override
public void click() { public void click() {
// TODO
} }
@Override @Override
public void newImage(BufferedImage image) { public void newImage(BufferedImage image) {
try { try {
image = fitImageScale(image, IMAGE_WIDTH, IMAGE_HEIGHT); image = fitImageScale(image, INNER_FRAME_WIDTH, INNER_FRAME_HEIGHT);
this.image = image; this.image = image;
} catch (IOException e) { } catch (IOException e) {
//eh, whatevs // eh, whatevs
} }
} }
private static BufferedImage fitImageScale(BufferedImage image, int width, int height) throws IOException { private static BufferedImage fitImageScale(BufferedImage image, int width,
int height) throws IOException {
int imageWidth = image.getWidth(); int imageWidth = image.getWidth();
int imageHeight = image.getHeight(); int imageHeight = image.getHeight();
@ -246,24 +302,30 @@ public class ImgurDownloader implements BasicApp, ImageListener{
scaleY = scaleX; scaleY = scaleX;
// give us the transform object thing // 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 // out new image that we need to crop onto the buffer with the right
// dimensions. // dimensions.
BufferedImage newImage = bilinearScaleOp.filter(image, new BufferedImage((int) (imageWidth * scaleX), (int) (imageHeight * scaleY), image.getType())); BufferedImage newImage = bilinearScaleOp.filter(image,
new BufferedImage((int) (imageWidth * scaleX),
(int) (imageHeight * scaleY), image.getType()));
// make the buffer // 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();
int newImageWidth = newImage.getWidth(null); int newImageWidth = newImage.getWidth(null);
int newImageHeight = newImage.getHeight(null); int newImageHeight = newImage.getHeight(null);
// do math, shove it on. // do math, shove it on.
g.drawImage(newImage, (width - newImageWidth) / 2, (height - newImageHeight) / 2, null); g.drawImage(newImage, (width - newImageWidth) / 2,
(height - newImageHeight) / 2, null);
// return dat // return dat
return buffer; return buffer;