用Java代码编写一个程序实现一下功能 1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
时间: 2024-03-10 10:49:23 浏览: 88
以下是一个简单的Java程序,实现了上述功能:
```java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ImageEditor extends JFrame {
private BufferedImage image;
private JPanel imagePanel;
private JPanel drawPanel;
private JComboBox<String> imageSelector;
private List<Shape> shapes;
public ImageEditor() {
super("Image Editor");
// 初始化变量
shapes = new ArrayList<>();
// 创建图像选择器
File folder = new File("/path/to/folder"); // 设置图像文件夹的路径
File[] files = folder.listFiles((dir, name) -> name.endsWith(".jpg")); // 只选择 JPG 文件
String[] filenames = new String[files.length];
for (int i = 0; i < files.length; i++) {
filenames[i] = files[i].getName();
}
imageSelector = new JComboBox<>(filenames);
imageSelector.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadSelectedImage();
}
});
// 创建图像显示面板
imagePanel = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(image, 0, 0, null);
for (Shape shape : shapes) {
g2d.draw(shape);
}
}
}
};
// 创建绘图面板
drawPanel = new JPanel();
drawPanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Point2D p = new Point2D.Double(e.getX(), e.getY());
shapes.add(new Line2D.Double(p, p));
}
});
drawPanel.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
Point2D p = new Point2D.Double(e.getX(), e.getY());
Line2D line = (Line2D)shapes.get(shapes.size()-1);
line.setLine(line.getP1(), p);
imagePanel.repaint();
}
});
// 创建按钮
JButton rotateButton = new JButton("Rotate");
rotateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (image != null) {
int w = image.getWidth();
int h = image.getHeight();
AffineTransform transform = AffineTransform.getRotateInstance(Math.PI/6, w/2, h/2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
image = op.filter(image, null);
imagePanel.repaint();
}
}
});
JButton playButton = new JButton("Play");
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (File file : files) {
try {
image = ImageIO.read(file);
imagePanel.repaint();
Thread.sleep(1000); // 暂停 1 秒
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}
}
});
// 创建布局
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel controlPanel = new JPanel();
controlPanel.add(imageSelector);
controlPanel.add(rotateButton);
controlPanel.add(playButton);
contentPane.add(controlPanel, BorderLayout.NORTH);
contentPane.add(imagePanel, BorderLayout.CENTER);
contentPane.add(drawPanel, BorderLayout.SOUTH);
// 显示窗口
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setVisible(true);
}
private void loadSelectedImage() {
String filename = (String)imageSelector.getSelectedItem();
try {
image = ImageIO.read(new File("/path/to/folder/" + filename)); // 设置图像文件夹的路径
imagePanel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
shapes.clear();
imagePanel.repaint();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new ImageEditor();
}
}
```
注意替换代码中的 `/path/to/folder` 为实际的图像文件夹路径。
阅读全文