Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>+1 to MadProgrammers comment and link.</p> <p>Using the method from link (edited slightly to omit use of <code>GraphicsConfiguration</code>, <code>drawRenderImage(..)</code> and changed variable names):</p> <pre><code>//https://stackoverflow.com/questions/4156518/rotate-an-image-in-java public static BufferedImage createTransformedImage(BufferedImage image, double angle) { double sin = Math.abs(Math.sin(angle)); double cos = Math.abs(Math.cos(angle)); int originalWidth = image.getWidth(); int originalHeight = image.getHeight(); 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>Here is an example:</p> <p><img src="https://i.stack.imgur.com/0Xf41.png" alt="enter image description here"></p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class RotateImage { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MyRotatableImage(createImage(), -45)); frame.pack(); frame.setVisible(true); } }); } public static BufferedImage createImage() { BufferedImage img = new BufferedImage(100, 50, BufferedImage.TRANSLUCENT); Graphics2D g2d = img.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, img.getWidth(), img.getHeight()); g2d.setColor(Color.BLACK); g2d.setFont(new Font("Calibri", Font.BOLD, 20)); FontMetrics fm = g2d.getFontMetrics(); String text = "Hello world"; int textWidth = fm.stringWidth(text); g2d.drawString(text, (img.getWidth() / 2) - textWidth / 2, img.getHeight() / 2); g2d.dispose(); return img; } } class MyRotatableImage extends JPanel { private BufferedImage transformedImage; public MyRotatableImage(BufferedImage img, int angle) { transformedImage = createTransformedImage(img, angle); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawImage(transformedImage, 0, 0, null); } @Override public Dimension getPreferredSize() { return new Dimension(transformedImage.getWidth(), transformedImage.getHeight()); } //https://stackoverflow.com/questions/4156518/rotate-an-image-in-java public static BufferedImage createTransformedImage(BufferedImage image, double angle) { double sin = Math.abs(Math.sin(angle)); double cos = Math.abs(Math.cos(angle)); int originalWidth = image.getWidth(); int originalHeight = image.getHeight(); 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><strong>Reference:</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/4156518/rotate-an-image-in-java">Rotate an image in java</a></li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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