How do I draw an image in the shape of a parallelogram?

后端 未结 2 534
栀梦
栀梦 2020-12-22 06:25

So, I know that images can be cropped, shrunk, and expanded, but can you adapt an image into the shape of a parallelogram? I\'m using Java Swing to draw images. I was thinki

2条回答
  •  一整个雨季
    2020-12-22 06:35

    Just an example using a Java image processing framework.

    Output:

    enter image description here

    public class SkewExample extends JFrame{
        MarvinImagePlugin skew = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.transform.skew");
    
        public SkewExample(){
            super("Skew Example");
            // Layout
            setLayout(new GridLayout(6,1));
    
            // Load Image
            MarvinImage image = MarvinImageIO.loadImage("./res/chamaleon.jpg");
            skew.setAttribute("skew", "Horizontal");
    
            // Process the image multiple times with different angle.
            for(int i=1; i<=6; i++){
                add(new JLabel(new ImageIcon(skew(image, i*7).getBufferedImage())));
            }
            setSize(340,880);
            setVisible(true);
        }
        private MarvinImage skew(MarvinImage imageIn, int angle){
            skew.setAttribute("SkewAngle", angle);
            MarvinImage ret = new MarvinImage(imageIn.getWidth(),imageIn.getHeight());
            ret.fillRect(0, 0,imageIn.getWidth(),imageIn.getHeight(), new Color(238,238,238));
            ret.update();
            skew.process(imageIn, ret);
            ret.update();
            return ret;
        }
        public static void main(String[] args) {
            new SkewExample().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    

提交回复
热议问题