该程序实现了图片的缩放以及浏览
package graphics;
/**
* 图片的缩放功能实现;
*为什么图片不能无限放大,因为Thread的run方法不断在调整。
*必须选择jpg或png的图片
*/
import java.applet.*;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import java.io.FileFilter;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Zoom extends JFrame {
static AudioClip p=null;
File []f1;
File f2[];
int n;//n为计算队列中的图片个数
int gs=0;//为图片指针,指向图片当前的位置g=(n+g)%n
Thread th=null;
Thread th1=null;
boolean b1,b2,b3;
int width=200,height=170;
int x,y,x1,y1,x2,y2,x3,x4,y4;
int count;
int t;
int tt;
public Zoom(){
setTitle("刘凯图片相册");
File f=new File("C:\\Users\\liukai\\Desktop\\图片相册");
f2=f.listFiles();
f1=new File[50];
n=f2.length;
System.out.println(n);
for(int i=0;i
f1[i]=f2[i];
}
Container c=getContentPane();
setLayout(null);
final JPanel jp=new GJpanel();
jp.setBounds(0, 0, 400, 340);
JButton jb=new JButton("加图");//选择图片
jb.setBounds(320, 340, 80, 50);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFileChooser chooser=new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter ft=new FileNameExtensionFilter("jpg&&png","png","gif","jpg");
chooser.setFileFilter(ft);
int re=chooser.showSaveDialog(jp);
if(re==JFileChooser.APPROVE_OPTION){
f1[n]=chooser.getSelectedFile();
n++;
gs=n-1;
System.out.println(n);
repaint();
}
}
});
final JSlider js=new JSlider();
js.setValue(40);
//js.setMaximum(100);
js.setPaintLabels(true);
js.setPaintTicks(true);
js.setPaintTrack(true);
js.setBounds(0, 350, 300, 30);
th=new Thread(new Runnable() {
@Override
public void run() {
while(true){
t=js.getValue();
width=200+2*t;
height=170+(int)(1.7*t);
x=t;
y=(int)((1.7*t)/2);
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
th1=new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
File g=new File("C:\\Users\\liukai\\Desktop\\夜空中最亮的星.wav");
URL u=null;
try {
u=g.toURL();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p=Applet.newAudioClip(u);
if(!b3){
p.loop();
b3=true;
}
}
});
jp.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
Point pt2=MouseInfo.getPointerInfo().getLocation();
x4=pt2.x;
y4=pt2.y;
if(x4<71){
gs=gs-1;
}
if(x4>326&&y<340){
gs=gs+1;
}
repaint();
}
});
jp.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
Point pt1=MouseInfo.getPointerInfo().getLocation();
x3=pt1.x;
if(x3<71){
b1=true;
}
else if(x3>326){
b2=true;
}else{
b1=false;
b2=false;
}repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
jp.addMouseWheelListener(new MouseWheelListener() {
//此处添加滚动事件
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.getWheelRotation()==1){
js.setValue(js.getValue()+(int)(5));
}
if(e.getWheelRotation()==-1){
js.setValue(js.getValue()+(int)(-(5)));
}
}
});
th1.start();
th.start();
c.add(js);
c.add(jp);
c.add(jb);
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
public static void main(String[] args) {
new Zoom();
}
class GJpanel extends JPanel{//在这个JPanel上显示图片。
private Image buffer;
public void paint(Graphics g){
Graphics2D g2=(Graphics2D) g;
//g2.shear(0.3, 0);
if(f1!=null){//判断File数组是否为空
ImageIcon icon=new ImageIcon(f1[Math.abs((n+gs)%n)].getPath());
g2.drawImage(icon.getImage(), 100-x, 100-y, width, height, this);
//实现点击左右两边实现翻页
if(b1==true){
ImageIcon icon1=new ImageIcon(new File("C:\\Users\\liukai\\Desktop\\point1.png").getPath());
g2.drawImage(icon1.getImage(), 0,0,71,400,this);
}
if(b2==true){
ImageIcon icon2=new ImageIcon(new File("C:\\Users\\liukai\\Desktop\\point2.png").getPath());
g2.drawImage(icon2.getImage(), 326,0,76,400,this);
}
}
}
}
}
第一次写博客,如有错误之处,还请见谅。