📄 testimage.java
字号:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.border.*;
public class testImage extends JFrame {
int frames = 50;
int h;
int w;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
MorphingImage morphingImage = new MorphingImage();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JSlider slider = new JSlider();
ImageIcon ic1;
ImageIcon ic2;
ImageIcon ic3;
Image currentImage;
BorderLayout borderLayout1 = new BorderLayout();
EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED);
JButton jBrowse = new JButton();
JButton jBrowse2 = new JButton();
JButton jStart = new JButton();
JButton jStop = new JButton();
String filepath1 = "";
String filepath2 = "";
Timer timer;
public testImage() {
try {
jbInit();
setSize(383, 350);
Toolkit theKit = this.getToolkit();
Dimension wndSize = theKit.getScreenSize();
int xLocation = (wndSize.width - this.getWidth())/2 ;
int yLocation = (wndSize.height - this.getHeight()) / 2;
this.setLocation(xLocation, yLocation);
setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
jBrowse.setText("Picture One");
jBrowse2.setText("Picture Two");
jStart.setText("Morphing start");
jStop.setText("Stop");
jBrowse.addActionListener(new testImage_jBrowse_actionAdapter(this));
jBrowse2.addActionListener(new testImage_jBrowse_actionAdapter(this));
jStart.addActionListener(new testImage_jBrowse_actionAdapter(this));
jStop.addActionListener(new testImage_jBrowse_actionAdapter(this));
/////////////////// timer
ActionListener timerListener = new testImage_jBrowse_actionAdapter(this);
timer = new Timer(200, timerListener);
//////////////////
jp1.setLayout(new FlowLayout(FlowLayout.CENTER));
jp1.add(jBrowse);
jp1.add(jBrowse2);
jp1.add(jStart);
//jp1.add(jStop);
jp1.setBorder(edge);
this.getContentPane().add(jp1, BorderLayout.NORTH);
jLabel1.setBorder(edge);
jLabel2.setBorder(edge);
jLabel3.setBorder(edge);
slider.setBorder(edge);
jp2.setLayout(new BorderLayout());
jp2.add(jLabel1, BorderLayout.WEST);
jp2.add(jLabel2, BorderLayout.EAST);
jp2.add(jLabel3, BorderLayout.CENTER);
jp2.add(slider, BorderLayout.SOUTH);
jp3.add(jStop);
this.getContentPane().add(jp3, BorderLayout.SOUTH);
this.getContentPane().add(jp2, BorderLayout.CENTER);
}
public static void main(String[] args) {
testImage testImage1 = new testImage();
testImage1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
void jBrowse_actionPerformed(ActionEvent e) {
if (e.getSource() == jBrowse) {
JFileChooser chooser = new JFileChooser();
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
String strSelectedPath1 = chooser.getSelectedFile().getPath();
filepath1 = strSelectedPath1;
ic1 = new ImageIcon(filepath1);
jLabel1.setIcon(ic1);
///////////////////////////////////////////////////////////////
try {
morphingImage.setSourceImage(ic1.getImage());
} catch (Exception ex) {
System.out.println(ex);
}
//////////////////////////////////////////////////////////////
}
} else if (e.getSource() == jBrowse2) {
JFileChooser chooser = new JFileChooser();
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
String strSelectedPath2 = chooser.getSelectedFile().getPath();
filepath2 = strSelectedPath2;
ic2 = new ImageIcon(filepath2);
jLabel2.setIcon(ic2);
///////////////////////////////////////////////////////////////
try {
morphingImage.setTargetImage(ic2.getImage());
} catch (Exception ex) {
System.out.println(ex);
}
//////////////////////////////////////////////////////////////
}
} else if (e.getSource() == jStart) {
if (morphingImage != null) {
morphingImage.setFrames(frames);
morphingImage.morphing();
Image currentImage = morphingImage.getCurrentImage();
ic3 = new ImageIcon(currentImage);
jLabel3.setIcon(new ImageIcon(currentImage));
// fire the timer
timer.start();
timer.setDelay(150);
}
// every slot of delay, the following section would be executed.
} else if (e.getSource() == timer) {
if (morphingImage != null) {
morphingImage.morphing();
currentImage = morphingImage.getCurrentImage();
jLabel3.setIcon(new ImageIcon(currentImage));
}
//else timer.stop();
}
//else timer.stop();
else if (e.getSource()== jStop){
timer.stop();
}
}
}
class testImage_jBrowse_actionAdapter implements java.awt.event.ActionListener {
testImage adaptee;
testImage_jBrowse_actionAdapter(testImage adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jBrowse_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -