java图片浏览器代码 java实现浏览器( 五 )


import javax.swing.JPanel;
/**
* @ClassName: ImageView
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Btboy1978
* @date 2017年6月12日 下午9:46:13 *
*/
public class ImageView extends JPanel {
private MapInteger, Image images;
private int currentIndex;
public ImageView() {
super();
images = new HashMapInteger, 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);
}
}
【java图片浏览器代码 java实现浏览器】java图片浏览器代码的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于java实现浏览器、java图片浏览器代码的信息别忘了在本站进行查找喔 。

推荐阅读