Extracted generator into abstract class for experiments
This commit is contained in:
@@ -59,7 +59,7 @@ class Color {
|
||||
return r + g + b;
|
||||
}
|
||||
|
||||
public java.awt.Color toColor() {
|
||||
java.awt.Color toColor() {
|
||||
return new java.awt.Color(r - Byte.MIN_VALUE, g - Byte.MIN_VALUE, b - Byte.MIN_VALUE);
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ class Color {
|
||||
return toString(false);
|
||||
}
|
||||
|
||||
public String toString(boolean unsigned) {
|
||||
private String toString(boolean unsigned) {
|
||||
return "(" + (unsigned ? r - Byte.MIN_VALUE : r) + "," + (unsigned ? g - Byte.MIN_VALUE : g) + "," + (unsigned ? b - Byte.MIN_VALUE : b) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package fr.klemek.marble;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
class DefaultGenerator extends Generator {
|
||||
|
||||
private final float slope;
|
||||
private final Color source;
|
||||
private final Color divergence;
|
||||
|
||||
DefaultGenerator(int width, int height) {
|
||||
this(width, height, Utils.randInt(3, 12));
|
||||
}
|
||||
|
||||
DefaultGenerator(int width, int height, int size) {
|
||||
this(width, height, size, (float) Utils.randInt(40, 60) / 100f);
|
||||
}
|
||||
|
||||
private DefaultGenerator(int width, int height, int size, float slope) {
|
||||
this(width, height, size, slope, new Color(Utils.randInt(0, 30),
|
||||
Utils.randInt(0, 30),
|
||||
Utils.randInt(0, 30)));
|
||||
}
|
||||
|
||||
private DefaultGenerator(int width, int height, int size, float slope, Color divergence) {
|
||||
this(width, height, size, slope, divergence, null);
|
||||
}
|
||||
|
||||
private DefaultGenerator(int width, int height, int size, float slope, Color divergence, Color source) {
|
||||
super(width, height, size);
|
||||
this.slope = slope;
|
||||
this.divergence = divergence;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
void generate() {
|
||||
if (seed == 0L)
|
||||
seed = ThreadLocalRandom.current().nextLong();
|
||||
System.out.println("\t\tseed : " + seed);
|
||||
System.out.println("\t\tsize : " + size);
|
||||
System.out.println("\t\tslope : " + slope);
|
||||
System.out.println("\t\tdivergence : " + divergence + " = " + divergence.sum());
|
||||
rand = new Random(seed);
|
||||
table = new Color[width2][height2];
|
||||
table[0][0] = source == null ? Color.random(rand) : source;
|
||||
System.out.println("\t\tsource : " + source);
|
||||
for (int y = 0; y < height2; y++)
|
||||
generateLine(y);
|
||||
}
|
||||
|
||||
private void generateLine(int y) {
|
||||
for (int x = 0; x < width2; x++) {
|
||||
Color div = divergence.diverge(rand);
|
||||
if (x > 0 && y == 0) {
|
||||
table[x][y] = Color.add(table[x - 1][y], div);
|
||||
} else if (x == 0 && y > 0) {
|
||||
table[x][y] = Color.add(table[x][y - 1], div);
|
||||
} else if (x > 0 && y > 0) {
|
||||
table[x][y] = Color.add(new float[]{slope, 1f - slope, 1f}, new Color[]{table[x][y - 1], table[x - 1][y], div});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,52 +1,27 @@
|
||||
package fr.klemek.marble;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
class Generator {
|
||||
abstract class Generator {
|
||||
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final int width2;
|
||||
private final int height2;
|
||||
private final int size;
|
||||
final int width2;
|
||||
final int height2;
|
||||
final int size;
|
||||
|
||||
private final float slope;
|
||||
private final Color source;
|
||||
private final Color divergence;
|
||||
long seed = 0L;
|
||||
|
||||
private long seed = 0L;
|
||||
|
||||
private Color[][] table;
|
||||
private Random rand;
|
||||
|
||||
Generator(int width, int height) {
|
||||
this(width, height, Utils.randInt(3, 12));
|
||||
}
|
||||
Color[][] table;
|
||||
Random rand;
|
||||
|
||||
Generator(int width, int height, int size) {
|
||||
this(width, height, size, (float) Utils.randInt(40, 60) / 100f);
|
||||
}
|
||||
|
||||
Generator(int width, int height, int size, float slope) {
|
||||
this(width, height, size, slope, new Color(Utils.randInt(0, 30),
|
||||
Utils.randInt(0, 30),
|
||||
Utils.randInt(0, 30)));
|
||||
}
|
||||
|
||||
Generator(int width, int height, int size, float slope, Color divergence) {
|
||||
this(width, height, size, slope, divergence, null);
|
||||
}
|
||||
|
||||
Generator(int width, int height, int size, float slope, Color divergence, Color source) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.size = size;
|
||||
this.width2 = width % size == 0 ? (width / size) : (width / size) + 1;
|
||||
this.height2 = height % size == 0 ? (height / size) : (height / size) + 1;
|
||||
this.slope = slope;
|
||||
this.divergence = divergence;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public Color[][] getTable() {
|
||||
@@ -73,33 +48,7 @@ class Generator {
|
||||
return size;
|
||||
}
|
||||
|
||||
void generate() {
|
||||
if (seed == 0L)
|
||||
seed = ThreadLocalRandom.current().nextLong();
|
||||
System.out.println("\t\tseed : " + seed);
|
||||
System.out.println("\t\tsize : " + size);
|
||||
System.out.println("\t\tslope : " + slope);
|
||||
System.out.println("\t\tdivergence : " + divergence + " = " + divergence.sum());
|
||||
rand = new Random(seed);
|
||||
table = new Color[width2][height2];
|
||||
table[0][0] = source == null ? Color.random(rand) : source;
|
||||
System.out.println("\t\tsource : " + source);
|
||||
for (int y = 0; y < height2; y++)
|
||||
generateLine(y);
|
||||
}
|
||||
|
||||
private void generateLine(int y) {
|
||||
for (int x = 0; x < width2; x++) {
|
||||
Color div = divergence.diverge(rand);
|
||||
if (x > 0 && y == 0) {
|
||||
table[x][y] = Color.add(table[x - 1][y], div);
|
||||
} else if (x == 0 && y > 0) {
|
||||
table[x][y] = Color.add(table[x][y - 1], div);
|
||||
} else if (x > 0 && y > 0) {
|
||||
table[x][y] = Color.add(new float[]{slope, 1f - slope, 1f}, new Color[]{table[x][y - 1], table[x - 1][y], div});
|
||||
}
|
||||
}
|
||||
}
|
||||
abstract void generate();
|
||||
|
||||
byte[] getData() {
|
||||
byte[] data = new byte[width * height * 3];
|
||||
@@ -124,6 +73,15 @@ class Generator {
|
||||
return data;
|
||||
}
|
||||
|
||||
boolean saveBmp(File file) {
|
||||
byte[] fileData = ImageUtils.generateBmpFile(width, getData());
|
||||
return ImageUtils.saveBmpFile(fileData, file);
|
||||
}
|
||||
|
||||
void show() {
|
||||
new MarbleViewer(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package fr.klemek.marble;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
class LocalTests {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Generator gen = new Generator(800, 800, 2);
|
||||
DefaultGenerator gen = new DefaultGenerator(800, 800, 2);
|
||||
gen.generate();
|
||||
|
||||
new MarbleViewer(gen);
|
||||
gen.saveBmp(new File("temp.bmp"));
|
||||
gen.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.awt.*;
|
||||
|
||||
class MarbleViewer extends JFrame {
|
||||
|
||||
public MarbleViewer(Generator generator) {
|
||||
MarbleViewer(Generator generator) {
|
||||
this.setLocation(0, 0);
|
||||
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
this.setResizable(false);
|
||||
@@ -16,7 +16,7 @@ class MarbleViewer extends JFrame {
|
||||
|
||||
private class Panel extends JPanel {
|
||||
|
||||
private final Generator generator;
|
||||
private transient final Generator generator;
|
||||
|
||||
Panel(Generator generator) {
|
||||
this.generator = generator;
|
||||
|
||||
@@ -31,9 +31,7 @@ final class Utils {
|
||||
|
||||
static byte[] subArray(byte[] array, int start, int stop) {
|
||||
byte[] out = new byte[stop - start];
|
||||
for (int i = start; i < stop; i++) {
|
||||
out[i - start] = array[i];
|
||||
}
|
||||
System.arraycopy(array, start, out, 0, stop - start);
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -55,9 +53,7 @@ final class Utils {
|
||||
}
|
||||
|
||||
static void writeArray(byte[] out, byte[] data, int start, int stop, int padding) {
|
||||
for (int i = 0; i < Math.min(stop, out.length) - start; i++) {
|
||||
out[i + start] = data[i + padding];
|
||||
}
|
||||
System.arraycopy(data, padding, out, start, Math.min(stop, out.length) - start);
|
||||
}
|
||||
|
||||
static void writeArray(byte[] out, byte[] data, int start, int padding) {
|
||||
@@ -68,9 +64,9 @@ final class Utils {
|
||||
writeArray(out, data, start, 0);
|
||||
}
|
||||
|
||||
static byte[] num2bytes(int number, int nbyte) {
|
||||
byte[] b = new byte[nbyte];
|
||||
for (int i = 0; i < nbyte; i++) {
|
||||
static byte[] num2bytes(int number, int nByte) {
|
||||
byte[] b = new byte[nByte];
|
||||
for (int i = 0; i < nByte; i++) {
|
||||
b[i] = (byte) (number % 256);
|
||||
number = number / 256;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package fr.klemek.marble;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
class WallpaperGenerator {
|
||||
|
||||
@@ -45,7 +47,7 @@ class WallpaperGenerator {
|
||||
File outputFile = new File(name + ".jpg");
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
Generator gen = size == 0 ? new Generator(width, height) : new Generator(width, height, size);
|
||||
DefaultGenerator gen = size == 0 ? new DefaultGenerator(width, height) : new DefaultGenerator(width, height, size);
|
||||
long t1 = System.currentTimeMillis();
|
||||
gen.generate();
|
||||
System.out.println("\tGeneration done in " + (System.currentTimeMillis() - t1) + " ms");
|
||||
@@ -65,8 +67,12 @@ class WallpaperGenerator {
|
||||
return;
|
||||
System.out.println("\tFile converting done in " + (System.currentTimeMillis() - t1) + " ms");
|
||||
|
||||
if (bmpFile.delete())
|
||||
try {
|
||||
Files.delete(bmpFile.toPath());
|
||||
System.out.println("\tFile '" + bmpFile.getName() + "' deleted");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("Wallpaper done in " + (System.currentTimeMillis() - t0) + " ms");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user