用Java代码编写一个程序实现一下功能 1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
时间: 2024-03-11 08:45:03 浏览: 137
商业编程-源码-图象载码程序源代码.zip
5星 · 资源好评率100%
好的,以下是Java代码实现上述功能的示例:
```java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageEditor extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel imagePanel, drawPanel;
private JLabel imageLabel;
private JButton rotateButton, lineButton, rectButton, polyButton, circleButton, playButton;
private BufferedImage image;
private File[] imageFiles;
private int currentIndex;
public ImageEditor(File[] imageFiles) {
super("Image Editor");
this.imageFiles = imageFiles;
currentIndex = 0;
// 初始化UI组件
imagePanel = new JPanel();
imageLabel = new JLabel();
imagePanel.add(imageLabel);
drawPanel = new JPanel();
rotateButton = new JButton("Rotate");
lineButton = new JButton("Line");
rectButton = new JButton("Rectangle");
polyButton = new JButton("Polygon");
circleButton = new JButton("Circle");
playButton = new JButton("Play");
drawPanel.add(rotateButton);
drawPanel.add(lineButton);
drawPanel.add(rectButton);
drawPanel.add(polyButton);
drawPanel.add(circleButton);
drawPanel.add(playButton);
// 设置布局
setLayout(new BorderLayout());
add(imagePanel, BorderLayout.CENTER);
add(drawPanel, BorderLayout.SOUTH);
// 添加事件监听器
rotateButton.addActionListener(this);
lineButton.addActionListener(this);
rectButton.addActionListener(this);
polyButton.addActionListener(this);
circleButton.addActionListener(this);
playButton.addActionListener(this);
setSize(800, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 显示第一张图片
showImage();
}
private void showImage() {
try {
image = ImageIO.read(imageFiles[currentIndex]);
imageLabel.setIcon(new ImageIcon(image));
pack();
} catch (IOException e) {
e.printStackTrace();
}
}
private void rotateImage() {
// 顺时针旋转90度
BufferedImage rotatedImage = new BufferedImage(image.getHeight(), image.getWidth(), image.getType());
Graphics2D g2d = rotatedImage.createGraphics();
g2d.translate((image.getHeight() - image.getWidth()) / 2, (image.getWidth() - image.getHeight()) / 2);
g2d.rotate(Math.PI / 2, image.getWidth() / 2, image.getHeight() / 2);
g2d.drawRenderedImage(image, null);
g2d.dispose();
image = rotatedImage;
imageLabel.setIcon(new ImageIcon(image));
pack();
}
private void drawLine() {
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.BLACK);
g2d.drawLine(0, 0, image.getWidth() - 1, image.getHeight() - 1);
imageLabel.repaint();
}
private void drawRectangle() {
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.BLACK);
g2d.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
imageLabel.repaint();
}
private void drawPolygon() {
BufferedImage tempImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
Graphics2D g2d = tempImage.createGraphics();
g2d.setColor(Color.BLACK);
int[] xPoints = {0, image.getWidth() / 2, image.getWidth() - 1};
int[] yPoints = {image.getHeight() / 2, 0, image.getHeight() - 1};
g2d.drawPolygon(xPoints, yPoints, 3);
g2d.dispose();
image = tempImage;
imageLabel.setIcon(new ImageIcon(image));
pack();
}
private void drawCircle() {
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.BLACK);
g2d.drawOval(0, 0, image.getWidth() - 1, image.getHeight() - 1);
imageLabel.repaint();
}
private void playImages() {
Thread thread = new Thread() {
public void run() {
for (File file : imageFiles) {
currentIndex++;
if (currentIndex >= imageFiles.length) {
currentIndex = 0;
}
showImage();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == rotateButton) {
rotateImage();
} else if (e.getSource() == lineButton) {
drawLine();
} else if (e.getSource() == rectButton) {
drawRectangle();
} else if (e.getSource() == polyButton) {
drawPolygon();
} else if (e.getSource() == circleButton) {
drawCircle();
} else if (e.getSource() == playButton) {
playImages();
}
}
public static void main(String[] args) {
File[] imageFiles = {new File("image1.jpg"), new File("image2.jpg"), new File("image3.jpg")};
new ImageEditor(imageFiles);
}
}
```
这个程序通过Java Swing库实现了一个简单的图像编辑器,支持打开、旋转、绘制基本图形、连续播放图片等功能。注意:此示例中的图片文件名应该根据实际情况更改。
阅读全文