java有意思的程序_有没有关于JAVA的一些简单又有趣的程序

展开全部

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 ;

// 定义玩家的行走步伐,数值越大62616964757a686964616fe4b893e5b19e31333330356238,移动速度越快 private int step = 3;

Point p; // @jve:decl-index=0:

double x = 0.0;

double y = 0.0;

// 定义了子弹的个数 int zidanshu = 70;

// 定义子弹初始值,这个是不变的

// 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.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyReleased(java.awt.event.KeyEvent e) {

if(e.getKeyCode()==10){

if(!gamexunhuan){

jButton1.setVisible(false);

jLabel.setVisible(false);

reset();

}

}

if (e.getKeyCode() == 37) {

kleft = false;

}

if (e.getKeyCode() == 38) {

kup = false;

}

if (e.getKeyCode() == 39) {

kright = false;

}

if (e.getKeyCode() == 40) {

kdown = false;

}

}

public void keyPressed(java.awt.event.KeyEvent e) {

if (e.getKeyCode() == 37) {

kleft = true;

}

if (e.getKeyCode() == 38) {

kup = true;

}

// 触发按右键

if (e.getKeyCode() == 39) {

kright = true;

}

if (e.getKeyCode() == 40) {

kdown = true;

}

}

});

}

return jButton;

}

class Threads implements Runnable {

public Threads(JButton jjb) {

jb = jjb;

}

JButton jb = null;

private boolean first = true;

public void run() {

while (gamexunhuan) {

go();

}

}

public void go() {

int zzx = 0;

int zzy = 0;

int zx = 0;

int zy = 0;

while (true) {

if(gamexunhuan){

int fangxiang = (int) (Math.random() * 4 + 1);

// 四个if随即从四个边发射子弹

if (fangxiang == 1) {

zx = 0;

zy = (int) (Math.random() * 701);

}

if (fangxiang == 2) {

zx = (int) (Math.random() * 701);

zy = 0;

}

if (fangxiang == 3) {

zx = 700;

zy = (int) (Math.random() * 701);

}

if (fangxiang == 4) {

zx = (int) (Math.random() * 701);

zy = 700;

}

// 初始化子弹,有了就不在加了

if (first) {

jPanel.add(jb, null);

first = false;

}

jb.setBounds(new Rectangle(zx, zy, 10, 10));

// 定义子弹与物体之间的步长

zzx = (int) (((x + 15) - zx) / 30);

zzy = (int) (((y + 15) - zy) / 30);

}

while (gamexunhuan) {

try {

zx += zzx;

zy += zzy;

jb.setLocation(zx, zy);

if (zx + 5 > x & zx + 5 < x + 30 & zy + 5 > y

& zy + 5 < y + 30) {

jButton.setIcon(new ImageIcon(fileLoc1));

gamexunhuan = false;

first = true;

jButton1.setVisible(true);

jLabel.setVisible(true);

lasttime = new Date().getTime();

Date gametime = new Date(lasttime-firsttime);

int min =0;

int sec =0;

min = gametime.getMinutes();

sec = gametime.getSeconds();

String endtime = "";

if(min!=0){

endtime=min + "分 " + sec + "秒";

}else{

endtime=sec + "秒";

}

jLabel.setText(" GAME OVER!!! \n用时:" + endtime);

break;

}

// 超出边线停止循环

if (zx > 700 | zy > 700 | zx < 0 | zy < 0) {

break;

}

Thread.sleep(60);

} catch (InterruptedException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

}

try {

Thread.sleep(50);

} catch (InterruptedException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

}

}

}

} // @jve:decl-index=0:visual-constraint="10,10"

这是一个以前写过的“是男人就撑过30秒的小游戏源码”

如果想要执行程序,麻烦留个邮箱。

追问

太太太太多了 有没有简单一点的 比如动画之类的简单一点的程序

追答

如果想只用代码写游戏程序的话,代码肯定会多啊,动画之类的真的没有,一般那个都是用flash做的。如果是贪吃蛇和其他的游戏,代码也是很多的。

本回答由网友推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值