This is going to be a long one. there are many things commented out as i have tried A LOT. im still a learner and quite bad. First: its Java and mostly done. just one thing is missing.
This is file one: AcoViewer
import gui.Window;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
public class AcoViewer {
public static void main(String[] args) throws IOException {
// AcoSwatchesReader.swatchReader("aufgaben-01/swatches/bricklink_v1.aco");
// Swatch swatch = AcoSwatchesReader.read('C:/Users/mikej/IdeaProjects/oop2_mike.spengler/aufgaben-01/swatches/bricklink_v1.aco');
String path = System.getProperty("user.dir") + "/" + args[0];
System.out.println(path);
// System.out.println("---");
// System.out.println(path);
// System.out.println("---");
var returnlist = AcoSwatchesReader.read(Path.of(path));
// "C:/Users/mikej/IdeaProjects/oop2_mike.spengler/aufgaben-01/swatches/bricklink_v2.aco"
int count = 0;
//String[] hexColors = swatch.getHexColors();
//werte von AcoSwatchReader erhalten und dann nutzen.
var width = 1200;
var height = 950;
Window window = new Window("ACOViewer_v1", width, height);
// Up next, just some ways to keep the window tidy
int cX = 40;
int cY = 40;
int rad = 30;
int plusX = 250;
int plusY = 70;
// get color from Swatches one by one
// calculate decima value out of hex value
// extract respective red green and blue value
// color the circles as needed
// add name of color
// add hex value of color
for (Swatch i:returnlist) {
// Coolor color = i.getColor
// String nameColor = i.getName
// take apart color into different values here
String red1 = "00";
String green1 = "AB";
String blue1 = "00";
//convert HEX to Decimal
int decimalRed = Integer.parseInt(red1, 16);
int decimalGreen = Integer.parseInt(green1, 16);
int decimalBlue = Integer.parseInt(blue1, 16);
// String hexValue = "1A";
// int decimalValue = Integer.parseInt(hexValue, 16);
// System.out.println("Decimal Value: " + decimalValue);
// System.out.println(i);
var color = i.getColor();
int red = i.getColor().getRed();
int green = i.getColor().getGreen();
int blue = i.getColor().getBlue();
String redHex = Integer.toHexString(red);
if (i.getColor().getRed() <= 15){
redHex = "0" + Integer.toHexString(red);
}
String greenHex = Integer.toHexString(green);
if (i.getColor().getGreen() <= 15){
greenHex = "0" + Integer.toHexString(green);
}
String blueHex = Integer.toHexString(blue);
if (i.getColor().getBlue() <= 15){
blueHex = "0" + Integer.toHexString(blue);
}
String fullHex = "#" + redHex + greenHex + blueHex;
// String colorToHex = Integer.toHexString(color);
// window.setColor(red, green, blue);
window.setColor(11, 11, 11);
window.fillCircle(cX, cY, rad+2);
//add name of color here aswell as the whole HEX color value.
//if (nameColor.equals "null"){
// Add window creation here without name of color
// } else {
// Add window creation with name of color here
// }
String name = "Null";
try {
if (i.getName().equals(null)) {
name = "No name given";
} else {
name = i.getName();
}
} catch (NullPointerException e){
name = "No name given";
}
window.setColor(red, green, blue);
window.fillCircle(cX, cY, rad);
window.setFontSize(15);
window.setColor(0,0,0);
window.drawString(name, cX + 35, cY -10);
window.drawString(fullHex, cX + 35, cY +10);
cY += plusY;
if (cY >= 850){
cX +=plusX;
cY = 40;
}
}
// do {
// window.setColor(red, green, blue);
// window.fillCircle(cX, cY, rad);
// window.drawString("Test", cX + 30, cY);
// cY += plusY;
// if (cY >= 850){
// cX +=plusX;
// cY = 40;
// }
// count ++;
// } while (count < nbrColor);
// String hexString = "FF"; // replace with your hexadecimal string
// int decimalValue = Integer.parseInt(hexString, 16);
// System.out.println(decimalValue); // output: 255
// window.setColor(140, 140, 140);
// window.fillCircle(cX, cY, rad);
//
// window.setColor(230, 220, 200);
// window.fillCircle(40, 110, 30);
//
// window.setColor(120, 160, 230);
// window.fillCircle(40, 180, 30);
//
// window.setColor(220, 100, 50);
// window.fillCircle(40, 250, 30);
// Runtime r = Runtime.getRuntime();
// System.out.println(r);
window.open();
window.waitUntilClosed();
}
}
So this is file two: AcoSwatchReader
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class AcoSwatchesReader {
public static List<Swatch> read(Path path) throws IOException {
try (InputStream inputStream = Files.newInputStream(path)) {
return read(inputStream);
} catch (IOException e) {
throw e;
}
}
public static List<Swatch> read(InputStream inputStream) throws IOException {
// ignore first two words, they are just the file version
var version = readNextWord(inputStream);
System.out.println("--**" + version + "**--");
var returnList = new ArrayList<Swatch>();
if(version == 1) {
System.out.println("--V1--");
returnList = readVersion1(inputStream);
} else if(version == 2) {
System.out.println("--V2--");
returnList = readVersion2(inputStream);
} else {
throw new IOException("Unknown file version: " + version);
}
return returnList;
}
private static ArrayList<Swatch> readVersion1(InputStream inputStream) throws IOException {
var returnList = new ArrayList<Swatch>();
var anzFarben = readNextWord(inputStream);
System.out.println(anzFarben);
var count = 0;
while (true) {
try {
// check if first word is empty (v1 continues, or if it is v2 header)
var firstWord = readNextWord(inputStream);
// 2 means header of v2
if(firstWord == 2) {
System.out.println("--V2--");
// skip remaining header
readNextWord(inputStream);
var v2List = readVersion2(inputStream);
returnList.addAll(v2List);
return returnList;
}
var red = readNextByte(inputStream);
readNextByte(inputStream);
var green = readNextByte(inputStream);
readNextByte(inputStream);
var blue = readNextByte(inputStream);
readNextByte(inputStream);
// ignore last word
readNextWord(inputStream);
var swatch = new Swatch(new Color(red, green, blue), null);
returnList.add(swatch);
count ++;
// check
} catch (EOFException e) {
// End of file reached
break;
} catch (IOException e) {
throw e;
}
}
return returnList;
}
private static ArrayList<Swatch> readVersion2(InputStream inputStream) throws IOException {
var returnList = new ArrayList<Swatch>();
var anzFarben = readNextByte(inputStream);
var count = 0;
while (count < anzFarben) {
try {
// System.out.println("bla");
// skip farbraum
readNextWord(inputStream);
// always skip second byte in word, because colors are stored twice
var red = readNextByte(inputStream);
readNextByte(inputStream);
var green = readNextByte(inputStream);
readNextByte(inputStream);
var blue = readNextByte(inputStream);
readNextByte(inputStream);
// skip next two blanks
readNextWord(inputStream);
readNextWord(inputStream);
// readNextWord(inputStream);
// skip name length
var nameLenght = readNextWord(inputStream);
var count2 = 0;
System.out.println("-.-.-.");
System.out.println(nameLenght);
System.out.println("-.-.-.");
// String name = "";
// while (count2 < nameLenght){
// name += readNextWord(inputStream);
// readNextWord(inputStream);
// count ++;
// }
// System.out.println(name);
var name = readNullTerminatedString(inputStream);
// String name = readNullTerminatedString(inputStream, StandardCharsets.UTF_8);
// readNextWord(inputStream);
// System.out.println(name);
var swatch = new Swatch(new Color(red, green, blue), name);
returnList.add(swatch);
count ++;
} catch (EOFException e) {
// End of file reached
break;
} catch (IOException e) {
throw e;
}
}
return returnList;
}
private static int readNextWord(InputStream inputStream) throws EOFException, IOException {
int b1 = inputStream.read();
int b2 = inputStream.read();
if (b1 == -1 || b2 == -1) {
throw new EOFException();
}
return (b1 << 8) | b2;
}
public static String readNullTerminatedString(InputStream inputStream, Charset charset) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int nextByte;
while ((nextByte = inputStream.read()) != 0) {
if (nextByte == -1) {
throw new EOFException();
}
outputStream.write(nextByte);
}
return outputStream.toString(charset);
}
private static int readNextByte(InputStream inputStream) throws EOFException, IOException {
int b = inputStream.read();
if (b == -1) {
throw new EOFException();
}
return b;
}
public static String readNullTerminatedString(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int nextByte;
while ((nextByte = inputStream.read()) != 0) {
if (nextByte == -1) {
throw new EOFException();
}
outputStream.write(nextByte);
}
return outputStream.toString(StandardCharsets.UTF_8.name());
}
}
This is file three: Swatch
import java.awt.*;
public class Swatch {
final Color color;
final String name;
public Swatch(Color color, String name) {
this.color = color;
this.name = name;
}
public Color getColor() {
return color;
}
public String getName() {
return name;
}
}
Then i have ACO files which look something like this:
The real file looks like this:
I underlined where v1 and where v2 begins
Now the programm shall (and already does) takes this file and with some java magic outputs a GUI window which displays the colors in circles with the hex value and (this does not work) the name of the color.
Now if its v1, there is no name, so null. But if its v2, first comes the v1 versin of the file, thats just ow ACO works, and than v2 with a distinct header.
The script should detect that and switch to the v2 reader. it actually does that but does not enter the while loop, if i make the loop always true, some weird, wrong, colors get added to the output, not replaced or anything. Right now the while loop tries to only run as many times as there are colors as there is a value in the header telling me how many colors there are.
i spent about 3 hours just on this little thing now, made WAY to many tries, deleted most, commented outsome. does still not work.
Does anyone have any clue or solution to this problem?
I dont get why. I have uploaded the ACO file to the first fileuploader i could find if you want to try it out: https://ufile.io/5hs4kfus
Thanks for the help, i appreciate it A LOT.
Fixed it after talking to others
public static String readNullTerminatedString(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int nextByte;
while ((nextByte = inputStream.read()) != 0) {
if (nextByte == -1) {
throw new EOFException();
}
outputStream.write(nextByte);
}
return outputStream.toString(StandardCharsets.UTF_8.name());
}
This thing made problems.
Changed it to the following:
public static String readNullTerminatedString(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int nextByte;
int nextByteTwo = 0;
while ((nextByte = readNextWord(inputStream)) != 0 || (nextByteTwo = readNextByte(inputStream)) + nextByte != 0 && (nextByteTwo = readNextByte(inputStream)) + nextByte != -1) {
if (nextByte == -1) {
throw new EOFException();
}
outputStream.write(nextByte);
}
return outputStream.toString(StandardCharsets.UTF_8.name());
}
Most likely this isn't very nice code but it does the job.
Reason for posting just everything pretty much was me not having a clue where the problem laid. I also made some changes to the main, mostly visual things.