1

i write this codes :

BufferedImage image;
BufferedImage im = image;

    @Override
    public void paint(Graphics g) {
    try {
        image = ImageIO.read(new File("jet.png"));
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(image, 100, 100, Color.BLACK, null);
        image = rotateImage(-45);
        g2.drawImage(image, 250, 100, Color.BLACK, null);
    } catch (IOException ex) {

    }
    }

    public BufferedImage rotateImage(int degress) {

    double deg = Math.toRadians(degress);
    double xrot = image.getWidth()/2 ;
    double yrot = image.getHeight() /2;
    AffineTransform xt = AffineTransform.getRotateInstance(deg, xrot, yrot);
    AffineTransformOp op = new AffineTransformOp(xt, AffineTransformOp.TYPE_BILINEAR);
    return op.filter(image, im);

    }
}

and output is :

enter image description here

and new image size is change!!! i want to rotate this image , and its important new image size whitout change

i want some code to rotate image whitout resize image , Like this

enter image description here

Hani Nakhli
  • 351
  • 2
  • 9

2 Answers2

2

The effect shown is characteristic of AffineTransformOp#filter(): "The coordinates of the rectangle returned by getBounds2D(BufferedImage) are not necessarily the same as the coordinates of the BufferedImage returned by this method." Instead, draw the image into BufferedImage having a rotated graphics context, as shown here. Use RenderingHints, such as VALUE_ANTIALIAS_ON and VALUE_INTERPOLATION_BILINEAR, as needed.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • this is example of my code ! my code have 11 Image for draw , and i want rotate one of them – Hani Nakhli Jul 04 '15 at 04:57
  • It might be easier if you make the background transparent using one of the approaches mentioned [here](http://stackoverflow.com/a/27335373/230513). – trashgod Jul 04 '15 at 15:26
2

First of all:

  1. Don't override paint(). Custom painting is done by overriding paintComponent(...) and don't forget the super.paintComponent(...) at the start of the method.

  2. Don't do I/O in a painting method. Read the image in the constructor of the class

When you rotate a square other than 90, 180, 270, etc. degrees, the size of the square will change. So, the problem is the version of the drawImage(...) method you are using:

g2.drawImage(image, 100, 100, Color.BLACK, null);

Instead of using the drawImage() method to draw the background and the image you can paint the background and the image separately so you can control the size of the background:

//g2.drawImage(image, 100, 100, Color.BLACK, null);
g2.setColor( Color.BLACK );
g2.fillRect(100, 100, image.getWidth(), image.getHeight());
g2.drawImage(image, 100, 100, null);
image = rotateImage(-45);
g2.fillRect(250, 100, image.getWidth(), image.getHeight());
g2.drawImage(image, 250, 100, null);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • tnx for your answer i draw background to show image size ! and orginal size image is importnat Because i want to use image.getWidth() and image.getHeight() in my code for other work ! – Hani Nakhli Jul 04 '15 at 04:46