Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/com/editor/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.editor;

import javax.swing.SwingUtilities;

public class App {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MapEditor().setVisible(true);
}
});

}

}
13 changes: 13 additions & 0 deletions src/com/editor/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.editor;

public class Constants {

public static final int TILE_SIZE = 32;

private static final int SCREEN_COL = 28;
private static final int SCREEN_ROW = 20;

public static final int SCREEN_WIDTH = TILE_SIZE * SCREEN_COL;
public static final int SCREEN_HEIGHT = TILE_SIZE * SCREEN_ROW;

}
81 changes: 81 additions & 0 deletions src/com/editor/FileManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.editor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.ImageIcon;

public class FileManager {
public static void savePalette(HashMap<Integer, ImageIcon> imageMap, String filePath) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (Integer key : imageMap.keySet()) {
ImageIcon icon = imageMap.get(key);
String path = icon.getDescription();
writer.write(key + ", " + path);
writer.newLine();
}
}
}

public static HashMap<Integer, ImageIcon> loadPalette(String filePath) throws IOException {
HashMap<Integer, ImageIcon> imageMap = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(", ");
int key = Integer.parseInt(parts[0]);
String path = parts[1];
ImageIcon icon = new ImageIcon(path);
icon.setDescription(path);
imageMap.put(key, icon);
}
}
return imageMap;
}

public static void saveMap(int[][] map, String filePath) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (int j = 0; j < map[0].length; j++) {
for (int i = 0; i < map.length; i++) {
writer.write(map[i][j] + " ");
}
writer.newLine();
}
}
}

public static int[][] loadMap(String filePath) throws IOException {
int rows = 0;
int cols = 0;

// First pass to determine the size of the map
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
rows++;
String[] values = line.split(" ");
cols = Math.max(cols, values.length);
}
}

int[][] map = new int[rows][cols];

// Second pass to populate the map
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
int row = 0;
while ((line = reader.readLine()) != null) {
String[] values = line.split(" ");
for (int col = 0; col < values.length; col++) {
map[row][col] = Integer.parseInt(values[col]);
}
row++;
}
}
return map;
}
}
Loading