import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Date;
public class Game extends JFrame {
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(478, 361, 164, 51));
jButton1.setText("重新开始");
jButton1.setVisible(false);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jButton1.setVisible(false);
jLabel.setVisible(false);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO �Զ���� catch ��
e1.printStackTrace();
}
reset();
}
});
}
return jButton1;
}
public static void main(String[] args) {
Game game = new Game();
game.start();
game.reset();
game.gogo();
}
public void reset() {
kup = false;
kdown = false;
kleft = false;
kright = false;
int chushihua = 0;
while (chushihua < zidanshu) {
((JButton) buttonal.get(chushihua)).setBounds(new Rectangle(-50,
-50, 10, 10));
chushihua++;
}
gamexunhuan = true;
jButton.setIcon(new ImageIcon(fileLoc));
jButton.setLocation(320, 320);
p = jButton.getLocation();
x=p.getX();
y=p.getY();
firsttime=new Date().getTime();
}
public void start() {
int chushihua = 0;
while (chushihua < zidanshu) {
JButton jb = new JButton();
jb.setBounds(new Rectangle(-50, -50, 10, 10));
jb.setEnabled(false);
Threads ths = new Threads(jb);
Thread th = new Thread(ths);
buttonal.add(jb);
threadal.add(th);
chushihua++;
}
Game.Move move = new Move();
Thread tm = new Thread(move);
tm.start();
}
public void gogo() {
int chushihua = 0;
while (chushihua < zidanshu) {
((Thread) threadal.get(chushihua)).start();
chushihua++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO �Զ���� catch ��
e.printStackTrace();
}
}
}
// private static Game game;
private long firsttime;
private long lasttime;
private static final long serialVersionUID = 1L;
private JPanel jPanel = null;
private JButton jButton = null;
private boolean kup ;
private boolean kdown ;
private boolean kleft ;
private boolean kright ;
// ������ҵ����߲�������ֵԽ���ƶ��ٶ�Խ��
private int step = 3;
Point p; // @jve:decl-index=0:
double x = 0.0;
double y = 0.0;
// �������ӵ��ĸ���
int zidanshu = 70;
// �����ӵ���ʼֵ������Dz����
// int chushihua = 0;
// ��������ӵ����ߵ�ѭ��false�Ͳ�����
private boolean gamexunhuan = true;
private JLabel jLabel = null;
private JButton jButton1 = null;
private ArrayList buttonal = new ArrayList();
private ArrayList threadal = new ArrayList();
URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
URL fileLoc = urlLoader.findResource("MyGameIcons/gwl1.gif"); // @jve:decl-index=0:
URL fileLoc1 = urlLoader.findResource("MyGameIcons/gwls1.gif");
/**
* This is the default constructor
*/
public Game() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(700, 700);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(1);
}
});
this.setResizable(false);
this.setContentPane(getJPanel());
this.setTitle("小游戏!(模拟撑过30秒的小DEMO)");
this.setVisible(true);
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(42, -33, 595, 308));
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setForeground(new Color(250, 2, 2));
jLabel.setEnabled(true);
jLabel.setVisible(false);
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJButton(), null);
jPanel.setForeground(new Color(1, 1, 1));
jPanel.setBackground(new Color(1, 1, 1));
jPanel.setVisible(true);
jPanel.add(jLabel, null);
jPanel.add(getJButton1(), null);
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
class Move implements Runnable {
public void run() {
while(true){
while (gamexunhuan) {
p = jButton.getLocation();
if (kup) {
if (kleft) {
x = p.getX();
y = p.getY();
if (x > 0 && y > 0) {
jButton.setLocation((int) x - step, (int) y
- step);
}
} else if (kright) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y > 0) {
jButton.setLocation((int) x + step, (int) y
- step);
}
} else {
x = p.getX();
y = p.getY();
if (y > 0) {
jButton.setLocation((int) x, (int) y - step);
}
}
}
if (kdown) {
if (kleft) {
x = p.getX();
y = p.getY();
if (y + 60 < 700 && x > 0) {
jButton.setLocation((int) x - step, (int) y
+ step);
}
} else if (kright) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y + 60 < 700) {
jButton.setLocation((int) x + step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (y + 60 < 700) {
jButton.setLocation((int) x, (int) y + step);
}
}
}
if (kleft) {
if (kup) {
x = p.getX();
y = p.getY();
if (x > 0 && y > 0) {
jButton.setLocation((int) x - step, (int) y
- step);
}
} else if (kdown) {
x = p.getX();
y = p.getY();
if (y + 60 < 700 && x > 0) {
jButton.setLocation((int) x - step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (x > 0) {
jButton.setLocation((int) x - step, (int) y);
}
}
}
if (kright) {
if (kup) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y > 0) {
jButton.setLocation((int) x + step, (int) y
- step);
}
} else if (kdown) {
x = p.getX();
y = p.getY();
if (x + 40 < 700 && y + 60 < 700) {
jButton.setLocation((int) x + step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (x + 40 < 700) {
jButton.setLocation((int) x + step, (int) y);
}
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO �Զ���� catch ��
e.printStackTrace();
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO �Զ���� catch ��
e.printStackTrace();
}
}
}
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(320, 320, 30, 30));
jButton.setBackground(new Color(1, 1, 1));
p = jButton.getLocation();
x = p.getX();
y = p.getY();
jButton.setIcon(new ImageIcon(fileLoc));
jButton.addKeyL