用java编写一个图片浏览器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java编写一个图片浏览器相关的知识,希望对你有一定的参考价值。
用到布局
取出文件夹中所有图片后 将图片放进网格布局中 每个网格显示6张图片 并且网格布局可以上下翻页 查看文件夹中所有的图片 然后 点击 ”大图浏览“ (网格布局中显示的)用卡片布局 进行单个图浏览 并且可以上下翻页
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class ImageGallery extends JFrame implements ActionListener
public static final int G_WIDTH = 400, G_HEIGHT = 620;
public static final int L_WIDTH = 1200, L_HEIGHT = 520;
public static final int MAX_R = 3, MAX_C = 2;
public static final int GRID = 1, LARGE = 2;
private JFileChooser chooser;
private JMenuBar toolBar;
private JMenu menu;
private JMenuItem open;
private ArrayList<String> paths;
private JPanel showingPanel, buttonPanel;
private int page = 1;
private int count = 0;
private int showType = GRID;
private int pageMax;
private JButton last, next, large, grid;
public ImageGallery()
this.setTitle("Image gallery 0.1");
this.setBounds(100, 100, G_WIDTH, G_HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
toolBar = new JMenuBar();
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
menu = new JMenu("File");
toolBar.add(menu);
open = new JMenuItem("open");
open.addActionListener(this);
menu.add(open);
paths = new ArrayList<String>();
this.setJMenuBar(toolBar);
showingPanel = new JPanel();
buttonPanel = new JPanel();
last = new JButton("last");
last.addActionListener(this);
next = new JButton("next");
next.addActionListener(this);
large = new JButton("large");
large.addActionListener(this);
grid = new JButton("grid");
grid.addActionListener(this);
buttonPanel.add(last);
buttonPanel.add(large);
buttonPanel.add(grid);
buttonPanel.add(next);
this.add(buttonPanel, BorderLayout.SOUTH);
this.add(showingPanel);
this.setVisible(true);
public void doOpen()
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION)
String directory = chooser.getSelectedFile().getPath();
this.getAllImage(directory);
for (String s : this.paths)
System.out.println(s);
this.showingImage();
public void getAllImage(String directory)
paths.clear();
page = 1;
count = 0;
File file = new File(directory);
if (file.isDirectory())
String[] list = file.list();
for (String s : list)
String path = directory + "\\" + s;
File temp = new File(path);
if (!temp.isDirectory())
this.paths.add(path);
public void changePage(int page)
if (!paths.isEmpty())
switch (showType)
case GRID:
this.page += page;
if (this.page == 0)
this.page = 1;
if (this.page > pageMax)
this.page = pageMax;
break;
case LARGE:
this.count += page;
if (count < 0)
count = 0;
if (count > paths.size() - 1)
count = paths.size() - 1;
break;
this.showingImage();
public void showingImage()
this.remove(showingPanel);
showingPanel = new JPanel();
int size = paths.size();
int max = MAX_R * MAX_C;
pageMax = (size % max == 0 ? size / max : size / max + 1);
switch (showType)
case GRID:
this.setBounds(100, 100, G_WIDTH, G_HEIGHT);
showingPanel.setLayout(new GridLayout(MAX_R, MAX_C - 1));
int i = (page - 1) * max;
int j = page * max;
for (; i < j && i < size; i++)
String path = paths.get(i);
showingPanel.add(new ImagePanel(path));
break;
case LARGE:
int left = count - 1;
int right = count + 1;
this.setBounds(100, 100, L_WIDTH, L_HEIGHT);
showingPanel.setLayout(new GridLayout(1, 2));
if (left >= 0)
showingPanel.add(new ImagePanel(paths.get(left)));
else
showingPanel.add(new ImagePanel(""));
showingPanel.add(new ImagePanel(paths.get(count)));
if (right < size)
showingPanel.add(new ImagePanel(paths.get(right)));
else
showingPanel.add(new ImagePanel(""));
break;
this.add(showingPanel, BorderLayout.CENTER);
showingPanel.updateUI();
/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
new ImageGallery();
class ImagePanel extends JPanel
private ImageIcon image;
ImagePanel(String path)
image = new ImageIcon(path);
public ImageIcon getImageIcon()
return this.image;
@Override
protected void paintComponent(Graphics g)
// TODO Auto-generated method stub
g.drawImage(image.getImage(), 0, 0, this.getWidth(), this
.getHeight(), this);
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
String key = e.getActionCommand();
System.out.println(key);
if (key.equals("open"))
doOpen();
else if (key.endsWith("last"))
changePage(-1);
else if (key.endsWith("next"))
changePage(1);
else if (key.endsWith("large"))
if (!paths.isEmpty())
count =0;
showType = LARGE;
showingImage();
else if (key.endsWith("grid"))
if (!paths.isEmpty())
page = 1;
showType = GRID;
showingImage();
参考技术A 稍麻烦一点,
下午给出代码
-------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class ImageView extends JFrame
private boolean isSingle = true;
private JPanel panel = new JPanel();
private CardLayout card = null;
private File[] files = null;
private int fileIndex = 0;
private JPanel single = new JPanel();
private JPanel multiple = new JPanel();
public ImageView()
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(600, 450);
setResizable(false);
getContentPane().setLayout(null);
card = new CardLayout(0, 0);
panel.setLayout(card);
panel.setBounds(12, 46, 570, 328);
getContentPane().add(panel);
panel.add(single, "single");
single.setLayout(new BorderLayout(0, 0));
panel.add(multiple, "multiple");
multiple.setLayout(new GridLayout(2, 3, 0, 0));
JButton btnBrowse = new JButton("Browse...");
btnBrowse.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
int returnVal = chooser.showOpenDialog(getContentPane());
if (returnVal == JFileChooser.APPROVE_OPTION)
files = chooser.getSelectedFiles();
showPicture();
);
btnBrowse.setBounds(12, 10, 91, 21);
getContentPane().add(btnBrowse);
JButton btnSingle = new JButton("single");
btnSingle.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
isSingle = true;
card.show(panel, "single");
showPicture();
);
btnSingle.setBounds(115, 10, 91, 21);
getContentPane().add(btnSingle);
JButton btnMultiple = new JButton("multiple");
btnMultiple.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
isSingle = false;
card.show(panel, "multiple");
showPicture();
);
btnMultiple.setBounds(218, 10, 91, 21);
getContentPane().add(btnMultiple);
JButton btnPrevious = new JButton("previous");
btnPrevious.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (isSingle)
fileIndex -= 1;
else
fileIndex -= 6;
showPicture();
);
btnPrevious.setBounds(92, 384, 91, 21);
getContentPane().add(btnPrevious);
JButton btnNext = new JButton("next");
btnNext.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (isSingle)
fileIndex += 1;
else
fileIndex += 6;
showPicture();
);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - getWidth()) / 2,
(screenSize.height - getHeight()) / 2);
btnNext.setBounds(423, 384, 91, 21);
getContentPane().add(btnNext);
setVisible(true);
private void showPicture()
if (files == null || files.length == 0)
return;
if (fileIndex > files.length || fileIndex < 0)
fileIndex = 0;
PicturePanel canvas = null;
if (isSingle)
if (single.getComponentCount() > 0)
canvas = (PicturePanel) single.getComponent(0);
else
canvas = new PicturePanel();
single.add(canvas, BorderLayout.CENTER);
canvas.setPath(files[fileIndex].getPath());
canvas.repaint();
single.repaint();
else
for (int i = 0; i < 6; i++)
if (files.length == fileIndex + i)
break;
if (multiple.getComponentCount() > i)
canvas = (PicturePanel) multiple.getComponent(i);
else
canvas = new PicturePanel();
multiple.add(canvas);
canvas.setPath(files[fileIndex + i].getPath());
canvas.repaint();
multiple.repaint();
panel.updateUI();
public static void main(String[] args)
new ImageView();
class PicturePanel extends JPanel
private Image image;
public PicturePanel()
public void setPath(String filename)
ImageIcon icon = new ImageIcon(filename);
this.image = icon.getImage();
public void paint(Graphics g)
super.paint(g);
protected void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(image, 0, 0, 400, 300, this);
追问
还是有错误
追答什么错?
追问net beans 中不能运行
可以在程序中添加一个线程 将取出的图片 自动播放
这就是一个类
你在netbeans下建一个同名类,然后把内容替换掉。
是不是你少引用包了?不能运行有什么错,你看一下提示。应该可以看出些问题的。
这个在eclipse下是可以运行的。换到netbeans,只要你没有拷错,也可以运行。
用BufferedImage加载图片
参考技术A BufferedImage是Image的一个子类, BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片,通常用来做图片修改操作如大小变换、图片变灰、设置图片透明或不透明等。Image是一个抽象列,BufferedImage是Image的实现。Image和BufferedImage的主要作用就是将一副图片加载到内存中。
Java将一副图片加载到内存中的方法是:
String imgPath = "C://demo.jpg";
BufferedImage image = ImageIO.read(new FileInputStream(imgPath));
可以用该方法可以获得图片的详细信息,例如:
获得图片的宽度:image.getWidth(null);图片只有加载内存中才能对图片进行进一步的处理。
接下来画出加载过的图片
当我们要在哪里绘图时,就先获得该对象的Graphics对象的引用,然后调用drawImage的方法。对于BuffereImage对象也是一样的。先在BuffereImage对象中绘图,然后将BuffereImage对象绘制到Frame中。这样就实现了双缓冲。
在paint方法中具体实现如下:
public void paint(Graphics g)
super.paint(g);
bi = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB);
Graphics bg = bi.getGraphics(); //在这里获得了bi的Graphics的引用
bg.drawImage(要显示的图片,.....);//调用bg的drawImage方法。
if (fi)
g = img.getGraphics();
g.drawImage(bi, 50, 50, 40, 40, this);
以上是关于用java编写一个图片浏览器的主要内容,如果未能解决你的问题,请参考以下文章
请教java高手们,帮我写一个java编写的图片浏览器,功能如下:有自动浏览功能,每隔几秒图片自动翻页。用
10R,求个简单的JAVA图片浏览器,窗体我设计好了,只需要,下一张,上一张按钮具有功能。,急
在JAVA中,实现一个简单的图像浏览器,部署“上一张”、“下一张”两个按钮,点击按钮可前后翻阅图片。