用Java代码编写一个程序实现一下功能 1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
时间: 2024-03-10 21:48:49 浏览: 46
java实现的一个画板小程序画直线,矩形,三角形以及拖动图形功能
5星 · 资源好评率100%
好的,以下是使用Java代码实现功能的示例:
1. 显示并旋转图像
```java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageDisplay extends JPanel {
private Image image;
private double angle = 0;
public ImageDisplay(String imagePath) throws IOException {
image = ImageIO.read(new File(imagePath));
}
public void rotate(double angle) {
this.angle += angle;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform transform = new AffineTransform();
transform.translate(getWidth() / 2, getHeight() / 2);
transform.rotate(Math.toRadians(angle), 0, 0);
transform.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
g2d.drawImage(image, transform, null);
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Image Display");
ImageDisplay panel = new ImageDisplay("image.jpg");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
panel.rotate(45); // 旋转45度
}
}
```
2. 绘制图形
```java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShapeDrawer extends JPanel {
private ArrayList<Point> points = new ArrayList<Point>();
private ArrayList<Shape> shapes = new ArrayList<Shape>();
private Shape currentShape = null;
public ShapeDrawer() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
if (currentShape != null) {
shapes.add(currentShape);
currentShape = null;
}
points.clear();
repaint();
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (points.size() > 0) {
Point lastPoint = points.get(points.size() - 1);
currentShape = new Line(lastPoint, e.getPoint());
repaint();
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Shape shape : shapes) {
shape.draw(g2d);
}
if (currentShape != null) {
currentShape.draw(g2d);
}
}
private abstract class Shape {
public abstract void draw(Graphics2D g);
}
private class Line extends Shape {
private Point start;
private Point end;
public Line(Point start, Point end) {
this.start = start;
this.end = end;
}
@Override
public void draw(Graphics2D g) {
g.drawLine(start.x, start.y, end.x, end.y);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Shape Drawer");
ShapeDrawer panel = new ShapeDrawer();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
```
3. 连续播放图像
```java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ImagePlayer extends JPanel {
private Image[] images;
private int currentIndex = 0;
private double angle = 0;
public ImagePlayer(String folderPath) throws IOException {
File folder = new File(folderPath);
File[] imageFiles = folder.listFiles((dir, name) -> name.endsWith(".jpg"));
images = new Image[imageFiles.length];
for (int i = 0; i < imageFiles.length; i++) {
images[i] = ImageIO.read(imageFiles[i]);
}
Timer timer = new Timer(1000, e -> {
currentIndex = (currentIndex + 1) % images.length;
repaint();
});
timer.start();
}
public void rotate(double angle) {
this.angle += angle;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Image image = images[currentIndex];
AffineTransform transform = new AffineTransform();
transform.translate(getWidth() / 2, getHeight() / 2);
transform.rotate(Math.toRadians(angle), 0, 0);
transform.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
g2d.drawImage(image, transform, null);
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Image Player");
ImagePlayer panel = new ImagePlayer("images");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
panel.rotate(45); // 旋转45度
}
}
```
阅读全文