在我的这个Java程序当中,我已经实现了缩小的功能,那么我应该如何修改才能实现旋转图片的功能

import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import javax.swing.*;public class lianxi13 extends JFrame{JPanel n;JLabel n1;JLabel n2;ImageIcon n3;JButton n4,n5;public lianxi13(){ super("fds"); setSize(286,168); this.setVisible(true); n=new JPanel(); n.setLayout(new BorderLayout()); n3=new ImageIcon("D://8.jpg"); n1=new JLabel(n3); n2=new JLabel("带图标的组件"); n.add(n1,BorderLayout.WEST); n.add(n2,BorderLayout.EAST); n4=new JButton("缩小"); n5=new JButton("旋转"); n.add(n4); n.add(n5); this.add(n); n4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { BufferedImage bi = new BufferedImage((int)(n3.getIconWidth()*0.8),(int)(n3.getIconHeight()*0.8),BufferedImage.TYPE_3BYTE_BGR); bi.getGraphics().drawImage(n3.getImage(), 0, 0, (int)(n3.getIconWidth()*0.8), (int)(n3.getIconHeight()*0.8),null); bi.getGraphics().dispose(); n3.setImage(bi); repaint(); } catch (Exception e1) { e1.printStackTrace(); } } });} public static void main(String[] args) { new lianxi13(); }}
2026年09月29日 02:56
有3个网友回答
网友(1):

Graphics对象转换成Graphics2D,然后里面有一个rotate方法,里面可以设置旋转角度。

网友(2):

旋转应该需要线程吧 隔一段时间做移动并刷新界面

网友(3):

可以使用 getGraphics() 的画布旋转,注意是弧度