日期:2014-05-20 浏览次数:20715 次
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JApplet;
public class TApplet extends JApplet {
abstract class Action {
protected int clickX;
protected int clickY;
private boolean isFinish = false;
private Action nextAction;
public Action(int x, int y) {
clickX = x;
clickY = y;
}
public Action getNextAction() {
return nextAction;
}
public boolean isFinish() {
return isFinish;
}
public abstract void render(Graphics2D g);
protected void setFinish(boolean flag) {
isFinish = flag;
}
public void setNextAction(Action action) {
nextAction = action;
}
public abstract void update(long timeMillions);
}
class BeginAction extends Action {
private int beginX;
private int beginY;
private int currentX;
private int currentY;
public BeginAction(int x, int y) {
super(x, y);
beginX = currentX = x;
beginY = currentY = y + 300;
}
@Override
public void render(Graphics2D g) {
g.setColor(Color.red);
g.drawLine(beginX, beginY, currentX, currentY);
}
@Override
public void update(long timeMillions) {
if (!isFinish()) {
currentY -= 2;
if (currentY < clickY)
setFinish(true);
}
}
}
class DiffuseAction extends Action{
private int radius = 0;
public DiffuseAction(int x, int y) {
super(x, y);
}
@Override
public void render(Graphics2D g) {
g.setColor(Color.red);
g.drawRoundRect(clickX - radius / 2, clickY - radius / 2, radius, radius, radius, radius);
}
@Override
public void update(long timeMillions) {
radius += 2;
if(radius > 400)
setFinish(true);
}
}
private static final int HEIGHT = 600;
private static final int WIDTH = 600;
private Action currentAction;
private Thread currentThread;
private BufferedImage image;
@Override
public void init() {
super.init();
setSize(WIDTH, HEIGHT);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
final int x = e.getX();
final int y = e.getY();
if(currentThread != null)
currentThread.interrupt();
currentThread = new Thread() {