动画播放,包括播放速度、是否循环、上一张、下一张(我局的例子是卫星图和雷达图两种的播放)
// 上一个按钮
private JButton lastBut = new JButton();
// 下一个按钮
private JButton nextBut = new JButton();
// 播放按钮
private JButton playBut = new JButton();
//监听事件
lastBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
palayLastpicture();
}
});
nextBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
palayNestpicture();
}
});
playBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doPlayManyPictures();
}
});
// 播放上一张图片时触发的方法
public void palayLastpicture() {
if (tCurrentRow > 0) {
nextBut.setEnabled(true);
tCurrentRow--;
}
byte[] imageContent = null;
if (contenes != null && contenes.size() > 0) {
WDBImageRadar imageSatellite = contenes.get(tCurrentRow);
filenametable.setRowSelectionInterval(tCurrentRow, tCurrentRow);
imageContent = imageSatellite.getContent();
Image image = null;
image = Toolkit.getDefaultToolkit().createImage(imageContent);
imagePane.setImage(image);
imagePane.repaint();
}
if (tCurrentRow == 0) {
lastBut.setEnabled(false);
}
}
// 播放下一张图片时触发的方法
public void palayNestpicture() {
int size = subList.size();
if (tCurrentRow < size - 1) {
lastBut.setEnabled(true);
tCurrentRow++;
}
byte[] imageContent = null;
if (contenes != null && contenes.size() > 0) {
filenametable.setRowSelectionInterval(tCurrentRow, tCurrentRow);
WDBImageRadar imageSatellite = contenes.get(tCurrentRow);
imageContent = imageSatellite.getContent();
Image image = null;
image = Toolkit.getDefaultToolkit().createImage(imageContent);
imagePane.setImage(image);
imagePane.repaint();
}
if (tCurrentRow == size - 1) {
nextBut.setEnabled(false);
}
}
// 播放多张图片
public void doPlayManyPictures() {
if (this.filenametable.getRowCount() > 0) {
isPalyingMethod();
if (imagePane.getCleckCount() == 1) {
playBut.setToolTipText("暂停");
playBut.setIcon(stopImage);
playBut.updateUI();
imagePane.setCleckCount(2);
imagePane.setPlayMany(true);
int[] tableRows = filenametable.getSelectedRows();
List<Image> images = new ArrayList<Image>();
if (tableRows.length > 1) {
for (int i = 0; i < tableRows.length; i++) {
WDBImageRadar wdbStatel = contenes.get(tableRows[i]);
byte[] imageContent = wdbStatel.getContent();
Image image = null;
if (imageContent != null) {
image = Toolkit.getDefaultToolkit().createImage(
imageContent);
images.add(image);
}
}
} else {
tableRows = new int[contenes.size()];
for (int i = 0; i < contenes.size(); i++) {
tableRows[i] = i;
WDBImageRadar wdbStatel = contenes.get(i);
byte[] imageContent = wdbStatel.getContent();
Image image = null;
if (imageContent != null) {
image = Toolkit.getDefaultToolkit().createImage(
imageContent);
images.add(image);
}
}
}
imagePane.setImages(images);
int times = (int) (Double.parseDouble(palyTimeComboBox
.getSelectedItem().toString()) * 1000);//设置的播放速度
imagePane.setDelay(times);
boolean isCycle = cycleBox.getState();
imagePane.setCycle(isCycle);
imagePane.setInfoIndex(tableRows);
imagePane.setCurrentImage(0);
imagePane.startAnimation();
} else {
playBut.setToolTipText("播放");
playBut.setIcon(playImage);
playBut.updateUI();
imagePane.setCleckCount(1);
imagePane.stopAnimation();
}
} else {
}
}
显示图片的panel--imagePane
public void startAnimation() { // 开始动画
if (animationTimer == null) {
animationTimer = new Timer(delay, this); // 实例化Timer对象
animationTimer.start(); // 开始运行
} else if (!animationTimer.isRunning()) // 如果没有运行
animationTimer.restart(); // 重新运行
}
public void stopAnimation() {
setPlayMany(false);
animationTimer.stop(); // 停止动画
if (animationTimer != null) {
animationTimer = null;
}
if (isStatelLive) {
statelLitFrame.noPlaying();
} else {
radarFrame.noPlaying();
}
}
public void paintComponent(Graphics g1) {
super.paintComponent(g1);
if (playMany) {
if (images != null && images.size() > 0) {
// Image imag = images.get(currentImage);
image = images.get(currentImage);
Graphics2D G2 = (Graphics2D) g1;
int panelSize_w = this.getWidth();
int panelSize_h = this.getHeight();
ImageIcon inc = new ImageIcon(getImage());
float rate = image.getWidth(null) * 1.0f
/ image.getHeight(null);
int w = (int) (panelSize_w / rate);
int y = (panelSize_h - w) / 2;
ResampleOp resampleOp = new ResampleOp(panelSize_w, w);// 转换
BufferedImage rescaledTomato = resampleOp.filter(
convertImageToBuffer(image), null);
Image imageTem = Toolkit.getDefaultToolkit().createImage(
rescaledTomato.getSource());
G2.drawImage(imageTem, 0, y, panelSize_w, w, this);
int tCurrentRow = infoIndex[currentImage];
if (isStatelLive) {
statelLitFrame.settCurrentRow(tCurrentRow);
statelLitFrame.getFilenametable().setRowSelectionInterval(
tCurrentRow, tCurrentRow);
} else {
radarFrame.settCurrentRow(tCurrentRow);
radarFrame.getFilenametable().setRowSelectionInterval(
tCurrentRow, tCurrentRow);
}
currentImage = currentImage + 1;
}
if (currentImage == images.size()) {
if (isCycle) {//是否循环
currentImage = 0;
} else {
this.stopAnimation();
cleckCount = 1;
}
}
} else {
if (this.getImage() != null) {
Graphics2D G2 = (Graphics2D) g1;
int panelSize_w = this.getWidth();
int panelSize_h = this.getHeight();
ImageIcon inc = new ImageIcon(getImage());
float rate = image.getWidth(null) * 1.0f
/ image.getHeight(null);
int w = (int) (panelSize_w / rate);
int y = (panelSize_h - w) / 2;
ResampleOp resampleOp = new ResampleOp(panelSize_w, w);// 转换
BufferedImage rescaledTomato = resampleOp.filter(
convertImageToBuffer(image), null);
imageTemp = Toolkit.getDefaultToolkit().createImage(
rescaledTomato.getSource());
G2.drawImage(imageTemp, 0, y, panelSize_w, w, this);
}
}
}