2
I’m trying to make a stupid little program but I’m not getting it.
I want to randomly redraw a pixel-by-pixel image, doing this with multiple threads.
I don’t know much about Java Swing, so I’m hitting myself pretty hard.
I did some research before posting, I understood I should use the BufferedImage
for this.
I found an example of multi-processing that approaches what I would like to do. The difference is that he actually draws an image and does it line by line, I’d like to upload it, but I couldn’t adapt it to my need.
Here is the full source: Multiprocessing - Image.
And here where he draws the picture:
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 upload my own image and redraw the pixels?
EDITED:
I tested as the example below, but it does not draw pixel by pixel as the example, it seems that is drawn in rectangles.
It follows the way I did. What could be wrong?? I am 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();
}
}
}
Dude, ball show. I’m gonna try to apply this to that fountain I found, simpler than I thought. With multiple threads should get super fast, I’ll test there put here again.
– Fernando A.W.
I tried as you suggested, but I didn’t get the same effect. It looks like it’s drawn by rectangles. What could be wrong? I edited my question and added the font. Thank you.
– Fernando A.W.
increase the time he falls asleep drawing each stitch, here
Thread.sleep(100)
in my right spot...– jsantos1991
I edited the code, but the logic remains the same, I only applied peter’s help.
– jsantos1991
Show Showww... I will try to apply in my example Multithread this solution.. To see how it looks.
– Fernando A.W.
It was cool guys.. Thanks for the tip.. Now I have to make each thread work with an X amount of pixels.. Because they all update everything.. But that’s another 500. Thank you.
– Fernando A.W.
yes I think the logic now is not very difficult, you just have to divide this array into several and have each thread do one of these array
– jsantos1991
Yes, I made a limiter for the number of threads selected, it was good show. Thanks for the help.
– Fernando A.W.