java rotate_Graphic2D中的rotate方法应用问题

本文探讨了在Java中使用Graphics2D进行图片旋转的问题,特别是关于`g2d.rotate(Math.PI/2,200,200)`的用法。代码示例展示了如何创建一个简单的GUI,点击按钮期望实现图片逆时针旋转90度,但实际运行中图片未按预期旋转。问题可能出在旋转操作没有持久化或者坐标系统理解错误上。
摘要由CSDN通过智能技术生成

我想请教各位Graphics2D中的g2d.rotate(Math.PI/2,200,200);是不是指将图片以(200,200)为旋转中心,逆时针旋转90度?另外我写了个很小的测试程序,但是点击旋转按钮,图片怎么都不...

我想请教各位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);

}

}

不胜感激!

我已经知道了。

不过二楼的代码不符合我的要求啊,我的要求是每点击一次按钮,逆时针旋转固定角度90度。

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值