java怎么做图片浏览器

Python015

java怎么做图片浏览器,第1张

以前在itjob做过:就功能来讲:需要展示,需要上一页下一页,,,这是最简单的

布局上讲:一个Panel展示,一个Panel操作

设计的话,,首先得有图片的路径,可以保存在一个list链表中,点击上一页或下一页,展示图片的JPanel通过list的路径,找到相应的图片,并画出来,,这样就可以了

其他:至于其他的扩展功能,如全屏,旋转等,,都可以一点一点再加,先完成大体的架构功能吧

import java.awt.BorderLayout

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 <sizei++) {

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()

}

}

}

}

自定义一个jpanel用于展示image

/**

 * 

 */

package org.demo

import java.awt.Graphics

import java.awt.Graphics2D

import java.awt.Image

import java.util.HashMap

import java.util.Map

import javax.swing.JPanel

/**

 * @ClassName: ImageView

 * @Description: TODO(这里用一句话描述这个类的作用)

 * @author Btboy1978

 * @date 2017年6月12日 下午9:46:13 *

 */

public class ImageView extends JPanel {

private Map<Integer, Image> images

private int currentIndex

public ImageView() {

super()

images = new HashMap<Integer, Image>()

currentIndex = 0

}

/*

 * (non-Javadoc)

 * 

 * @see javax.swing.JComponent#paint(java.awt.Graphics)

 */

@Override

public void paint(Graphics g) {

g.clearRect(0, 0, WIDTH, HEIGHT)

Graphics2D g2 = (Graphics2D) g

g2.drawImage(images.get(currentIndex), 0, 0, this)

}

public void showNext() {

if (images.size() != 0) {

if (currentIndex != images.size() - 1) {

currentIndex++

}

}

this.repaint()

}

public void showUp() {

if (images.size() != 0) {

if (currentIndex != 0) {

currentIndex--

}

}

this.repaint()

}

public void addImage(Image image){

images.put(images.size(), image)

System.out.println(images.size())

}

}

测试方法

/**

 * 

 */

package org.demo

import java.awt.BorderLayout

import java.awt.EventQueue

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.border.EmptyBorder

import javax.imageio.ImageIO

import javax.swing.JButton

import java.awt.event.ActionListener

import java.io.File

import java.io.IOException

import java.awt.event.ActionEvent

/** 

* @ClassName: ImageViewTest 

* @Description: TODO(这里用一句话描述这个类的作用) 

* @author Btboy1978

* @date 2017年6月12日 下午9:59:42 *  

*/

public class ImageViewTest extends JFrame {

private JPanel contentPane

private ImageView imageView

/**

 * Launch the application.

 */

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

ImageViewTest frame = new ImageViewTest()

frame.setVisible(true)

} catch (Exception e) {

e.printStackTrace()

}

}

})

}

/**

 * Create the frame.

 */

public ImageViewTest() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

setBounds(100, 100, 450, 393)

contentPane = new JPanel()

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5))

setContentPane(contentPane)

contentPane.setLayout(null)

imageView = new ImageView()

imageView.setBounds(10, 10, 414, 284)

try {

imageView.addImage(ImageIO.read(new File("images/1.jpg")))

imageView.addImage(ImageIO.read(new File("images/2.jpg")))

imageView.addImage(ImageIO.read(new File("images/3.jpg")))

imageView.addImage(ImageIO.read(new File("images/4.jpg")))

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace()

}

contentPane.add(imageView)

JButton btnNewButton = new JButton("UP")

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

imageView.showUp()

}

})

btnNewButton.setBounds(81, 304, 117, 41)

contentPane.add(btnNewButton)

JButton button = new JButton("NEXT")

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

imageView.showNext()

}

})

button.setBounds(228, 304, 117, 41)

contentPane.add(button)

}

}