Added python project; fixed spelling of folder
This commit is contained in:
225
Java Projects/TileMaper/src/Controller.java
Normal file
225
Java Projects/TileMaper/src/Controller.java
Normal file
@@ -0,0 +1,225 @@
|
||||
package com.itdominator.tilemaper;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.stage.DirectoryChooser;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.scene.canvas.Canvas;
|
||||
import javafx.scene.canvas.GraphicsContext;
|
||||
import javafx.scene.SnapshotParameters;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.WritableImage;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.geometry.Rectangle2D;
|
||||
|
||||
import java.lang.Integer;
|
||||
import java.lang.Exception;
|
||||
import java.lang.Double;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.awt.image.RenderedImage;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
|
||||
public class Controller {
|
||||
// Classes
|
||||
SaveController saveController = new SaveController();
|
||||
|
||||
// FXML Stuff
|
||||
@FXML private Canvas canvas;
|
||||
@FXML private VBox tileImagesVbox;
|
||||
@FXML private ComboBox<String> imgSizeComboBox;
|
||||
@FXML private TextField offsetTextField, columnCountTextField;
|
||||
|
||||
// Generics
|
||||
private DirectoryChooser folderChooser = new DirectoryChooser();
|
||||
private FileChooser fileChooser = new FileChooser();
|
||||
private SnapshotParameters params = new SnapshotParameters();
|
||||
private GraphicsContext graphicsContext;
|
||||
private File[] fileList;
|
||||
private File directory, file;
|
||||
private ImageView imgView;
|
||||
private Image img;
|
||||
private String path = "";
|
||||
private double offset = 5.0;
|
||||
private double x = 0;
|
||||
private double y = 0;
|
||||
private int columnCount = 2;
|
||||
|
||||
|
||||
@FXML void initialize() {
|
||||
assert imgSizeComboBox != null : "fx:id=\"imgSizeComboBox\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
assert offsetTextField != null : "fx:id=\"offsetTextField\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
assert columnCountTextField != null : "fx:id=\"columnCountTextField\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
assert tileImagesVbox != null : "fx:id=\"tileImagesVbox\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
assert canvas != null : "fx:id=\"canvas\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
|
||||
params.setFill(Color.TRANSPARENT);
|
||||
graphicsContext = canvas.getGraphicsContext2D();
|
||||
}
|
||||
|
||||
@FXML void importFromDir(ActionEvent event) {
|
||||
try {
|
||||
Stage stage = new Stage();
|
||||
directory = folderChooser.showDialog(stage);
|
||||
fileList = directory.listFiles();
|
||||
} catch (Exception e) { return; }
|
||||
|
||||
tileImagesVbox.getChildren().clear();
|
||||
|
||||
for (int i = 0; i < fileList.length; i++) {
|
||||
path = "file://" + fileList[i];
|
||||
if (path.toLowerCase().matches("^.*?(png|jpg|jpeg|gif).*$")) {
|
||||
img = new Image(path);
|
||||
imgView = new ImageView(img);
|
||||
imgView.setFitWidth(128);
|
||||
imgView.setFitHeight(128);
|
||||
tileImagesVbox.getChildren().add(imgView);
|
||||
}
|
||||
}
|
||||
|
||||
drawImgsToCanvas(fileList);
|
||||
}
|
||||
|
||||
@FXML void importFromFile(ActionEvent event) {
|
||||
try {
|
||||
Stage stage = new Stage();
|
||||
file = fileChooser.showOpenDialog(stage);
|
||||
} catch (Exception e) { return; }
|
||||
|
||||
tileImagesVbox.getChildren().clear();
|
||||
img = new Image("file://" + file);
|
||||
graphicsContext.clearRect(0.0, 0.0, img.getWidth(), img.getHeight()); // Clear rectangle
|
||||
setNewCanvasSize(img.getWidth(), img.getHeight());
|
||||
drawImage(0.0, 0.0, img.getWidth(), img.getHeight());
|
||||
}
|
||||
|
||||
@FXML void saveCanvas(ActionEvent event) {
|
||||
saveController.saveCanvas(canvas, params);
|
||||
}
|
||||
|
||||
@FXML void saveTilesToImages(ActionEvent event) {
|
||||
saveController.saveTilesToImages(canvas, params, tileImagesVbox);
|
||||
}
|
||||
|
||||
@FXML void splitIntoTiles(ActionEvent event) {
|
||||
drawToImgs();
|
||||
}
|
||||
|
||||
@FXML void updateTileSize(ActionEvent event) {
|
||||
if (fileList != null) {
|
||||
drawImgsToCanvas(fileList);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML void setOffset(KeyEvent event) {
|
||||
try {
|
||||
offset = Double.parseDouble(offsetTextField.getText());
|
||||
drawImgsToCanvas(fileList);
|
||||
} catch (Exception e) { return; }
|
||||
|
||||
System.out.println("Offset value set to: " + offset);
|
||||
}
|
||||
|
||||
@FXML void setColumnCount(KeyEvent event) {
|
||||
try {
|
||||
columnCount = Integer.parseInt(columnCountTextField.getText());
|
||||
drawImgsToCanvas(fileList);
|
||||
} catch (Exception e) { return; }
|
||||
|
||||
System.out.println("Column count set to: " + columnCount);
|
||||
}
|
||||
|
||||
private void drawImgsToCanvas(File[] tileFiles) {
|
||||
// Initial value is offset to get proper placement.
|
||||
x = offset;
|
||||
y = offset;
|
||||
int tileSize = Integer.parseInt(imgSizeComboBox.getValue().trim());
|
||||
int fileCount = tileFiles.length;
|
||||
int counter = 0;
|
||||
double width = (tileSize * columnCount) +
|
||||
(offset * (columnCount + 1));
|
||||
double height = ((fileCount / columnCount ) * tileSize) +
|
||||
(offset * ((fileCount / columnCount ) + 1));
|
||||
|
||||
// Make sure there are enough rows for the tiles
|
||||
if (fileCount % columnCount > 0) {
|
||||
height += tileSize;
|
||||
}
|
||||
|
||||
System.out.println("Canvas Width: " + width +
|
||||
"\nCanvas Height: " + height);
|
||||
|
||||
setNewCanvasSize(width, height);
|
||||
graphicsContext.clearRect(0.0, 0.0, width, height); // Clear rectangle
|
||||
|
||||
for (int i = 0; i < fileCount; i++) {
|
||||
path = "file://" + tileFiles[i];
|
||||
|
||||
if (path.toLowerCase().matches("^.*?(png|jpg|jpeg|gif).*$")) {
|
||||
img = new Image(path);
|
||||
drawImage(x, y, tileSize, tileSize);
|
||||
|
||||
x = x + offset + tileSize;
|
||||
counter++;
|
||||
|
||||
if (counter == columnCount) {
|
||||
counter = 0;
|
||||
x = offset;
|
||||
y = y + offset + tileSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawToImgs() {
|
||||
// Initial value is offset to get proper placement.
|
||||
x = offset;
|
||||
y = offset;
|
||||
int counter = 0;
|
||||
int yTileCount = -1; // Set to neg one b/c of offseting from count
|
||||
int tileSize = Integer.parseInt(imgSizeComboBox.getValue().trim());
|
||||
double width = canvas.getWidth();
|
||||
double height = canvas.getHeight();
|
||||
|
||||
tileImagesVbox.getChildren().clear();
|
||||
|
||||
// We can use col count and by way of itteration based on offset and size,
|
||||
// add to a tile count by traversing "rows" through the column size.
|
||||
while (y <= height) {
|
||||
WritableImage writableImage = new WritableImage(tileSize, tileSize);
|
||||
params.setViewport(new Rectangle2D(x, y, (double) tileSize, (double) tileSize));
|
||||
img = canvas.snapshot(params, writableImage);
|
||||
imgView = new ImageView(img);
|
||||
imgView.setFitWidth(128);
|
||||
imgView.setFitHeight(128);
|
||||
tileImagesVbox.getChildren().add(imgView);
|
||||
|
||||
x = x + offset + tileSize;
|
||||
counter++;
|
||||
|
||||
if (counter == columnCount) {
|
||||
counter = 0;
|
||||
x = offset;
|
||||
y = y + offset + tileSize;
|
||||
yTileCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawImage(double x, double y, double w, double h) {
|
||||
graphicsContext.drawImage(img, x, y, w, h); // x,y,w,h
|
||||
}
|
||||
|
||||
private void setNewCanvasSize(double w, double h) {
|
||||
canvas.setWidth(w);
|
||||
canvas.setHeight(h);
|
||||
}
|
||||
}
|
95
Java Projects/TileMaper/src/SaveController.java
Normal file
95
Java Projects/TileMaper/src/SaveController.java
Normal file
@@ -0,0 +1,95 @@
|
||||
package com.itdominator.tilemaper;
|
||||
|
||||
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.DirectoryChooser;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.scene.canvas.Canvas;
|
||||
import javafx.scene.image.WritableImage;
|
||||
import javafx.scene.SnapshotParameters;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.WritableImage;
|
||||
import javafx.embed.swing.SwingFXUtils;
|
||||
|
||||
import java.awt.image.RenderedImage;
|
||||
import java.awt.image.PixelGrabber ;
|
||||
import java.awt.Color;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.lang.InterruptedException;
|
||||
|
||||
|
||||
class SaveController {
|
||||
// Generics
|
||||
private DirectoryChooser folderChooser = new DirectoryChooser();
|
||||
private FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
|
||||
"png files (*.png)", "*.png");
|
||||
private File directory;
|
||||
|
||||
public void saveCanvas(Canvas canvas, SnapshotParameters params) {
|
||||
FileChooser fileChooser = new FileChooser();
|
||||
Stage stage = new Stage();
|
||||
int width = (int) canvas.getWidth();
|
||||
int height = (int) canvas.getHeight();
|
||||
|
||||
fileChooser.getExtensionFilters().add(extFilter);
|
||||
File file = fileChooser.showSaveDialog(stage);
|
||||
|
||||
if(file != null){
|
||||
try {
|
||||
WritableImage writableImage = new WritableImage(width, height);
|
||||
canvas.snapshot(params, writableImage);
|
||||
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
|
||||
ImageIO.write(renderedImage, "png", file);
|
||||
} catch (IOException ex) { }
|
||||
}
|
||||
}
|
||||
|
||||
public void saveTilesToImages(Canvas canvas,
|
||||
SnapshotParameters params,
|
||||
VBox tileImagesVbox) {
|
||||
int tilesCount = tileImagesVbox.getChildren().size();
|
||||
int width = (int) canvas.getWidth();
|
||||
int height = (int) canvas.getHeight();
|
||||
|
||||
try {
|
||||
Stage stage = new Stage();
|
||||
directory = folderChooser.showDialog(stage);
|
||||
System.out.println("Saving images to: " + directory);
|
||||
|
||||
for (int i = 0; i < tilesCount; i++) {
|
||||
ImageView iView = (ImageView) tileImagesVbox.getChildren().get(i);
|
||||
Image img = iView.getImage();
|
||||
RenderedImage renderedImage = SwingFXUtils.fromFXImage(img, null);
|
||||
boolean isValid = false;
|
||||
int[] pixels = new int[width * height];
|
||||
|
||||
java.awt.Image awImg = SwingFXUtils.fromFXImage(img, null);
|
||||
PixelGrabber pg = new PixelGrabber(awImg, 0, 0, width, height, pixels, 0, width);
|
||||
pg.grabPixels();
|
||||
|
||||
// checking to see if image has any pixils or is purly transparent
|
||||
for (int pixel : pixels) {
|
||||
Color color = new Color(pixel);
|
||||
|
||||
if (color.getRGB() == Color.WHITE.getRGB() ||
|
||||
color.getRGB() == Color.RED.getRGB() ||
|
||||
color.getRGB() == Color.BLUE.getRGB() ||
|
||||
color.getRGB() == Color.GREEN.getRGB()) {
|
||||
isValid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isValid == true) {
|
||||
File file = new File(directory + "/" + (i + 1) + ".png");
|
||||
ImageIO.write(renderedImage, "png", file);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) { return; }
|
||||
catch (InterruptedException ex) { return; }
|
||||
}
|
||||
}
|
36
Java Projects/TileMaper/src/TileMaper.java
Normal file
36
Java Projects/TileMaper/src/TileMaper.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.itdominator.tilemaper;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.image.Image;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
public class TileMaper extends Application {
|
||||
// Classes
|
||||
private TileMaperLogger tilemaperLogger = TileMaperLogger.getInstance();
|
||||
|
||||
@Override public void start(Stage stage) {
|
||||
try {
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("resources/TileMaper.fxml"));
|
||||
loader.setController(new Controller());
|
||||
loader.load();
|
||||
Scene scene = new Scene(loader.getRoot());
|
||||
scene.getStylesheets().add("/com/itdominator/tilemaper/resources/stylesheet.css");
|
||||
stage.setTitle("TileMaper");
|
||||
stage.setScene(scene);
|
||||
} catch (IOException startException) {
|
||||
String message = "\nTileMaper Failed to launch...\n";
|
||||
System.out.println(message + startException);
|
||||
tilemaperLogger.insertToLog(Level.SEVERE, message, startException);
|
||||
}
|
||||
stage.getIcons().add(new Image(TileMaper.class.getResourceAsStream("resources/TileMaper.png")));
|
||||
stage.setResizable(true);
|
||||
stage.show();
|
||||
}
|
||||
public static void main(String[] args) { launch(args); }
|
||||
}
|
31
Java Projects/TileMaper/src/TileMaperLogger.java
Normal file
31
Java Projects/TileMaper/src/TileMaperLogger.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.itdominator.tilemaper;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
public class TileMaperLogger {
|
||||
private static TileMaperLogger tilemaperLogger = new TileMaperLogger();
|
||||
private Logger logger = Logger.getLogger(TileMaperLogger.class.getName());
|
||||
private boolean append = false;
|
||||
|
||||
// Instance passer
|
||||
public static TileMaperLogger getInstance() { return tilemaperLogger; }
|
||||
|
||||
// Init TileMaperLogger
|
||||
private TileMaperLogger() {
|
||||
try {
|
||||
FileHandler logFile = new FileHandler("tilemaper_error.log", append);
|
||||
logger.addHandler(logFile);
|
||||
} catch (IOException e) {
|
||||
insertToLog(Level.SEVERE, "Can not access error log file...", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected void insertToLog(Level severity, String message, Exception stackTrace) {
|
||||
logger.log(severity, message, stackTrace);
|
||||
}
|
||||
}
|
8
Java Projects/TileMaper/src/unix_compile.sh
Executable file
8
Java Projects/TileMaper/src/unix_compile.sh
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
# -Xlint:unchecked
|
||||
function main() {
|
||||
javac *.java
|
||||
rm ../com/itdominator/tilemaper/*.class
|
||||
mv *.class ../com/itdominator/tilemaper/
|
||||
}
|
||||
main;
|
Reference in New Issue
Block a user