【问题标题】:Java(SWING) working with RotationJava(SWING)使用旋转
【发布时间】:2013-02-03 05:16:30
【问题描述】:

我有一个可以处理图片的应用程序(只是一个开头有 3 张图片的全屏应用程序),我应该可以移动和旋转这些图片。一切都很完美,直到旋转,如果我旋转一张图片,这张图片的边界将保持 hotizantal -> 所以问题是我如何在 java 中旋转组件边界?

【问题讨论】:

    标签: java swing


    【解决方案1】:

    这是我很久以前找到的一些代码:

    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    public class RotateImage {
        public static void main(String[] args) throws IOException {
            URL url = new URL("http://www.oracle.com/us/assets/im04t0-java-logo-1862786.jpg");
            BufferedImage original = ImageIO.read(url);
            GraphicsConfiguration gc = getDefaultConfiguration();
            BufferedImage rotated1 = tilt(original, -Math.PI/2, gc);
            BufferedImage rotated2 = tilt(original, +Math.PI/4, gc);
            BufferedImage rotated3 = tilt(original, Math.PI, gc);
            display(original, rotated1, rotated2, rotated3);
        }
    
        public static BufferedImage tilt(BufferedImage image, double angle, GraphicsConfiguration gc) {
            double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
            int w = image.getWidth(), h = image.getHeight();
            int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
            int transparency = image.getColorModel().getTransparency();
            BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
            Graphics2D g = result.createGraphics();
            g.translate((neww-w)/2, (newh-h)/2);
            g.rotate(angle, w/2, h/2);
            g.drawRenderedImage(image, null);
            return result;
        }
    
        public static GraphicsConfiguration getDefaultConfiguration() {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            return gd.getDefaultConfiguration();
        }
    
        public static void display(BufferedImage im1, BufferedImage im2, BufferedImage im3, BufferedImage im4) {
            JPanel cp = new JPanel(new GridLayout(2,2));
            addImage(cp, im1, "original");
            addImage(cp, im2, "rotate -PI/2");
            addImage(cp, im3, "rotate +PI/4");
            addImage(cp, im4, "rotate PI");
    
            JFrame f = new JFrame("RotateImage");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(cp);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        static void addImage(Container cp, BufferedImage im, String title) {
            JLabel lbl = new JLabel(new ImageIcon(im));
            lbl.setBorder(BorderFactory.createTitledBorder(title));
            cp.add(lbl);
        }
    }
    

    编辑:

    // BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    

    【讨论】:

    • 旋转我已经完成了,问题是旋转图像的边界->你可以在旋转+PI / 4图像中看到它被旋转但边界(黑色背景)不是......
    • @Le_Coeur,也许上面编辑的代码就是你要找的。​​span>
    猜你喜欢
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2022-11-20
    • 1970-01-01
    • 2013-09-24
    相关资源
    最近更新 更多