这会很长。 有很多东西被注释掉了,因为我已经尝试了很多。我仍然是一个学习者而且很糟糕。 首先:它的 Java 并且大部分已经完成。只缺少一件事。
这是文件一: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();
}
}
这是文件二: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());
}
}
这是文件三: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;
}
}
我在 v1 和 v2 开始的地方下划线
现在程序应该(并且已经)获取这个文件,并使用一些 java 魔法输出一个 GUI 窗口,该窗口在圆圈中显示带有十六进制值和(这不起作用)颜色名称的颜色。
现在如果是v1,没有名字,所以null。 但是如果它的 v2,首先是文件的 v1 versin,那就是 ACO 的工作原理,而不是具有不同标头的 v2。
脚本应该检测到并切换到 v2 阅读器。它实际上这样做但不进入 while 循环,如果我使循环始终为真,一些奇怪的、错误的、颜色会添加到输出中,而不是被替换或任何东西。现在 while 循环尝试只运行与颜色一样多的次数,因为标题中有一个值告诉我有多少种颜色。
我现在只在这个小东西上花了大约 3 个小时,做了很多尝试,删除了大部分,评论了一些。仍然不起作用。
有人对此问题有任何线索或解决方案吗?
我不明白为什么。 我已经将 ACO 文件上传到我能找到的第一个文件上传器,如果你想试试的话: https://ufile.io/5hs4kfus
感谢您的帮助,我非常感谢。
与其他人交谈后修复它
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());
}
这东西出了问题。
改成如下:
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());
}
很可能这不是很好的代码,但它完成了工作。
几乎只发布所有内容的原因是我不知道问题出在哪里。 我还对主要的、主要是视觉的东西做了一些改变。