2013-05-04 15:54:40Morris

[JAVA][作業] 點擊旋轉九十度



讀一個圖片檔,並且以點擊的方式,將圖片做旋轉九十度。


package ce1002.A5.s100502205;

import java.io.File;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;

public class A51 extends JFrame {
    private JButton btn;
    private BufferedImage buffer;

    public BufferedImage rotate90(BufferedImage source) {
        int width, height;
        width = source.getWidth();
        height = source.getHeight();
        BufferedImage out = new BufferedImage(height, width, source.getType());
        for(int j = 0; j < height; j++)
            for(int i = width-1; i >= 0; i--)
                out.setRGB(j, width-i-1, buffer.getRGB(i, j));
        return out;
    }

    public A51() {
        this.setTitle("A51");
        this.setSize(800, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            btn = new JButton();
            buffer = ImageIO.read(new File("test2.jpg"));
        } catch (Exception e) {

        }

        btn.setIcon(new ImageIcon(buffer));
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buffer = rotate90(buffer);
                btn.setIcon(new ImageIcon(buffer));
            }
        });

        this.add(btn);

        this.setVisible(true);
        this.setResizable(false);
    }

    public static void main(String[] args) {
        new A51();
    }
}

這裡給一個使用 rotate 的 method 做法,
但是無法做出長方形的旋轉目前還不太會調用。


package ce1002.A5.s100502205;

import java.io.File;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;

public class A5 extends JFrame {
    private JButton btn;
    private BufferedImage buffer;

    public BufferedImage rotate90(BufferedImage source) { // only width == height
        int width, height;
        width = source.getWidth();
        height = source.getHeight();
        BufferedImage out = new BufferedImage(height, width, source.getType());
        Graphics2D g2d = out.createGraphics();
        g2d.rotate(Math.toRadians(90), height/2, width/2);
        g2d.drawImage(source, 0, 0, null);
        return out;
    }

    public A5() {
        this.setTitle("A5");
        this.setSize(450, 350);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            btn = new JButton();
            buffer = ImageIO.read(new File("test.jpg"));
        } catch (Exception e) {

        }

        btn.setIcon(new ImageIcon(buffer));
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buffer = rotate90(buffer);
                btn.setIcon(new ImageIcon(buffer));
            }
        });

        this.add(btn);

        this.setVisible(true);
        this.setResizable(false);
    }

    public static void main(String[] args) {
        new A5();
    }
}