Created
March 16, 2016 17:34
-
-
Save JediMasterSam/3e3d474034c1da6ad9ad to your computer and use it in GitHub Desktop.
VectorApp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.imageio.ImageIO; | |
import java.io.File; | |
import java.io.IOException; | |
/** | |
* Created by Sam Gutermuth on 3/16/2016 | |
*/ | |
public class main { | |
public static void main(String[] args) throws IOException { | |
File file = new File("C:/Users/Sam/Desktop/mario.png"); | |
Sprite sprite = new Sprite(ImageIO.read(file),4); | |
sprite.save("C:/Users/Sam/Desktop/vector.png"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.util.ArrayList; | |
/** | |
* Created by Sam Gutermuth on 3/16/2016 | |
*/ | |
public class Shape implements Runnable{ | |
private Graphics graphics; | |
private boolean[][] table; | |
private Color color; | |
private int factor, half_factor; | |
private Thread thread; | |
int minX, minY, maxX, maxY; | |
public Shape(int width, int height, int color, int factor, int half_factor, Graphics graphics){ | |
this.graphics = graphics; | |
this.factor = factor; | |
this.half_factor = half_factor; | |
this.color = new Color(color); | |
table = new boolean[width][height]; | |
minX = width+1; | |
minY = height+1; | |
maxX = maxY = -1; | |
} | |
public void add(int x, int y){ | |
Point[] points = expand(x,y); | |
for(int i = 0; i < points.length; i++){ | |
placePoint(points[i].x,points[i].y); | |
} | |
} | |
//THREADED | |
@Override | |
public void run() { | |
render(sort(getPoints())); | |
} | |
public void start(){ | |
thread = new Thread(this); | |
thread.setPriority(Thread.MAX_PRIORITY); | |
thread.start(); | |
} | |
public Thread getThread(){ | |
return thread; | |
} | |
//HELPERS | |
private Point[] expand(int x, int y){ | |
x *= factor; | |
y *= factor; | |
return new Point[]{new Point(x+half_factor,y), new Point(x,y+half_factor), | |
new Point(x+factor,y+half_factor), new Point(x+half_factor,y+factor)}; | |
} | |
private void extrema(int x, int y){ | |
minX = Math.min(minX,x); | |
minY = Math.min(minY,y); | |
maxX = Math.max(maxX,x); | |
maxY = Math.max(maxY,y); | |
} | |
private void placePoint(int x, int y){ | |
table[x][y] = !table[x][y]; | |
extrema(x,y); | |
} | |
private ArrayList<Point> getPoints(){ | |
ArrayList<Point> points = new ArrayList<>(); | |
for(int x = minX; x <= maxX; x++){ | |
for(int y = minY; y <= maxY; y++){ | |
if(table[x][y]){ | |
points.add(new Point(x,y)); | |
} | |
} | |
} | |
return points; | |
} | |
private double distance(Point one, Point two){ | |
return Math.sqrt(Math.pow(one.x-two.x,2)+Math.pow(one.y-two.y,2)); | |
} | |
private ArrayList<Point> sort(ArrayList<Point> points){ | |
ArrayList<Point> hull = new ArrayList<>(); | |
hull.add(points.get(0)); | |
points.remove(0); | |
while (hasNext(points,hull)){} | |
return hull; | |
} | |
private Point next(Point current, ArrayList<Point> points){ | |
double distance = (double) factor; | |
int index = -1; | |
for(int i = 0; i < points.size(); i++){ | |
if(distance(current,points.get(i)) <= distance){ | |
distance = distance(current,points.get(i)); | |
index = i; | |
} | |
} | |
if(index < 0){ | |
return null; | |
} | |
return points.get(index); | |
} | |
private boolean hasNext(ArrayList<Point> points, ArrayList<Point> hull){ | |
Point next = next(hull.get(hull.size()-1),points); | |
if(next != null){ | |
hull.add(next); | |
points.remove(next); | |
return true; | |
} | |
return false; | |
} | |
private void render(ArrayList<Point> hull){ | |
int[] x = new int[hull.size()], y = new int[hull.size()]; | |
for(int i = 0; i < hull.size(); i++){ | |
x[i] = hull.get(i).x; | |
y[i] = hull.get(i).y; | |
} | |
graphics.setColor(color); | |
graphics.fillPolygon(new Polygon(x,y,hull.size())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.imageio.ImageIO; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.*; | |
/** | |
* Created by Sam Gutermuth on 3/16/2016 | |
*/ | |
public class Sprite { | |
private BufferedImage output; | |
private boolean[][] table; | |
private int width, height, factor, half_factor; | |
private ArrayList<Shape> shapes; | |
public Sprite(int factor){ | |
this.factor = factor - (factor%2); | |
half_factor = this.factor/2; | |
output = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); | |
shapes = new ArrayList<>(); | |
} | |
public void save(BufferedImage bufferedImage, String location) throws IOException { | |
table = new boolean[bufferedImage.getWidth()][bufferedImage.getHeight()]; | |
width = table.length*factor+factor; | |
height = table[0].length*factor+factor; | |
render(bufferedImage); | |
ImageIO.write(output,"png",new File(location)); | |
} | |
private void render(BufferedImage bufferedImage){ | |
getShapes(bufferedImage); | |
for(int i = 0; i < shapes.size(); i++){ | |
shapes.get(i).start(); | |
try { | |
shapes.get(i).getThread().join(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private void getShapes(BufferedImage bufferedImage){ | |
for(int x = 0; x < table.length; x++){ | |
for(int y = 0; y < table[0].length; y++){ | |
if(!table[x][y]){ | |
shapes.add(makeShape(x,y,bufferedImage.getRGB(x,y), bufferedImage)); | |
} | |
} | |
} | |
} | |
private Shape makeShape(int x, int y, int color, BufferedImage bufferedImage){ | |
Shape shape = new Shape(width,height,color,factor,half_factor,output.getGraphics()); | |
Queue<Point> queue = new LinkedList<>(); | |
queue.add(new Point(x,y)); | |
while (!queue.isEmpty()){ | |
Point point = queue.remove(); | |
if(point.x >= 0 && point.y >= 0 && point.x < table.length && point.y < table[0].length){ | |
int pX = point.x, pY = point.y; | |
if(bufferedImage.getRGB(pX,pY)== color && !table[pX][pY]){ | |
shape.add(pX,pY); | |
table[pX][pY] = true; | |
Point[] neighbors = neighbors(pX,pY); | |
for(int i = 0; i < neighbors.length; i++){ | |
queue.add(neighbors[i]); | |
} | |
} | |
} | |
} | |
return shape; | |
} | |
private Point[] neighbors(int x, int y){ | |
return new Point[]{new Point(x+1,y), new Point(x,y+1), new Point(x-1,y), new Point(x,y-1)}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment