I'm trying to do a beast program but I can not do it.
I want to redraw a pixel-by-pixel image randomly, doing this with multiple threads.
I do not handle much of Java Swing, so I'm beating myself a lot.
I did some research before posting, which meant I should use BufferedImage
for this.
I found an example of multi-processing that comes close to what I'd like to do. The difference is that it actually draws an image and does this line by line, I would like to upload it, but I could not adapt it to my need.
Here is the complete source: MultiProcessing - Image .
And here where he draws the image:
private class Runner extends Thread {
double xmin, xmax, ymin, ymax;
int maxIterations;
int[] rgb;
int[] palette;
int width, height;
int startRow, endRow;
Runner(int startRow, int endRow) {
this.startRow = startRow;
this.endRow = endRow;
width = image.getWidth();
height = image.getHeight();
rgb = new int[width];
palette = new int[256];
for (int i = 0; i < 256; i++)
palette[i] = Color.getHSBColor(i / 255F, 1, 1).getRGB();
xmin = -1.6744096740931858;
xmax = -1.674409674093473;
ymin = 4.716540768697223E-5;
ymax = 4.716540790246652E-5;
maxIterations = 10000;
}
public void run() {
try {
double x, y;
double dx, dy;
dx = (xmax - xmin) / (width - 1);
dy = (ymax - ymin) / (height - 1);
for (int row = startRow; row <= endRow; row++) {
y = ymax - dy * row;
for (int col = 0; col < width; col++) {
x = xmin + dx * col;
int count = 0;
double xx = x;
double yy = y;
while (count < maxIterations && (xx * xx + yy * yy) < 4) {
count++;
double newxx = xx * xx - yy * yy + x;
yy = 2 * xx * yy + y;
xx = newxx;
}
if (count == maxIterations)
rgb[col] = 0;
else
rgb[col] = palette[count % palette.length];
}
if (!running) {
return;
}
synchronized (image) {
image.setRGB(0, row, width, 1, rgb, 0, width);
}
display.repaint(0, row, width, 1);
}
} finally {
threadFinished();
}
}
}
How can I load my own image and redraw the pixels?
EDITED:
I tested the example below, but it does not draw pixel by pixel as the example, it looks like it is drawn in rectangles.
The way I did it. What could be wrong ?? I'm using JPanel and not Canvas.
private class Runner extends Thread {
int width, height;
Random randomi = new Random();
Random randomj = new Random();
Graphics g = image.getGraphics();
Runner(int startRow, int endRow) {
width = image.getWidth();
height = image.getHeight();
}
public void run() {
try {
for (int xx = 0; xx < (width + height); xx++) {
int i = randomi.nextInt(width);
int j = randomj.nextInt(height);
//System.out.println("i = " + i + " j = " + j);
//g.setColor(getColor(i, j));
//g.drawLine(i, j, i, j);
image.setRGB(i, j, getColor(i, j).getRGB());
display.repaint(i, j, i, j);
try {
Thread.sleep(5);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
} finally {
threadFinished();
}
}
}