用Java代码编写一个程序实现一下功能 1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
时间: 2024-03-10 09:49:39 浏览: 93
毕业设计&课程设计&毕设&课设-java-java图形图象处理(LW+系统).zip
好的,以下是一个简单的Java程序,实现了你所说的这些功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class ImageEditor extends JFrame implements ActionListener {
private JLabel imageLabel;
private JFileChooser fileChooser;
private ArrayList<File> fileList;
private int currentImageIndex;
private JPanel drawingPanel;
private Graphics2D graphics;
private Point lastPoint;
public ImageEditor() {
// 初始化窗口
setTitle("Image Editor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
// 初始化菜单栏
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(this);
fileMenu.add(openItem);
JMenuItem playItem = new JMenuItem("Play");
playItem.addActionListener(this);
fileMenu.add(playItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// 初始化图片显示区域
imageLabel = new JLabel();
JScrollPane imageScrollPane = new JScrollPane(imageLabel);
add(imageScrollPane, BorderLayout.CENTER);
// 初始化绘图区域
drawingPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (graphics != null) {
g.drawImage(graphics.getDeviceConfiguration().createCompatibleImage(getWidth(), getHeight()), 0, 0, null);
graphics.setColor(Color.BLACK);
graphics.setStroke(new BasicStroke(3));
}
}
};
drawingPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
lastPoint = e.getPoint();
}
});
drawingPanel.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
graphics.drawLine(lastPoint.x, lastPoint.y, e.getX(), e.getY());
lastPoint = e.getPoint();
drawingPanel.repaint();
}
});
add(drawingPanel, BorderLayout.EAST);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "Open":
openImage();
break;
case "Play":
playImages();
break;
case "Exit":
System.exit(0);
break;
}
}
private void openImage() {
// 显示文件选择对话框
if (fileChooser == null) {
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".jpg") || f.isDirectory();
}
@Override
public String getDescription() {
return "JPEG Images (*.jpg)";
}
});
}
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// 加载图片
File selectedFile = fileChooser.getSelectedFile();
try {
Image image = javax.imageio.ImageIO.read(selectedFile);
imageLabel.setIcon(new ImageIcon(image));
currentImageIndex = fileList.indexOf(selectedFile);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private void playImages() {
// 显示文件夹选择对话框
if (fileChooser == null) {
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// 加载文件夹下的所有图片
File selectedDirectory = fileChooser.getSelectedFile();
File[] files = selectedDirectory.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".jpg");
}
});
fileList = new ArrayList<>(Arrays.asList(files));
currentImageIndex = 0;
// 播放图片
while (true) {
try {
Image image = javax.imageio.ImageIO.read(fileList.get(currentImageIndex));
imageLabel.setIcon(new ImageIcon(image));
currentImageIndex++;
if (currentImageIndex >= fileList.size()) {
currentImageIndex = 0;
}
Thread.sleep(1000);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ImageEditor editor = new ImageEditor();
editor.setVisible(true);
}
});
}
}
```
这个程序使用了Java Swing库,实现了一个基本的图像编辑器,可以打开图像文件、显示图像、进行绘图和播放图像。注意,在播放图像时,程序会一直循环播放,直到用户手动关闭程序。
阅读全文