用Java代码编译1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
时间: 2024-03-05 09:51:22 浏览: 52
好的,以下是一个基于Java Swing和Java AWT的简单程序,可以实现您所需的功能:
```
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class ImageEditor extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
private JPanel imagePanel, drawPanel;
private JLabel imageLabel;
private BufferedImage currentImage, originalImage;
private Graphics2D g2d;
private int startX, startY, endX, endY;
private String currentFileName;
private Timer timer;
private File[] imageFiles;
private int currentIndex = 0;
public ImageEditor() {
super("Image Editor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLayout(new BorderLayout());
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(this);
fileMenu.add(openItem);
JMenuItem saveItem = new JMenuItem("Save");
saveItem.addActionListener(this);
fileMenu.add(saveItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
JMenuItem rotateItem = new JMenuItem("Rotate");
rotateItem.addActionListener(this);
editMenu.add(rotateItem);
menuBar.add(editMenu);
JMenu playMenu = new JMenu("Play");
JMenuItem playItem = new JMenuItem("Play");
playItem.addActionListener(this);
playMenu.add(playItem);
menuBar.add(playMenu);
setJMenuBar(menuBar);
// 创建图像面板和绘制面板
imagePanel = new JPanel();
add(imagePanel, BorderLayout.CENTER);
drawPanel = new JPanel();
drawPanel.setLayout(new BorderLayout());
drawPanel.addMouseListener(this);
drawPanel.addMouseMotionListener(this);
add(drawPanel, BorderLayout.SOUTH);
// 创建定时器
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentIndex < imageFiles.length - 1) {
currentIndex++;
} else {
currentIndex = 0;
}
loadImage(imageFiles[currentIndex]);
}
});
}
// 打开图像文件
private void openImage() {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp");
fileChooser.setFileFilter(filter);
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
loadImage(selectedFile);
}
}
// 加载图像文件到面板中
private void loadImage(File file) {
try {
currentFileName = file.getName();
currentImage = ImageIO.read(file);
originalImage = ImageIO.read(file);
imageLabel = new JLabel(new ImageIcon(currentImage));
imagePanel.removeAll();
imagePanel.add(imageLabel);
imagePanel.updateUI();
g2d = (Graphics2D) drawPanel.getGraphics();
} catch (IOException e) {
e.printStackTrace();
}
}
// 保存图像文件
private void saveImage() {
if (currentImage != null) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp");
fileChooser.setFileFilter(filter);
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
ImageIO.write(currentImage, "jpg", selectedFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 旋转图像
private void rotateImage() {
if (currentImage != null) {
double radians = Math.toRadians(90);
AffineTransform tx = new AffineTransform();
tx.rotate(radians, currentImage.getWidth() / 2, currentImage.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
currentImage = op.filter(currentImage, null);
imageLabel.setIcon(new ImageIcon(currentImage));
drawPanel.updateUI();
}
}
// 开始播放图像
private void playImages() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFolder = fileChooser.getSelectedFile();
imageFiles = selectedFolder.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".png") || name.toLowerCase().endsWith(".gif") || name.toLowerCase().endsWith(".bmp");
}
});
if (imageFiles != null && imageFiles.length > 0) {
currentIndex = 0;
loadImage(imageFiles[currentIndex]);
timer.start();
}
}
}
// 处理菜单项事件
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Open")) {
openImage();
} else if (command.equals("Save")) {
saveImage();
} else if (command.equals("Exit")) {
System.exit(0);
} else if (command.equals("Rotate")) {
rotateImage();
} else if (command.equals("Play")) {
playImages();
}
}
// 处理鼠标事件
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
if (e.getButton() == MouseEvent.BUTTON1) {
if (currentImage != null) {
g2d.setColor(Color.BLACK);
if (startX == endX && startY == endY) {
g2d.drawLine(startX, startY, endX + 1, endY + 1);
} else {
if (e.isControlDown()) {
drawPanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
g2d.draw(new Ellipse2D.Float(startX, startY, endX - startX, endY - startY));
} else {
drawPanel.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
g2d.drawRect(startX, startY, endX - startX, endY - startY);
}
}
drawPanel.updateUI();
}
} else if (e.getButton() == MouseEvent.BUTTON3) {
if (originalImage != null) {
currentImage = originalImage;
imageLabel.setIcon(new ImageIcon(currentImage));
drawPanel.updateUI();
}
}
}
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
if (e.getModifiersEx() == MouseEvent.BUTTON1_DOWN_MASK) {
if (currentImage != null) {
g2d.setColor(Color.BLACK);
if (startX == endX && startY == endY) {
g2d.drawLine(startX, startY, endX + 1, endY + 1);
} else {
if (e.isControlDown()) {
drawPanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
g2d.draw(new Ellipse2D.Float(startX, startY, endX - startX, endY - startY));
} else {
drawPanel.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
g2d.drawRect(startX, startY, endX - startX, endY - startY);
}
}
drawPanel.updateUI();
}
}
}
public void mouseMoved(MouseEvent e) {}
public static void main(String[] args) {
ImageEditor editor = new ImageEditor();
editor.setVisible(true);
}
}
```
这个程序可以打开图像文件,显示图像并支持旋转,还可以在画布区域上进行各种规则图形的绘制,包括线段、矩形、多边形和圆。用户也可以通过鼠标拖动自由绘图。最后,程序还支持连续播放所选文件夹下的所有图像。
阅读全文