eclipse下面实现的java扫雷游戏
源代码在线查看: imagepanel.java
/*
* Created on 2005-5-10
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package view;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.JPanel;
/**
* @author mqqqvpppm
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ImagePanel extends JPanel implements ActionListener {
/**
*
*/
public ImagePanel(Image image) {
super();
imageArray = new ArrayList();
imageArray.add(image);
this.image = image;
width = image.getWidth(null);
height = image.getHeight(null);
setSize(width, height);
sign = 1;
amount = 1;
waitTime = 100;
waitTime2 = 5;
i = 0;
alpha = 100;
timer = new Timer(50, this);
// TODO Auto-generated constructor stub
}
public void setInterval(int interval) {
timer.setDelay(interval);
}
public void start() {
timer.start();
}
public void stop() {
try {
timer.wait();
} catch (Exception e) {
}
}
public void add(Image image) {
imageArray.add(image);
amount++;
}
public void setBounds(int x, int y, int width, int height) {
this.width = width;
this.height = height;
super.setBounds(x, y, width, height);
}
public void setSize(int width, int height) {
this.width = width;
this.height = height;
super.setSize(width, height);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (image == null)
return;
//draw image with alpha
Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
(float) alpha / 100f);
Composite c = g2.getComposite();
g2.setComposite(comp);
g2.drawImage(image, 0, 0, width, height, null);
g2.setComposite(c);
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (alpha == 100) {//alpha of image will be decreased
sign = -1;
//the delay before the alpha of image will be decreased
--waitTime;
if (waitTime != 0)
return;
waitTime = 100;
} else if (alpha == 0) {//image will change
//the delay before the next image appear
--waitTime2;
if (waitTime2 != 0)
return;
waitTime2 = 5;
//change the sign go add the alpha
sign = 1;
//calculate i to load the image circularly
i++;
if (i == amount)
i = 0;
image = (Image) imageArray.get(i);
}
alpha = alpha + 10 * sign;
repaint();
}
private Timer timer;
private Image image;
private int alpha;
private int width;
private int height;
private int amount;
private int sign;
private int i;
private int interval;
private int waitTime;
private int waitTime2;
private ArrayList imageArray;
}