下面的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,jPanel;JLabel n1;ImageIcon n3;JButton n4,n5;public lianxi13(){ super("fds"); setSize(600,600); this.setVisible(true); n=new JPanel(); n.setLayout(new BorderLayout()); n3=new ImageIcon("D://8.jpg"); n1=new JLabel(n3); jPanel=new JPanel(); n.add(n1,BorderLayout.WEST); n.add(jPanel,BorderLayout.EAST); n4=new JButton("缩小"); n5=new JButton("扩大"); jPanel.add(n4,BorderLayout.EAST); jPanel.add(n5,BorderLayout.WEST); 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(); } } }); n5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { BufferedImage bi = new BufferedImage((int)(n3.getIconWidth()*1.2),(int)(n3.getIconHeight()*1.2),BufferedImage.TYPE_3BYTE_BGR); bi.getGraphics().drawImage(n3.getImage(), 0, 0, (int)(n3.getIconWidth()*1.2), (int)(n3.getIconHeight()*1.2),null); bi.getGraphics().dispose(); n3.setImage(bi); repaint(); } catch (Exception e1) { e1.printStackTrace(); } } });} public static void main(String[] args) { new lianxi13(); }}
2026年09月24日 07:32
有2个网友回答
网友(1):

Image的getGraphics();返回来信强制转换为Graphics2D
调用Graphics2D的rotate(角度,旋转中心x坐标,y坐标)

网友(2):

百度

BufferedImage rotateImage(final BufferedImage bufferedimage

得到过程,添加按钮、设置事件。。。。。