-2

can anyone help me and tell me how to create a gray scale image where one pixel of the image is shown as a square with the size 2 x 2?

I already searched for help and found this how to create a gray scale image from pixel values using java but i don't know how to create a gray scale with the information that one pixel is shown as a square with the size 2 x 2.

thanks!

Ravipati Praveen
  • 446
  • 1
  • 7
  • 28
goldenone
  • 11
  • 3

1 Answers1

0

to create a picture where each pixel has the size 2x2 you must either scale the image (factor 2) for display only... or if you want to create a image you have to do it manually and create an image and draw with scale factor 2 on it

int[] pixels = ... //we already have our gray scale pixels here
int widthOriginal = ... //size of original image
int heightOriginal = ...

//let's create an buffered Image twice the size
BufferedImage img = 
    new BufferedImage(2*widthOriginal, 2*heightOriginal, BufferedImage.TYPE_4BYTE_ABGR);

//we paint on the buffered image's graphic...
Graphics gr = img.getGraphics();

//we draw all pixels on the graphic
for(int y = 0; y < heightOriginal; y ++){
    for(int x = 0; x < widthOriginal; x ++){
        int index = y*widthOriginal + x;
        int gray = pixels[index];

        //to draw we first set the color
        gr.setColor(new Color(gray));

        //then draw the pixel
        gr.drawRect(2*x, 2*y,2,2); //draw a 2x2 pixel instead of a 1x1 pixel
    }
}

uhm - honestly i've written that code entirely out of my head, so there may be some minor compilation problems... but the technique is explained properly...

Martin Frank
  • 3,445
  • 1
  • 27
  • 47