Skip to content

Commit

Permalink
MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
RaneWallin committed Dec 13, 2018
1 parent 15ef2de commit 0f87dc3
Show file tree
Hide file tree
Showing 9 changed files with 1,274 additions and 58 deletions.
1,025 changes: 1,025 additions & 0 deletions TestImg

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/ClearButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Rane Wallin
Final Programming Project
ClearButton extends the javafx Button class. It clears the canvas when clicked
*/
import javafx.scene.control.Button;

public class ClearButton extends Button {

public ClearButton(String text) {
super(text);
}

public void fire() {
DrawingPane.getInstance().createNew();
}
}
41 changes: 38 additions & 3 deletions src/ColorMap.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
/*
Rane Wallin
Final Programming Assignment
ColorMap holds the color data for the active drawing
*/

import javafx.scene.paint.Color;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class ColorMap {
private static ColorMap single_instance = null;
private Map<Pixel, Color> colorMap = new HashMap<>();
private List<Pixel> allPixels = new ArrayList<>();
private Set<Pixel> allPixels = new TreeSet<>();
private double pixelSize = 0;

public void addColor(Pixel pix, Color color) {
if(pixelSize == 0) pixelSize = pix.getPixSize();
colorMap.put(pix, color);
allPixels.add(pix);
}
Expand All @@ -23,10 +33,35 @@ public Color getColor(Pixel pix) {
return colorMap.get(pix);
}

protected List<Pixel> getPixels() {
public void replaceColorMap(List<String> colorData) {
double pixelSize = Double.parseDouble(colorData.get(0));
colorMap = new HashMap<>();
allPixels = new TreeSet<>();

colorData.remove(0);
PreviewPane.getInstance().clearPreview();

for(String input: colorData) {
String[] data = input.split(":");
Pixel pix = new Pixel(pixelSize, Integer.parseInt(data[0]), Color.web(data[1]));
colorMap.put(pix, Color.web(data[1]));
allPixels.add(pix);
}
DrawingPane.getInstance().loadCanvas();
}

protected Set<Pixel> getPixels() {
return allPixels;
}

protected double getPixelSize() { return pixelSize; }

protected void clearColorMap() {
pixelSize = 0;
allPixels.clear();
colorMap.clear();
}

public static ColorMap getInstance() {
if (single_instance == null)
single_instance = new ColorMap();
Expand Down
58 changes: 51 additions & 7 deletions src/DrawingPane.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
/*
Rane Wallin
Final Programming Project
DrawingPane extends the FlowPane class. It represents the drawing canvas
*/
import javafx.geometry.Orientation;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.StrokeType;

import java.util.Set;

public class DrawingPane extends FlowPane {
private static DrawingPane single_instance = null;

private int workspaceSize;
private int bitStyle;
private PreviewPane preview;
private ColorPicker picker;

public DrawingPane(int workspaceSize, int bitStyle, PreviewPane preview) {
public DrawingPane() {
super(Orientation.HORIZONTAL, 0, 0);
this.bitStyle = bitStyle;
this.workspaceSize = workspaceSize;
this.preview = preview;
}

protected void createSpace(ColorPicker picker) {
protected void createNew() {
ColorMap colorMap = ColorMap.getInstance();
this.picker = picker;

setVgap(0);
setHgap(0);

setMinSize(workspaceSize, workspaceSize);
setMaxSize(workspaceSize, workspaceSize);

colorMap.clearColorMap();
getChildren().clear();

for(int i = 1; i <= bitStyle*bitStyle; i++) {
int dim = workspaceSize/bitStyle;
Pixel pix = new Pixel(dim, i, picker);

pix.paint(Color.WHITE);
pix.strokePixel(StrokeType.INSIDE, Color.LIGHTGRAY, 0.25);
pix.setPreview(preview);
PreviewPane.getInstance().updateSpace(pix, colorMap.getColor(pix));

colorMap.addColor(pix, Color.WHITE);

Expand All @@ -41,4 +51,38 @@ protected void createSpace(ColorPicker picker) {
}
}

protected void addPicker(ColorPicker picker) {
this.picker = picker;
}

protected void setBitStyle(int bitStyle) {
this.bitStyle = bitStyle;
}

protected void setWorkspaceSize(int workspaceSize) {
this.workspaceSize = workspaceSize;
}

protected void loadCanvas() {
ColorMap colorMap = ColorMap.getInstance();

Set<Pixel> pixels = colorMap.getPixels();
getChildren().clear();

for(Pixel pixel: pixels) {
pixel.strokePixel(StrokeType.INSIDE, Color.LIGHTGRAY, 0.25);
pixel.addPicker(picker);
getChildren().add(pixel);

}

}

public static DrawingPane getInstance() {
if (single_instance == null)
single_instance = new DrawingPane();

return single_instance;
}

}
40 changes: 34 additions & 6 deletions src/IOButton.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import javafx.scene.control.Button;
/*
Rane Wallin
Final Programming Assignment
IOButton extends the javafx Button class. It handles file input
and output
*/


import javafx.stage.FileChooser;
import javafx.stage.Stage;

import javafx.scene.control.Button;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class IOButton extends Button {
private String ioType;
Expand All @@ -19,10 +30,25 @@ public IOButton(String buttonText, String ioType, Stage stage) {
this.stage = stage;
}

private File openFile(String title) {
private void loadFile(String title) {
FileChooser chooser = new FileChooser();
FileInputStream in = null;
ColorMap colorMap = ColorMap.getInstance();
chooser.setTitle(title);
return chooser.showOpenDialog(stage);

File file = chooser.showOpenDialog(stage);

if(file != null) {
try {
Path input = Paths.get(file.getPath());
List<String> fileInput = new ArrayList<>();
fileInput = Files.readAllLines(input);
colorMap.replaceColorMap((ArrayList) fileInput);
} catch(IOException ex) {
ex.printStackTrace();
}
}

}

private void saveFile(String title) {
Expand All @@ -36,8 +62,9 @@ private void saveFile(String title) {
if(file != null) {
try {
Path output = Paths.get(file.getPath());
ArrayList<String> fileOutput = new ArrayList<>();
ArrayList<Pixel> pixels = (ArrayList) ColorMap.getInstance().getPixels();
List<String> fileOutput = new ArrayList<>();
fileOutput.add(ColorMap.getInstance().getPixelSize()+"");
Set<Pixel> pixels = (TreeSet) ColorMap.getInstance().getPixels();
for(Pixel pixel: pixels) {
fileOutput.add(pixel.getPixID()+":"+ColorMap.getInstance().getColor(pixel));
}
Expand All @@ -57,6 +84,7 @@ public void fire() {
saveFile(title);
break;
case "load":
loadFile(title);
break;
default:
System.out.println("Error");
Expand Down
40 changes: 33 additions & 7 deletions src/Pixel.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
/*
Rane Wallin
Final Programming Assignment
Pixel extends the javafx Rectangle class and implements the Comparable interface
Pixel contains the data for each individual pixel, as well as the logic and
EventHandlers
*/

import javafx.event.EventHandler;
import javafx.scene.control.ColorPicker;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;

public class Pixel extends Rectangle {
public class Pixel extends Rectangle implements Comparable<Pixel> {
private final int pixID;
private ColorPicker picker;
private PreviewPane preview;
ColorMap colorMap = ColorMap.getInstance();

public Pixel(double dim, int pixID, ColorPicker picker) {
Expand All @@ -28,11 +35,16 @@ public Pixel(double dim, int pixID) {
this.pixID = pixID;
}

public void setPreview(PreviewPane preview) {
this.preview = preview;
public int getPixID() { return pixID; }

public double getPixSize() {
return getHeight();
}

public int getPixID() { return pixID; }
protected void addPicker(ColorPicker picker) {
this.picker = picker;
setHandlers();
}

public void strokePixel(StrokeType type, Color strokeColor, double strokeWidth) {
setStrokeType(type);
Expand All @@ -41,9 +53,11 @@ public void strokePixel(StrokeType type, Color strokeColor, double strokeWidth)
}

public void paint(Color color) {
PreviewPane preview = PreviewPane.getInstance();

setFill(color);
if (preview != null) preview.updateSpace(this, color);

if(preview != null) preview.updateSpace(this, color);

colorMap.addColor(this, color);
}
Expand Down Expand Up @@ -71,4 +85,16 @@ public void handle(MouseEvent event) {
});
}

@Override
public int compareTo(Pixel otherPixel) {
int compared;

if (otherPixel.getPixID() == this.pixID) compared = 0;
else
if (otherPixel.getPixID() > this.pixID) compared = -1;
else compared = 1;

return compared;
}

}
20 changes: 16 additions & 4 deletions src/PixiePal.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/*
Rane Wallin
Final Programming Project
PixiePal is a simple pixel art style drawing tool for drawing tiles for RPG games
JavaFX layouts: https://docs.oracle.com/javafx/2/layout/builtin_layouts.htm
Stroke inside rectangle: https://stackoverflow.com/questions/40451544/javafx-setting-a-border-within-a-rectangle-to-keep-the-width-and-height
Expand All @@ -8,6 +11,7 @@
Singleton: https://www.geeksforgeeks.org/singleton-class-java/
File handling: https://docs.oracle.com/javafx/2/ui_controls/file-chooser.htm
File IO: https://www.tutorialspoint.com/java/java_files_io.htm
File IO: https://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/Files.html
*/

import javafx.application.Application;
Expand All @@ -24,7 +28,7 @@ public class PixiePal extends Application {
private final int padding = 100;
private final int stageWidth = workSpaceSize + toolBarSize;
private final int stageHeight = workSpaceSize + padding;
private int bitStyle = 64;
private int bitStyle = 32;

public static void main(String[] args) {
launch(args);
Expand All @@ -36,9 +40,17 @@ public void start(Stage stage) {

private void initializeApp(Stage stage) {
GridPane mainPane = new GridPane();
PreviewPane preview = new PreviewPane(bitStyle);
DrawingPane canvas = new DrawingPane(workSpaceSize, bitStyle, preview);
ToolPane tools = new ToolPane(canvas, preview, toolBarSize, stage);

DrawingPane canvas = DrawingPane.getInstance();
PreviewPane.getInstance().setBitStyle(bitStyle);

canvas.setBitStyle(bitStyle);
canvas.setWorkspaceSize(workSpaceSize);




ToolPane tools = new ToolPane(toolBarSize, stage);
Scene scene = new Scene(mainPane);

mainPane.setHgap(10);
Expand Down
Loading

0 comments on commit 0f87dc3

Please sign in to comment.