用Java 编写一个程序实现一下功能1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
时间: 2024-04-23 17:22:12 浏览: 203
好的,以下是一个简单的实现示例:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
public class ImageEditor extends JFrame {
private static final long serialVersionUID = 1L;
private JList<File> fileList;
private ImagePanel imagePanel;
private List<File> imageFiles;
public ImageEditor() {
super("Image Editor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create menu bar
JMenuBar menuBar = new JMenuBar();
// Create file menu
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
// Create open item
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openImage();
}
});
fileMenu.add(openItem);
// Create exit item
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitItem);
// Create draw menu
JMenu drawMenu = new JMenu("Draw");
menuBar.add(drawMenu);
// Create line item
JMenuItem lineItem = new JMenuItem("Line");
lineItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imagePanel.setDrawingMode(DrawingMode.LINE);
}
});
drawMenu.add(lineItem);
// Create rectangle item
JMenuItem rectItem = new JMenuItem("Rectangle");
rectItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imagePanel.setDrawingMode(DrawingMode.RECTANGLE);
}
});
drawMenu.add(rectItem);
// Create ellipse item
JMenuItem ellipseItem = new JMenuItem("Ellipse");
ellipseItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imagePanel.setDrawingMode(DrawingMode.ELLIPSE);
}
});
drawMenu.add(ellipseItem);
// Create polygon item
JMenuItem polygonItem = new JMenuItem("Polygon");
polygonItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imagePanel.setDrawingMode(DrawingMode.POLYGON);
}
});
drawMenu.add(polygonItem);
// Create free draw item
JMenuItem freeDrawItem = new JMenuItem("Free Draw");
freeDrawItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imagePanel.setDrawingMode(DrawingMode.FREE_DRAW);
}
});
drawMenu.add(freeDrawItem);
// Create play menu
JMenu playMenu = new JMenu("Play");
menuBar.add(playMenu);
// Create play item
JMenuItem playItem = new JMenuItem("Play");
playItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
playImages();
}
});
playMenu.add(playItem);
// Set menu bar
setJMenuBar(menuBar);
// Create file list
fileList = new JList<File>();
fileList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
File selectedFile = fileList.getSelectedValue();
if (selectedFile != null) {
try {
imagePanel.setImage(ImageIO.read(selectedFile));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
});
add(new JScrollPane(fileList), "West");
// Create image panel
imagePanel = new ImagePanel();
add(imagePanel);
// Set window size and show
setSize(800, 600);
setVisible(true);
}
private void openImage() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jpg") || name.endsWith(".png");
}
});
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile != null) {
try {
imagePanel.setImage(ImageIO.read(selectedFile));
imageFiles = new ArrayList<File>(Arrays.asList(selectedFile.getParentFile().listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jpg") || name.endsWith(".png");
}
})));
fileList.setListData(imageFiles.toArray(new File[0]));
fileList.setSelectedValue(selectedFile, true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
private void playImages() {
if (imageFiles != null && imageFiles.size() > 0) {
int delay = 1000; // 1 second
Timer timer = new Timer(delay, new ActionListener() {
private int index = 0;
public void actionPerformed(ActionEvent e) {
if (index >= imageFiles.size()) {
index = 0;
}
File selectedFile = imageFiles.get(index);
if (selectedFile != null) {
try {
imagePanel.setImage(ImageIO.read(selectedFile));
fileList.setSelectedValue(selectedFile, true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
index++;
}
});
timer.start();
}
}
public static void main(String[] args) {
new ImageEditor();
}
private enum DrawingMode {
LINE, RECTANGLE, ELLIPSE, POLYGON, FREE_DRAW
}
private class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
private java.awt.Image image;
private DrawingMode drawingMode;
private Path2D polygonPath;
private Point2D freeDrawPoint;
public ImagePanel() {
setBackground(Color.WHITE);
setDoubleBuffered(true);
setFocusable(true);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (drawingMode == DrawingMode.POLYGON) {
if (polygonPath == null) {
polygonPath = new Path2D.Double();
polygonPath.moveTo(e.getX(), e.getY());
} else {
polygonPath.lineTo(e.getX(), e.getY());
}
} else if (drawingMode == DrawingMode.FREE_DRAW) {
freeDrawPoint = e.getPoint();
}
}
public void mouseReleased(MouseEvent e) {
if (drawingMode == DrawingMode.POLYGON) {
if (e.getClickCount() == 2) {
polygonPath.closePath();
}
} else if (drawingMode == DrawingMode.FREE_DRAW) {
freeDrawPoint = null;
}
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
if (drawingMode == DrawingMode.LINE) {
repaint();
} else if (drawingMode == DrawingMode.RECTANGLE) {
repaint();
} else if (drawingMode == DrawingMode.ELLIPSE) {
repaint();
} else if (drawingMode == DrawingMode.POLYGON) {
polygonPath.lineTo(e.getX(), e.getY());
repaint();
} else if (drawingMode == DrawingMode.FREE_DRAW) {
Graphics2D g2d = (Graphics2D) getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.BLACK);
g2d.drawLine((int) freeDrawPoint.getX(), (int) freeDrawPoint.getY(), e.getX(), e.getY());
freeDrawPoint = e.getPoint();
}
}
});
}
public void setImage(java.awt.Image image) {
this.image = image;
repaint();
}
public void setDrawingMode(DrawingMode drawingMode) {
this.drawingMode = drawingMode;
polygonPath = null;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = (getWidth() - image.getWidth(null)) / 2;
int y = (getHeight() - image.getHeight(null)) / 2;
AffineTransform transform = new AffineTransform();
transform.translate(x, y);
if (drawingMode == DrawingMode.LINE) {
g2d.setColor(Color.BLUE);
g2d.draw(new Line2D.Double(0, getHeight() / 2, getWidth(), getHeight() / 2));
} else if (drawingMode == DrawingMode.RECTANGLE) {
g2d.setColor(Color.RED);
g2d.draw(new Rectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1));
} else if (drawingMode == DrawingMode.ELLIPSE) {
g2d.setColor(Color.GREEN);
g2d.draw(new Ellipse2D.Double(0, 0, getWidth() - 1, getHeight() - 1));
} else if (drawingMode == DrawingMode.POLYGON) {
if (polygonPath != null) {
g2d.setColor(Color.ORANGE);
g2d.draw(polygonPath);
}
}
g2d.drawImage(image, transform, null);
}
}
}
}
```
这个程序使用了Swing和Java2D库来实现图形界面和图像处理功能。其中,JList用于显示图片文件列表,JFileChooser用于打开图片文件,AffineTranform用于实现图像旋转和变换,Path2D用于实现自由绘图和多边形绘制,Timer用于实现图像连续播放,等等。
阅读全文