用Java代码实现用Java编写一个程序: 1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
时间: 2024-03-10 16:48:21 浏览: 133
编写JAVA程序实现图形用户界面的设计。
4星 · 用户满意度95%
好的,下面是一个用Java代码实现的示例:
```java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageEditor extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel canvasPanel;
private Canvas canvas;
private JList<String> fileList;
private JScrollPane scrollPane;
private JButton rotateButton;
private JButton playButton;
private JPopupMenu popupMenu;
private JMenuItem rotateMenuItem;
private JMenuItem deleteMenuItem;
private JMenuItem playMenuItem;
private ArrayList<File> imageFiles;
private int curIndex;
public ImageEditor() {
super("Image Editor");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 初始化UI组件
canvasPanel = new JPanel(new BorderLayout());
canvas = new Canvas();
canvasPanel.add(canvas, BorderLayout.CENTER);
fileList = new JList<>();
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fileList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
curIndex = fileList.getSelectedIndex();
if (curIndex >= 0) {
loadImage(imageFiles.get(curIndex));
}
}
});
scrollPane = new JScrollPane(fileList);
scrollPane.setPreferredSize(new Dimension(150, 0));
rotateButton = new JButton("Rotate");
rotateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showPopupMenu(e.getComponent(), e.getX(), e.getY());
}
});
playButton = new JButton("Play");
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
playImages();
}
});
// 初始化弹出菜单
popupMenu = new JPopupMenu();
rotateMenuItem = new JMenuItem("Rotate");
rotateMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rotateImage();
}
});
deleteMenuItem = new JMenuItem("Delete");
deleteMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteImage();
}
});
playMenuItem = new JMenuItem("Play");
playMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
playImages();
}
});
popupMenu.add(rotateMenuItem);
popupMenu.add(deleteMenuItem);
popupMenu.add(playMenuItem);
// 添加UI组件到主窗口
JPanel buttonPanel = new JPanel();
buttonPanel.add(rotateButton);
buttonPanel.add(playButton);
add(canvasPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.WEST);
add(buttonPanel, BorderLayout.SOUTH);
// 加载图像文件列表
imageFiles = new ArrayList<>();
File dir = new File("images");
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".gif");
}
});
Arrays.sort(files);
for (File file : files) {
imageFiles.add(file);
}
fileList.setListData(getFileNames(imageFiles));
if (imageFiles.size() > 0) {
fileList.setSelectedIndex(0);
}
}
// 获取文件名列表
private String[] getFileNames(ArrayList<File> files) {
String[] names = new String[files.size()];
for (int i = 0; i < files.size(); i++) {
names[i] = files.get(i).getName();
}
return names;
}
// 显示弹出菜单
private void showPopupMenu(Component invoker, int x, int y) {
boolean selected = (curIndex >= 0);
rotateMenuItem.setEnabled(selected);
deleteMenuItem.setEnabled(selected);
playMenuItem.setEnabled(selected);
popupMenu.show(invoker, x, y);
}
// 加载图像文件并显示到画布上
private void loadImage(File file) {
try {
BufferedImage image = ImageIO.read(file);
canvas.setImage(image);
canvas.repaint();
} catch (IOException e) {
e.printStackTrace();
}
}
// 旋转图像
private void rotateImage() {
if (curIndex >= 0) {
double angle = Math.toRadians(90);
BufferedImage image = canvas.getImage();
AffineTransform transform = new AffineTransform();
transform.rotate(angle, image.getWidth() / 2.0, image.getHeight() / 2.0);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage rotatedImage = new BufferedImage(image.getHeight(), image.getWidth(), image.getType());
op.filter(image, rotatedImage);
canvas.setImage(rotatedImage);
canvas.repaint();
}
}
// 删除图像
private void deleteImage() {
if (curIndex >= 0) {
imageFiles.remove(curIndex);
fileList.setListData(getFileNames(imageFiles));
if (imageFiles.size() > 0) {
curIndex = Math.min(curIndex, imageFiles.size() - 1);
fileList.setSelectedIndex(curIndex);
loadImage(imageFiles.get(curIndex));
} else {
canvas.setImage(null);
canvas.repaint();
}
}
}
// 连续播放图像
private void playImages() {
if (imageFiles.size() > 0) {
Timer timer = new Timer(1000, new ActionListener() {
int index = 0;
public void actionPerformed(ActionEvent e) {
loadImage(imageFiles.get(index));
index = (index + 1) % imageFiles.size();
}
});
timer.start();
}
}
// 画布类
private class Canvas extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage image;
public void setImage(BufferedImage image) {
this.image = image;
}
public BufferedImage getImage() {
return image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
int x = (getWidth() - image.getWidth()) / 2;
int y = (getHeight() - image.getHeight()) / 2;
g.drawImage(image, x, y, null);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ImageEditor editor = new ImageEditor();
editor.setVisible(true);
}
});
}
}
```
这个程序使用了Swing库来实现图形用户界面,使用了Java的图像处理库ImageIO和AffineTransform来实现图像的加载和旋转,使用了Java的定时器Timer来实现图像的连续播放。你可以在程序中修改图像文件列表所在的目录。
阅读全文