master
Marcus Gosselin 2015-02-10 20:19:40 -05:00
parent d8ee40fbe9
commit ed57313ab4
7 changed files with 464 additions and 0 deletions

7
.classpath 100644
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/gson-2.3.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
.project 100644
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ImgurScreensaver</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7

6
src/Image.java 100644
View File

@ -0,0 +1,6 @@
public class Image {
public String id;
public String type;
public boolean nsfw;
}

View File

@ -0,0 +1,6 @@
public class ImageArray {
public Image[] data;
public boolean success;
public int status;
}

234
src/Main.java 100644
View File

@ -0,0 +1,234 @@
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import com.google.gson.Gson;
public class Main extends Canvas implements KeyListener, MouseMotionListener {
public static void main(String[] args) {
new Main();
}
private JFrame frame;
public Main() {
frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
this.addMouseMotionListener(this);
this.addKeyListener(this);
requestFocus();
// Transparent 16 x 16 pixel cursor image.
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
// Create a new blank cursor.
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
// Set the blank cursor to the JFrame.
frame.getContentPane().setCursor(blankCursor);
int pages = Integer.parseInt(new Variable("imgurscreensaver", "pages", "10", false).getValue());
loopPages(pages, new Variable("imgurscreensaver", "subreddit", "annakendrick", false).getValue());
}
private void loopPages(int pages, String subreddit) {
while (true)
for (int i = 0; i < pages; i++)
try {
String path = "https://api.imgur.com/3/gallery/r/" + subreddit + "/time/" + i + ".json";
HttpURLConnection connection = (HttpURLConnection) ((new URL(path)).openConnection());
System.out.println("Connecting...");
connection.setRequestMethod("GET");
// TODO Auto-generated catch block
connection.addRequestProperty("Authorization", "client-id 76535d44f1f94da");
connection.connect();
System.out.println("Response recieved with code " + connection.getResponseCode());
if (connection.getResponseCode() == 200) {
InputStream responseStream = connection.getInputStream();
StringBuilder builder = new StringBuilder();
int j = -1;
while ((j = responseStream.read()) != -1)
builder.append((char) j);
System.out.println(builder.toString());
Gson gson = new Gson();
ImageArray response = gson.fromJson(builder.toString(), ImageArray.class);
for (Image image : response.data) {
if (!(image.nsfw && filterNSFW) && !image.type.equals("image/gif")) {
String url = "http://imgur.com/" + (image.id) + (parseExtension(image.type));
currentimage = ImageIO.read(new URL(url));
currentimage = getScaledImage(currentimage, getWidth(), getHeight());
repaint();
Thread.sleep(SLEEPTIME);
}
}
}
} catch (Exception e) {
}
}
private String parseExtension(String type) {
if (type.equals("image/jpeg")) {
return ".jpg";
} else if (type.equals("image/png")) {
return ".png";
} else if (type.equals("image/gif")) {
return ".gif";
} else {
return ".jpg";
}
}
private boolean filterNSFW = Boolean.parseBoolean(new Variable("imgurscreensaver", "filterNSFW", "true", false).getValue());
private BufferedImage currentimage;
private static final long SLEEPTIME = Long.parseLong(new Variable("imgurscreensaver", "SLEEPTIME", "1300", false).getValue());
public void update(Graphics g) {
java.awt.Image buffer = createImage(getWidth(), getHeight());
Graphics g2 = buffer.getGraphics();
paint(g2);
g.drawImage(buffer, 0, 0, null);
}
private 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;
double aspect = (double) imageWidth / imageHeight;
double screenAspect = ((double)getWidth()/getHeight());
System.out.println("" + aspect + "\n" + screenAspect);
// fill or fit bit
if (aspect < screenAspect || aspect > 2)
if (scaleX > scaleY)
scaleX = scaleY;
else
scaleY = scaleX;
else
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);
// do math, shove it on.
g.drawImage(newImage, (width - newImageWidth) / 2, (height - newImageHeight) / 2, null);
// return dat
return buffer;
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.drawString("Connecting to server...", 0, getHeight());
try {
// BufferedImage image = (BufferedImage) ImageIO.read(new
// URL(currentURL));
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(currentimage, 0, 0, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private void close() {
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
@Override
public void mouseDragged(MouseEvent arg0) {
close();
}
private boolean moved = false;
@Override
public void mouseMoved(MouseEvent e) {
if (moved)
close();
else
moved = true;
}
@Override
public void keyPressed(KeyEvent arg0) {
close();
}
@Override
public void keyReleased(KeyEvent arg0) {
close();
}
@Override
public void keyTyped(KeyEvent arg0) {
close();
}
}

183
src/Variable.java 100644
View File

@ -0,0 +1,183 @@
import java.io.File;
import java.io.IOException;
import java.util.Formatter;
import java.util.Scanner;
/**
* to note, will not work on Mac yet.
*
* edit: WILL WORK ON MAC MOTHER FUCKERS
*
* edit: idek if this will work on macs because app data...
*
* @author Marcus
*
*/
public class Variable {
private String value;
private String filePath;
private String fileDir;
private static String fileExtension;
private static String BASE_DIR = "" + System.getenv("APPDATA") + "\\";
static {
// first the default value is set. this is used
// only to get the real value.
fileExtension = "var";
// make a new variable, for the extension
// do not force it to be var.
Variable var = new Variable("MAndLib\\core", "extension", "var", false);
// grab its value and reset the extension.
fileExtension = var.getValue();
}
/**
* dir - where the variable file is stored. - enter things like "hjkl\\asdf"</br>
* </br>
* name - simple, name of variable file</br>
* </br>
* value - value to try and set the file to. though if it already has a
* value, it won't do anything</br>
* </br>
* force - if true, value will always be set to the value given, regardless
* of if the value is already there.</br>
* </br>
* @param dir
* @param name
* @param value
* @param force
*/
public Variable(String dir, String name, String value, boolean force) {
fileDir = BASE_DIR + dir;
filePath = BASE_DIR + dir + "\\" + name + "." + fileExtension;
// try and load value from file, if null, screw it.
String str = getValueFromFile();
// if we could not load a value from the file
// AKA didnt fucking exist.
// ORRRRRRR if you were an ass, and forced
// the value.
if (str == null) {
this.value = value;
saveValue();
} else if (force) {
this.value = value;
saveValue();
// else we can load the value from the file
} else {
this.value = str;
}
}
/**
* return the class variable for the value.
* shouldn't have to load it again because we
* always change the value locally first before we save.
* @return
*/
public String getValue() {
return value;
}
/**
* set the value in the local class, then open a thread for saving it
*
* @param value
*/
public void setValue(String value) {
this.value = value;
new Thread(new Runnable() {
public void run() {
saveValue();
}
}).start();
}
/**
* deletes and recreates the file with the new value
*/
private void saveValue() {
deleteFile();
createFile();
try {
// Q&D formatter to write value to current filepath.
Formatter f = new Formatter(filePath);
f.format("" + value);
f.close();
} catch (Exception e) {
// if(weArriveHere){
// we.are("fucked");
// }
e.printStackTrace();
}
}
/**
* for refreshing the file
*/
private void deleteFile() {
File f = new File(filePath);
f.delete();
}
/**
* creates empty file at the correct path.
*/
private void createFile() {
//make the directory because god knows, java can't do that for us
//when we say we want a new file in an unknown folder noooooo....
//jackass java
File f = new File(fileDir);
f.mkdirs();
//no onto the file itself. create the object
f = new File(filePath);
try {
//hopefully make the file...
f.createNewFile();
} catch (IOException e) {
// if(weArriveHere){
// we.are("fucked");
// }
e.printStackTrace();
}
}
/**
* grab the value from the file, if nothing is there, null.
*
* @return
*/
private String getValueFromFile() {
try {
//create the file... we don't want to do anyth stupid checking because if its
//not perfect, we honestly don't care, just return null and reset the variable
File f = new File(filePath);
//open a scanner on the file
Scanner s = new Scanner(f);
//get the assumed value
String str = s.nextLine();
//close the file because for some reason if you don't
//you end up dividing by zero...
s.close();
//gimme dat string
return str;
} catch (Exception e) {
//dunno, don't care, reset.
return null;
}
}
public static void setBaseDir(String dir) {
BASE_DIR = dir;
}
}