0

I am creating a very simple chart using awt.Graphics2D:

    int width = countRange, height = 200;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D ig2 = bi.createGraphics();
    //drawing some stuff

Now i want to scale in width if width exceeds maximum (i need to scale after drawing, and the drawing relies on full width=countRange):

    if (countRange > MAX_IMAGE_WIDTH) {
        float yScale = (float) MAX_IMAGE_WIDTH / countRange;
        ig2.scale(1,yScale);
    }

Image is put out as byte[]:

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        ImageIO.write(bi, "PNG", os);
    } catch (IOException e) {
        throw new IllegalStateException("Problem");
    }
    return os.toByteArray();

The problem is: the output is not scaled. It returns the image in original size. So how to scale the image after drawing and put it out?

dermoritz
  • 12,519
  • 25
  • 97
  • 185

1 Answers1

1

The Graphics2D.scale(double, double) only sets the scaling for the following drawing operations.

You need BufferedImage.getScaledInstance(width, height, hints)

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34