0

I'm trying to print a smooth curve but it seems that some of the pixels are stretched. I tried to debug this by printing the pixel array but it looked smooth. The JPanel created has width and height 800.

This is the printed pixels of the double array:

printed pixels of the double array

This is the image drawn:

image drawn

I have found this function that does the same thing and is really smooth but I want to figure out how to make this from an array of pixels.

grf.drawRoundRect(50, 50, 400, 400, 200, 200);
public void draw(){
  Graphics grf = screen.get_graphics();
  int width = 400;
  int height = 400;
  int scale = 1;
  BufferedImage red_fill = BufferedImageFactory.fill(width, height, Color.RED);
  Matrixi2d square_mask = new Soft_Square().get_filled_in_mask(width, height);
  BufferedImage hue_soft_square = square_mask.mask_image(red_fill);
  grf.drawImage(hue_soft_square, 50, 50, null);
}
public BufferedImage mask_image(BufferedImage input){
  //BufferedImage result = new BufferedImage(width + edge*2, height + edge*2, BufferedImage.TYPE_INT_ARGB);
  BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  //for(int x = edge, mask_x = 0; x < width; x++, mask_x++){
    //for(int y = edge, mask_y = 0; y < height; y++, mask_y++){
  for(int x = 0, mask_x = 0; x < width; x++, mask_x++){
    for(int y = 0, mask_y = 0; y < height; y++, mask_y++){
      if(get(mask_x, mask_y) != 0){
        int mask_alpha = get(mask_x, mask_y);
        int color = input.getRGB(x, y);
        int red = (0xFF & ( color >> 16));
        int alpha = (0xFF & (color >> 24));
        int blue = (0xFF & (color >> 0 ));
        int green = (0xFF & (color >> 8 ));
        //System.out.println("mask value: "+get(mask_x, mask_y));
        //System.out.println("initial alpha: " + alpha);
        alpha = (int)(alpha * mask_alpha / 255.0);
        int sum = ((alpha & 0xff) << 24 | (red & 0xff) << 16 | (blue & 0xff) << 8 | (green & 0xff));
        //System.out.println("alpha: " + alpha);
        //System.out.println("mask alpha: " + mask_alpha);
        result.setRGB(x, y, sum);
        //System.out.println("setting result: "+sum);
      }
    }
  }
  return result;
}

Edit: Downloading the image shows that the BufferedImage doesn't have the stretching. How do you eliminate the stretching in JPanel? Here's the BufferedImage as png

Edit: This SO-Question fixed the problem.

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
Tyler
  • 1
  • 1
  • If you are painting an image on a `JPanel` and the result is stretched, this is likely due to display scaling. Try writing the image to disk (as PNG to get the correct transparency) instead, and see if it looks smooth there. – Harald K Dec 15 '22 at 10:06
  • Would not 'see if it looks smooth there' imply he brings it back up onto the scaled screen? Well, a file on disk can also be moved to a different screen/computer. – Queeg Dec 15 '22 at 11:24
  • @HiranChaudhuri He would obviously need to look at the file in a tool that can display pixels 1:1 or somehow control the scaling, but good point! – Harald K Dec 15 '22 at 14:25
  • I think you need to set the rendering hints for the Graphics2D object. Check out: https://stackoverflow.com/a/16863758/131872 for an example. – camickr Dec 15 '22 at 15:04
  • On the image drawn I cannot see a problem with my computer. But on the printed image it seems that some pixels that should be in the lower right somehow appear in the top left. Not sure whether that could indicate where the problem stems from. – Queeg Dec 16 '22 at 08:06

0 Answers0