我想请教各位Graphics2D中的
g2d.rotate(Math.PI/2, 200, 200);
是不是指将图片以(200, 200)为旋转中心,逆时针旋转90度?
另外我写了个很小的测试程序,但是点击旋转按钮,图片怎么都不旋转,请教高手是什么问题,代码如下:
//MyPanel.java
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JPanel{
private Graphics g;
private Image image;
public void paintComponent(Graphics g){
this.g = g;
image = new ImageIcon("src/test.jpg").getImage();
g.drawImage(image, 3, 4, this);
}
public Graphics getGraphic(){
return g;
}
public Image getImage(){
return image;
}
}
//DrawImage.java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class DrawImage implements ActionListener {
JFrame frame; //主窗体
MyPanel panel;
JButton rotate; //控制旋转的按钮
public DrawImage(){
frame = new JFrame("绘图Demo");
frame.setSize(400, 500);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setVisible(true);
panel = new MyPanel();
panel.setBounds(0, 0, 400, 400);
frame.getContentPane().add(panel);
rotate = new JButton("逆旋"); //每按一次逆时针旋转90度
rotate.setBounds(5, 405, 390, 90);
frame.getContentPane().add(rotate);
rotate.addActionListener(this);
}
public static void main(String[]args){
new DrawImage();
}
public void actionPerformed(ActionEvent e) {
Graphics g = panel.getGraphic();
Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.PI/2, 200, 200);
g2d.drawImage(panel.getImage(), 0, 0, 400, 400, null);
}
}
不胜感激!
慕容3067478
qq_笑_17
炎炎设计