java实现图片的放大缩小(附完整源码)
时间: 2023-12-01 12:02:30 浏览: 101
以下是Java实现图片的放大缩小的完整源码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageZoom extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private ImagePanel imagePanel;
private JButton zoomInButton;
private JButton zoomOutButton;
private JButton openButton;
private JButton saveButton;
private BufferedImage image;
private int zoomFactor = 100;
public ImageZoom() {
super("Image Zoom");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(640, 480));
imagePanel = new ImagePanel();
add(imagePanel, BorderLayout.CENTER);
zoomInButton = new JButton("Zoom In");
zoomInButton.addActionListener(this);
zoomOutButton = new JButton("Zoom Out");
zoomOutButton.addActionListener(this);
openButton = new JButton("Open");
openButton.addActionListener(this);
saveButton = new JButton("Save");
saveButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(zoomInButton);
buttonPanel.add(zoomOutButton);
buttonPanel.add(openButton);
buttonPanel.add(saveButton);
buttonPanel.setBackground(Color.WHITE);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new ImageZoom();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == zoomInButton) {
zoomFactor += 10;
} else if (e.getSource() == zoomOutButton) {
zoomFactor -= 10;
} else if (e.getSource() == openButton) {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
image = ImageIO.read(fileChooser.getSelectedFile());
zoomFactor = 100;
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else if (e.getSource() == saveButton) {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
ImageIO.write(image, "png", fileChooser.getSelectedFile());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
imagePanel.repaint();
}
private class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
int width = image.getWidth() * zoomFactor / 100;
int height = image.getHeight() * zoomFactor / 100;
int x = (getWidth() - width) / 2;
int y = (getHeight() - height) / 2;
g2d.drawImage(image, x, y, width, height, null);
g2d.dispose();
}
}
}
}
```
实现思路:
1. 创建一个窗口,并在窗口中添加一个按钮面板和一个图片面板;
2. 实现按钮的点击事件,点击“放大”按钮时,将缩放系数增加10%,点击“缩小”按钮时,将缩放系数减少10%,点击“打开”按钮时,弹出文件选择对话框,选择图片文件后将图片加载到内存中,缩放系数设为100%,点击“保存”按钮时,弹出文件保存对话框,将图片保存为PNG格式;
3. 在图片面板的`paintComponent()`方法中绘制图片,使用`Graphics2D`对象进行绘制,设置渲染提示为双线性插值,根据缩放系数计算图片的宽度和高度,并居中显示。
实现细节:
1. 加载图片时,需要使用`ImageIO.read()`方法将图片文件读入内存;
2. 保存图片时,需要使用`ImageIO.write()`方法将图片保存到文件中;
3. 绘制图片时,需要使用`Graphics2D`对象进行绘制,并设置渲染提示为双线性插值,以获得更好的视觉效果;
4. 计算显示位置时,需要将图片居中显示。
阅读全文