updating the image
in
Programming Questions
•
2 years ago
We are building a graffiti machine as a project on our school, the idea is to load an image and draw the pixels on canvas.
I have a problem with my code, i want to draw rectangles (to indicated the pixels) in a certain pattern (from top to bottom and then from the left to the right the collums).
But the image is only refreshed when the code is executed, I want to refresh the image every time a rectangle is drawn. I am very new with processing, so can someone maybe help me with this?
The code so far:
final int maxwidth = 2000; //maxwidth of drawingcanvas in mm
final int dotsize = 30; //diameter of spray in mm
final int nPixelRow = maxwidth/dotsize; //number of pixels per row
final int imagewidth = 300;
int widthsquare = imagewidth/nPixelRow;
int [][] pixel2d = new int[imagewidth][imagewidth];
int [][] newpixel2d = new int [imagewidth][imagewidth];
int [] newpixel = new int[(imagewidth/widthsquare)*(imagewidth/widthsquare)];
int x,y,k;
PImage img;
void setup()
{
size(imagewidth, imagewidth);
img = loadImage("fontys.jpg");
image (img, 0, 0); //draw image starting at 0,0
loadPixels();
writepixels(pixel2d,pixels,width,height,1);
k = 0;
for(y=0; y<=(height-widthsquare); y=y+widthsquare)
{
for(x=0; x<=(width-widthsquare); x=x+widthsquare)
{
if(squarevalue(pixel2d,widthsquare,x,y)) newpixel[k] = 1;
else newpixel[k] = 0;
k++;
}
}
noLoop();
}
void draw()
{
writepixels(newpixel2d,newpixel,(width/widthsquare),(height/widthsquare),1);
testnewpixels(newpixel2d,width,height,widthsquare);
}
///////////////////////////
// Method for determining value of a square consisting of bundeld pixels
// pre: 2d array and coordinates of the left corner of the square
// post: value of the square
///////////////////////////
boolean squarevalue(int[][]pixel, int widthsquare, int x, int y)
{
int amountblack = 0;
int amountpixels = widthsquare * widthsquare;
for(int i = y; i<(widthsquare+y); i++)
{
for(int j = x; j<(widthsquare+x); j++)
{
if(pixel[j][i] <= color(10)) amountblack++;
}
}
if(amountblack >= amountpixels) return true;
else return false;
}
///////////////////////////
// Method for writing an array into a 2d array and vice versa
// pre: 2d array, array
// post: filled 2d array or array
///////////////////////////
void writepixels(int[][]pixel2d, int[]pixel, int width, int height, int to2d)
{
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
if(to2d==1) pixel2d[x][y] = pixel[x+(y*width)]; //fill pixelarray into a 2D array
else pixel[x+(y*width)] = pixel2d[x][y]; //fill 2Dpixelarray into original array
}
}
}
///////////////////////////
// Test method to determine if the newpixel array is filled correctly
// pre: 2d pixelarray
// post: converted image
///////////////////////////
void testnewpixels(int[][]pixel2d,int width, int height, int widthsquare)
{
int widthdraw = widthsquare;
stroke(100); //line color
for(int y=0; y<(height/widthsquare); y++)
{
for(int x=0; x<(width/widthsquare); x++)
{
if(pixel2d[x][y]==1) fill(0); //filling color: black
else fill(255); //filling color: white
rect(x*widthdraw,y*widthdraw,widthdraw,widthdraw); //draw rectangle at x*widthdraw,y*widthdraw
}
}
}
1