编写一个简单 JFrame 图像处理界面,实现图像的旋转、倾斜、水平垂直拼 接和放大缩小等功能

编写一个简单 JFrame 图像处理界面,实现图像的旋转、倾斜、水平垂直拼 接和放大缩小等功能,第1张

有些小bug,仅供参考

package work08.imageProcessing;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class PartImage extends JFrame {
    private BufferedImage img;
    private int dx1, dy1, dx2, dy2; //目标矩形坐标
    private int sx1, sy1, sx2, sy2; //源矩形坐标
    private int width, height; // 图片宽高
    private JButton vBtn = null; // 垂直翻转按钮
    private JButton hBtn = null; // 水平翻转按钮
    private JButton rBtn = null; // 旋转按钮
    private CanvasPanel canvasPanel = null;
    private int nums=0;

    private JSlider jSlider;

    public PartImage() {
        try {
            img = ImageIO.read(new File("imgs/img13.jpg"));// 读取图片文件
        } catch (IOException e) {
            e.printStackTrace();
        }

        dx2 = sx2 = width = img.getWidth(); // 初始化图像大小
        dy2 = sy2 = height = img.getHeight();

        vBtn = new JButton("垂直翻转");
        hBtn = new JButton("水平翻转");
        rBtn = new JButton("旋转");

        jSlider = new JSlider();
        jSlider.setMaximum(1000);
        jSlider.setValue(100);
        jSlider.setMinimum(1);
        jSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                canvasPanel.repaint();
            }
        });

        JPanel bottom = new JPanel();
        bottom.add(hBtn);
        bottom.add(vBtn);
        bottom.add(rBtn);
        bottom.add(jSlider);

        Container c = getContentPane();
        c.add(bottom, BorderLayout.SOUTH);
        canvasPanel = new CanvasPanel();
        c.add(canvasPanel, BorderLayout.CENTER);


//        JPanel center = new JPanel();
//        center.setLayout(new BorderLayout());
//        center.add(jSlider, BorderLayout.SOUTH);
//        center.add(canvasPanel, BorderLayout.CENTER);
//        setContentPane(center);

        addListener();

        setBounds(100, 100, 1000, 900); // 设置窗体大小和位置
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭模式
        setTitle("图片翻转"); // 设置窗体标题
    }

    private void addListener() {
        vBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sy1 = Math.abs(sy1 - height);// 纵坐标互换
                sy2 = Math.abs(sy2 - height);
                canvasPanel.repaint();
            }
        });
        hBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sx1 = Math.abs(sx1 - width);// 横坐标互换
                sx2 = Math.abs(sx2 - width);
                canvasPanel.repaint();
            }
        });
        rBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                nums++;
                canvasPanel.repaint();
            }
        });
        jSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                canvasPanel.repaint();
            }
        });
    }

    class CanvasPanel extends Canvas {
        public void paint(Graphics g) {
//            g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, this);// 绘制指定大小的图片

            int newW = 0, newH = 0;
            float value = jSlider.getValue();// 滑块组件的取值
            newW = (int) (width * value / 100);// 计算图片放大后的宽度
            newH = (int) (height * value / 100);// 计算图片放大后的高度
            g.drawImage(img, 0, 0, newW, newH, this);// 绘制指定大小的图片


            Graphics2D g2 = (Graphics2D) g;
            g2.rotate(Math.toRadians(nums)); // 图像旋转方法,旋转5度
//            g2.drawImage(img, dx1, dy1, dx2, dy2, sx1,sy1,sx2,sy2,this);  //图像旋转
        }
    }

    public static void main(String[] args) {
        new PartImage().setVisible(true);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/719622.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存