Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK I tried the sample <strong>David Kroukamp</strong> showed me. Thanks, David, you pointed me to right direction. I think it is <a href="https://stackoverflow.com/a/14301840/592704">a really good snippet</a> to lean back on. </p> <pre><code>public static BufferedImage rotateImage(Image image, int angle) { double sin = Math.abs(Math.sin(angle)); double cos = Math.abs(Math.cos(angle)); int originalWidth = image.getWidth(null); int originalHeight = image.getHeight(null); int newWidth = (int) Math.floor(originalWidth * cos + originalHeight * sin); int newHeight = (int) Math.floor(originalHeight * cos + originalWidth * sin); BufferedImage rotatedBI = new BufferedImage(newWidth, newHeight, BufferedImage.TRANSLUCENT); Graphics2D g2d = rotatedBI.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.translate((newWidth - originalWidth) / 2, (newHeight - originalHeight) / 2); g2d.rotate(angle, originalWidth / 2, originalHeight / 2); g2d.drawImage(image, 0, 0, null); g2d.dispose(); return rotatedBI; } </code></pre> <p><a href="https://i.stack.imgur.com/0Xf41.png" rel="nofollow noreferrer">David Kroukamp's result image preview</a></p> <p>I tested the snippet and the result image + its component's scale were really dynamic changed. <em>But the result component width I've got has been always a little bit wider than it's inner image was :S</em></p> <p>For example here is my version of an image rotated in 45 degrees angle</p> <p><img src="https://i.stack.imgur.com/2ueBr.png" alt="snippet based result image"></p> <p>... As I could find (on blue background) its width is not totally fit to surround white image ... Actually I was looking for some kind of <em>standard Java Image Processing rotate solution</em> but I couldn't find it :( so I had to dig deeper to the problem and at least figure out how to <strong>solve the 'wider effect'</strong> and <strong>avoid create new BufferedImage object every re-paint</strong>... </p> <p>OK, as a reault of my research, I tried to write some kind of my rotate code adaptation. Here it is : </p> <p><strong>>tested</strong></p> <pre><code>public static void rotateImage(Graphics g, Image image,int tilt,JComponent component) { // create the transform, note that the transformations happen // in reversed order (so check them backwards) AffineTransform at = new AffineTransform(); //5. modify component scale ... double sin = Math.abs(Math.sin(Math.toRadians(tilt))); double cos = Math.abs(Math.cos(Math.toRadians(tilt))); int w=image.getWidth(null); int h=image.getHeight(null); int newW=(int) Math.floor(w * cos + h * sin); int newH=(int) Math.floor(h * cos + w * sin); component.setSize(newW, newH); int width=component.getWidth(); int height=component.getHeight(); // 4. translate it to the center of the component at.translate(width / 2, height / 2); // 3. do the actual rotation at.rotate(Math.toRadians(tilt)); // 2. just a scale because this image is big // at.scale(1, 1); // 1. translate the object so that you rotate it around the // center (easier :)) at.translate(-image.getWidth(null)/2, -image.getHeight(null)/2); // draw the image ((Graphics2D) g).drawImage(image, at, null); } </code></pre> <p>... so the result image rotated on -30 degrees tilt looks like this </p> <p><img src="https://i.stack.imgur.com/pRYDI.png" alt="enter image description here"></p> <p>I dearly hope the tip saves ones day :)</p> <hr> <p>Thank you all for help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload