当前位置: 首页 > news >正文

Java GUI小程序之图片浏览器

以下是一个简单的图片浏览器示例代码,它包含了图片放大缩小、拖拽、上一张/下一张查看等功能。你可以根据它进行扩展,提高用户体验。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;public class ImageViewer extends JFrame {private static final long serialVersionUID = 1L;private JLabel imageLabel;private JScrollPane scrollPane;private BufferedImage image;private Dimension imageSize;private JPanel controlPanel;private JSlider zoomSlider;private JButton previousButton;private JButton nextButton;private int currentImageIndex = 0;private String[] imagePaths = {"image1.jpg", "image2.jpg", "image3.jpg"};private int zoomLevel = 100; // Default zoom levelpublic ImageViewer() {setTitle("Image Viewer");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setPreferredSize(new Dimension(800, 600));setLayout(new BorderLayout());// Create image label and scroll paneimageLabel = new JLabel();imageLabel.setHorizontalAlignment(SwingConstants.CENTER);scrollPane = new JScrollPane(imageLabel);add(scrollPane, BorderLayout.CENTER);// Create control panelcontrolPanel = new JPanel();controlPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));add(controlPanel, BorderLayout.SOUTH);// Create zoom sliderzoomSlider = new JSlider(SwingConstants.HORIZONTAL, 50, 200, zoomLevel);zoomSlider.setMajorTickSpacing(50);zoomSlider.setMinorTickSpacing(10);zoomSlider.setPaintTicks(true);zoomSlider.setPaintLabels(true);zoomSlider.addChangeListener(new ChangeListener() {public void stateChanged(ChangeEvent e) {zoomLevel = zoomSlider.getValue();updateImage();}});controlPanel.add(zoomSlider);// Create previous buttonpreviousButton = new JButton("Previous");previousButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (currentImageIndex > 0) {currentImageIndex--;loadImage();}}});controlPanel.add(previousButton);// Create next buttonnextButton = new JButton("Next");nextButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (currentImageIndex < imagePaths.length - 1) {currentImageIndex++;loadImage();}}});controlPanel.add(nextButton);// Load the first imageloadImage();// Add mouse listeners to enable draggingimageLabel.addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e) {imageLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.MOVE_CURSOR));}public void mouseReleased(MouseEvent e) {imageLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));}});imageLabel.addMouseMotionListener(new MouseMotionAdapter() {public void mouseDragged(MouseEvent e) {int dx = e.getX() - (imageSize.width - scrollPane.getViewport().getWidth()) / 2;int dy = e.getY() - (imageSize.height - scrollPane.getViewport().getHeight()) / 2;int newHValue = scrollPane.getHorizontalScrollBar().getValue() - dx;int newVValue = scrollPane.getVerticalScrollBar().getValue() - dy;if (newHValue < 0)newHValue = 0;if (newVValue < 0)newVValue = 0;if (newHValue + scrollPane.getViewport().getWidth() > imageSize.width)newHValue = imageSize.width - scrollPane.getViewport().getWidth();if (newVValue + scrollPane.getViewport().getHeight() > imageSize.height)newVValue = imageSize.height - scrollPane.getViewport().getHeight();scrollPane.getHorizontalScrollBar().setValue(newHValue);scrollPane.getVerticalScrollBar().setValue(newVValue);}});pack();setLocationRelativeTo(null);setVisible(true);}private void loadImage() {try {image = ImageIO.read(new File(imagePaths[currentImageIndex]));imageSize = new Dimension(image.getWidth(), image.getHeight());updateImage();} catch (IOException e) {JOptionPane.showMessageDialog(this, "Error loading image: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);}}private void updateImage() {int newWidth = (int) (imageSize.width * zoomLevel / 100.0);int newHeight = (int) (imageSize.height * zoomLevel / 100.0);BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);scaledImage.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);imageLabel.setIcon(new ImageIcon(scaledImage));}public static void main(String[] args) {try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (Exception e) {// Ignore}new ImageViewer();}}

运行效果如下:

http://www.lryc.cn/news/235691.html

相关文章:

  • Kafka-4.1-工作原理综述
  • Linux八股文
  • SPASS-偏相关分析
  • 第二证券:今日投资前瞻:小米汽车引关注 全球风光有望持续高速发展
  • Docker中的RabbitMQ已经启动运行,但是管理界面打不开
  • 自动化网络图软件
  • 如何基于亚马逊云科技打造高性能的 SQL 向量数据库 MyScale
  • 《轻松入门!快速安装PyCharm,打造高效Python编程环境》
  • Golang环境搭建Win10(简洁版)
  • 【算法每日一练]-分块(保姆级教程 篇1)POJ3648
  • 【华为OD题库-026】通过软盘拷贝文件-java
  • 定量数据和定性数据
  • 【Linux】:体系结构与进程概念
  • react-router-dom 版本6.18.0中NavLink的api和属性介绍
  • 八叉树(Octree)和KD树区别?2d tree与3d tree区别?
  • Union(联合体、共用体)
  • C++11的互斥包装器
  • HR应用在线人才测评,给企业招聘带来的好处
  • 深入了解百度爬虫工作原理
  • 【C语言基础】分享近期学习到的volatile关键字、__NOP__()函数以及# #if 1 #endif
  • docker容器自启动
  • 【C++】:模板的使用
  • Springboot框架中使用 Redis + Lua 脚本进行限流功能
  • 【nlp】2.5(cpu version) 人名分类器实战项目(对比RNN、LSTM、GRU模型)
  • 记录基于scapy构造ClientHello报文的尝试
  • 程序设计实践学习笔记
  • Ubuntu中apt-get update显示域名解析失败
  • go学习之简单项目
  • 代码随想录二刷 | 数组 | 总结篇
  • go test 命令详解