当前位置:首页 > 代码 > 正文

java小游戏源代码带注释(简单的java程序代码带注释)

admin 发布:2022-12-19 04:56 116


今天给各位分享java小游戏源代码带注释的知识,其中也会对简单的java程序代码带注释进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求一个java扫雷游戏的程序源代码,尽量多点注释,要确实可用的!急急急急急急急急急急急急!!!!!

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.Insets;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.Timer;

public class ScanLei1 extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;

private Container contentPane;

private JButton btn;

private JButton[] btns;

private JLabel b1;

private JLabel b2;

private JLabel b3;

private Timer timer;

private int row=9;

private int col=9;

private int bon=10;

private int[][] a;

private int b;

private int[] a1;

private JPanel p,p1,p2,p3;

public ScanLei1(String title){

super(title);

contentPane=getContentPane();

setSize(297,377);

this.setBounds(400, 100, 400, 500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

timer =new Timer(1000,(ActionListener) this);

a = new int[row+2][col+2];

initGUI();

}

public void initGUI(){

p3=new JPanel();

b=bon;

JMenuBar menuBar=new JMenuBar();

JMenu menu1=new JMenu("游戏");

JMenu menu2=new JMenu("帮助");

JMenuItem mi1=new JMenuItem("初级");

JMenuItem mi2 = new JMenuItem("中级");

JMenuItem mi3 =new JMenuItem("高级");

mi1.addActionListener(this);

menu1.add(mi1);

mi2.addActionListener(this);

menu1.add(mi2);

mi3.addActionListener(this);

menu1.add(mi3);

menuBar.add(menu1);

menuBar.add(menu2);

p3.add(menuBar);

b1=new JLabel(bon+"");

a1=new int[bon];

btn =new JButton("开始");

btn.addActionListener(this);

b2=new JLabel("0");

b3=new JLabel("");

btns=new JButton[row*col];

p=new JPanel();

p.setLayout(new BorderLayout());

contentPane.add(p);

p.add(p3,BorderLayout.NORTH);

//combo=new JComboBox(new Object[]{"初级","中级","高级"} );

//加监听

/*combo.addItemListener(new ItemListener(){

}});*/

p1=new JPanel();

//在那个位置

//(( FlowLayout)p1.getLayout()).setAlignment( FlowLayout.RIGHT);

p1.add(b1);

p1.add(btn);

p1.add(b2);

p1.add(b3);

p.add(p3,BorderLayout.NORTH);

p.add(p1,BorderLayout.CENTER);

p2=new JPanel();

p2.setLayout(new GridLayout(row,col,0,0));

for(int i=0;irow*col;i++){

btns[i]=new JButton("");

btns[i].setMargin(new Insets(0,0,0,0));

btns[i].setFont(new Font(null,Font.BOLD,25));

btns[i].addActionListener(this);

btns[i].addMouseListener(new NormoreMouseEvent());

p2.add(btns[i]);

}

contentPane.add(p,BorderLayout.NORTH);

contentPane.add(p2,BorderLayout.CENTER);

}

public void go(){

setVisible(true);

}

public static void main(String[] args){

new ScanLei1("扫雷").go();

}

public void out(int[][] a,JButton[] btns,ActionEvent e,int i,int x,int y){

int p=1;

if(a[x][y]==0){

a[x][y]=10;

btns[i].setEnabled(false); //33

for(int l=y-1;l=y+1;l++){

int m=x-1-1;

int n=l-1;

p=1;

System.out.println(a[1][2]);

if(n-1ncolm-1mrow)

{

for(int q=0;qrowp==1;q++){//col--row;

if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){

if(a[x-1][l]!=0a[x-1][l]!=10){

btns[n+col*q].setText(a[x-1][l]+"");

a[x-1][l]=10;

btns[n+col*q].setEnabled(false);

}

else if(a[x-1][l]==0){

//a[x-1][l]=10;

btns[n+col*q].setEnabled(false);

out(a,btns,e,n+col*q,x-1,l); ////55////

a[x-1][l]=10;

btns[n+col*q].setEnabled(false);

}

p=0;

}

}

}

p=1;

m=x;

if(n-1ncolm-1mcol)

{

for(int q=0;qrowp==1;q++){

if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){

if(a[x+1][l]!=0a[x+1][l]!=10){

btns[n+col*q].setText(a[x+1][l]+"");

a[x+1][l]=10;

btns[n+col*q].setEnabled(false);

}

else if(a[x+1][l]==0){

out(a,btns,e,n+col*q,x+1,l);///55////

a[x+1][l]=10;

btns[n+col*q].setEnabled(false);

}

p=0;

}

}

}

}

int m=x-1;

int n=y-1-1;

p=1;

if(n-1ncolm-1mcol)

{

for(int q=0;qrowp==1;q++){

if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){

if(a[x][y-1]!=0a[x][y-1]!=10){

btns[n+col*q].setText(a[x][y-1]+"");

a[x][y-1]=10;

btns[n+col*q].setEnabled(false);

}

else if(a[x][y-1]==0){

out(a,btns,e,n+col*q,x,y-1);

a[x][y-1]=10;

btns[n+col*q].setEnabled(false);

}

p=0;

}

}

}

p=1;

m=x-1;

n=y+1-1;

if(n-1ncolm-1mcol)

{

for(int q=0;qrowp==1;q++){

if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){

if(a[x][y+1]!=0a[x][y+1]!=10){

btns[n+col*q].setText(a[x][y+1]+"");

a[x][y+1]=10;

btns[n+col*q].setEnabled(false);

}

else if(a[x][y+1]==0){

out(a,btns,e,n+col*q,x,y+1);

a[x][y+1]=10;

btns[n+col*q].setEnabled(false);

}

p=0;

}

}

}

}

}

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand()=="初级"){

row=9;

col=9;

bon=10;

a1=new int[bon];

b=bon;

//setSize(297,377);

a = new int[row+2][col+2];

this.remove(p2);

timer.stop();

b1.setText("10");

b2.setText("0");

b3.setText("");

btns=new JButton[row*col];

p2=new JPanel();

p2.setLayout(new GridLayout(row,col,0,0));

for(int i=0;irow*col;i++){

btns[i]=new JButton(" ");

btns[i].setMargin(new Insets(0,0,0,0));

btns[i].setFont(new Font(null,Font.BOLD,25));

btns[i].addActionListener(this);

btns[i].addMouseListener(new NormoreMouseEvent());

p2.add(btns[i]);

}

contentPane.add(p2,BorderLayout.CENTER);

//setSize(297,377);

this.pack();

for(int i=0;irow*col;i++){

btns[i].setText(" ");

btns[i].setEnabled(true);

}

for(int i=0;irow+2;i++){

for(int j=0;jcol+2;j++){

a[i][j]=0;

}

}

}else if(e.getActionCommand()=="中级"){

row=16;

col=16;

bon=40;

//setSize(33*col,33*row+80);

a1=new int[bon];

a = new int[row+2][col+2];

b=bon;

this.remove(p2);

timer.stop();

b1.setText("40");

b2.setText("0");

b3.setText("");

btns=new JButton[row*col];

p2=new JPanel();

p2.setLayout(new GridLayout(row,col,0,0));

for(int i=0;irow*col;i++){

btns[i]=new JButton(" ");

btns[i].setMargin(new Insets(0,0,0,0));

btns[i].setFont(new Font(null,Font.BOLD,25));

btns[i].addActionListener(this);

btns[i].addMouseListener(new NormoreMouseEvent());

p2.add(btns[i]);

}

contentPane.add(p2,BorderLayout.CENTER);

this.pack();

//setSize(33*col,33*row+80);

for(int i=0;irow*col;i++){

btns[i].setText("");

btns[i].setEnabled(true);

}

for(int i=0;irow+2;i++){

for(int j=0;jcol+2;j++){

a[i][j]=0;

}

}

}else if(e.getActionCommand()=="高级"){

row=16;

col=32;

bon=99;

setSize(33*col,33*row+80);

a1=new int[bon];

a = new int[row+2][col+2];

b=bon;

this.remove(p2);

timer.stop();

b1.setText("99");

b2.setText("0");

b3.setText("");

btns=new JButton[row*col];

p2=new JPanel();

p2.setLayout(new GridLayout(row,col,0,0));

for(int i=0;irow*col;i++){

btns[i]=new JButton(" ");

btns[i].setMargin(new Insets(0,0,0,0));

btns[i].setFont(new Font(null,Font.BOLD,25));

btns[i].addActionListener(this);

btns[i].addMouseListener(new NormoreMouseEvent());

p2.add(btns[i]);

}

contentPane.add(p2,BorderLayout.CENTER);

//setSize(33*col,33*row+80);

this.pack();

for(int i=0;irow*col;i++){

btns[i].setText("");

btns[i].setEnabled(true);

}

for(int i=0;irow+2;i++){

for(int j=0;jcol+2;j++){

a[i][j]=0;

}

}

}

if(e.getSource()==btn){

timer.start();

b=bon;

b3.setText("");

//System.out.println(bon);

//清空

for(int i=0;irow*col;i++){

btns[i].setText("");

btns[i].setEnabled(true);

}

for(int i=0;irow+2;i++){

for(int j=0;jcol+2;j++){

a[i][j]=0;

}

}

//产生随机数

for(int i=0;ibon;i++)

{ int p=1;

int m=(int)(Math.random()*row*col);

while(p==1){

int l=1;

int j;

for( j=0;jil==1;j++){

if(a1[j]==m){

m=(int)(Math.random()*row*col);

l=0;

}

}

if(j==i){

a1[i]=m;

p=0;

}

}

}

b1.setText(bon+"");

b2.setText("0");

//布雷

for(int i=0;ibon;i++){

int x=(a1[i]/col+1);

int y=(a1[i]%col+1);

a[x][y]=100;

}

for(int i=0;irow+2;i++){

for(int j=0;jcol+2;j++){

if(i==0||j==0||i==row+1||j==col+1){

a[i][j]=0;

}

}

}

for(int i=1;i=row;i++){

for(int j=1;j=col;j++){

if(a[i][j]!=100){

for(int l=j-1;l=j+1;l++){

if(a[i-1][l]==100){

a[i][j]++;

}

if(a[i+1][l]==100){

a[i][j]++;

}

}

if(a[i][j-1]==100){

a[i][j]++;

}

if(a[i][j+1]==100){

a[i][j]++;

}

}

}

}

}

if(e.getSource()==timer)

{

String time=b2.getText().trim();

int t=Integer.parseInt(time);

//System.out.println(t);

if(t=600){

timer.stop();

}else{

t++;

b2.setText(t+"");

}

}

for(int i=0;icol*row;i++){

if(btns[i].getText()!="★")

{

int x=i/col+1;

int y=i%col+1;

if(e.getSource()==btns[i]a[x][y]==100){

btns[i].setText("★");

btns[i].setEnabled(false);

a[x][y]=10;

for(int k=0;kcol*row;k++){

int m1=k/col+1;

int n1=k%col+1;

if(a[m1][n1]!=10btns[k].getText()=="★"){

btns[k].setText("*o*");

}

}

for(int j=0;jcol*row;j++){

int m=j/col+1;

int n=j%col+1;

if(a[m][n]==100){

btns[j].setText("★");

btns[j].setEnabled(false);

b3.setText("你输了 !!");

}

btns[j].setEnabled(false);

a[m][n]=10;

}

timer.stop();

}

else if(e.getSource()==btns[i]){

if(a[x][y]==0){

out(a,btns,e,i,x,y);

a[x][y]=10;

btns[i].setEnabled(false);

}

if(a[x][y]!=0a[x][y]!=10){

btns[i].setText(a[x][y]+"");

btns[i].setEnabled(false);

a[x][y]=10;

}

}

}else if(btns[i].getText()=="★"){

}

}

}

class NormoreMouseEvent extends MouseAdapter{

public void mouseClicked(MouseEvent e) {

System.out.println(b);

for(int i=0;icol*row;i++){

int x1=i/col+1;

int y1=i%col+1;

if(e.getSource()==btns[i]btns[i].getText()!="★"a[x1][y1]!=10)

{

if(e.getButton()==MouseEvent.BUTTON3){

btns[i].setText("★");

b--;

if(b==0){

int flag=0;

for(int j=0;jcol*row;j++){

int x=j/col+1;

int y=j%col+1;

if(a[x][y]==100btns[j].getText()=="★"){

flag++;

}

}

if(flag==bon){

timer.stop();

b3.setText("你赢了!");

}

}

b1.setText(b+"");

}

}else if(e.getSource()==btns[i]btns[i].getText()=="★"a[x1][y1]!=-1){

if(e.getButton()==MouseEvent.BUTTON3){

btns[i].setText("");

b++;

if(bbon){

b1.setText(bon+"");

}

else{

b1.setText(b+"");

}

btns[i].setEnabled(true);

}

}

}

}

}

}

在百度知道上找到了网友的java石头剪刀布游戏的代码,但是理解不来,希望大神能帮我把程序每句话都注释

public class Test {

private static Scanner sc;

private static Random rad;

private static final String[] FINGERS = { "剪刀", "石头", "布" };

private static int win = 0, loose = 0, draw = 0;

public static void main(String[] args) {

// 捕获用户输入类

sc = new Scanner(System.in);

//产生随机数的类

rad = new Random();

//一进来就让用户输入开始游戏,直到输入E, 退出游戏。

while (true) {

System.out.println("~~~~~~~~~~~~剪刀石头布游戏,输入E可以退出~~~~~~~~~~~");

System.out.println("请选择你要出什么?Z——剪刀,X——石头,C——布");

//获取用户输入的字符

String command = sc.nextLine();

//输入字符转换 手势没发比较大小 就把字符转换成数字比较大小

int playerFinger = getValue(command);

if (playerFinger == -1) {//用户输入的是E == -1 表示退出

break;

}

else if (playerFinger == 3) {//用户输入的是 E Z X C 之外的字符 == 3 表示输入的不是合法的,然后继续让用户重新输入

System.out.println("输入错误,请参考说明!");

continue;

}

//当用户输入 ZXC 中的字符时才会到这一步

//用户的输入转换成了 0,1,2

//finger[0]="剪刀" finger[1]="石头" finger[2]="布" 程序第一行定义好的

System.out.println("你出的是" + FINGERS[playerFinger]);

//生成随机整数 3以内的(1,2,3)

int cpuFinger = rad.nextInt(3);

//finger[0]="剪刀" finger[1]="石头" finger[2]="布" 程序第一行定义好的

System.out.println("计算机出的是" + FINGERS[cpuFinger]);

//比较两个数字,你可以理解 0 就是剪刀,1是石头 2 是布

int result = playerFinger - cpuFinger;

if (0 == result) {//结果等于零说明两个数字一样

System.out.println("平局!");

draw++;

}

else if (-1 == result || 2 == result) {// 0-1=-1,1-2=-1,2-0=2 表示你输的三种情况 ;0 就是剪刀,1是石头 2 是布

System.out.println("你输了!");

loose++;

}

else {//剩下的情况除了平局,输局 肯定就是你赢了

System.out.println("你赢了!");

win++;

}

}

System.out.println("游戏结束!\r\n游戏统计次数");

System.out.println(String.format("赢:%d\r\n输:%d\r\n平局:%d", win, loose, draw));

}

/**

* 用户输入字符进行转换

* 输入字符 E 就返回-1 作为后续判断,表示退出程序

* 输入字符 Z 返回 0 代表剪刀

* 输入字符 X 返回 1 代表石头

* 输入字符 C 返回 2 代表布

* 输入其他字符 返回3 ,表明输入的不是符合规则的手势(0,1,2)

*

* @param command

* @return

*/

private static int getValue(String command) {

if (command.equalsIgnoreCase("E")) {

return -1;

}

if (command.equalsIgnoreCase("Z")) {

return 0;

}

if (command.equalsIgnoreCase("X")) {

return 1;

}

if (command.equalsIgnoreCase("C")) {

return 2;

}

return 3;

}

}

求"贪吃蛇"小游戏JAVA源代码一份

贪吃蛇

import java.awt.*;

import java.awt.event.*;

public class GreedSnake //主类

{

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new MyWindow();

}

}

class MyPanel extends Panel implements KeyListener,Runnable//自定义面板类,继承了键盘和线程接口

{

Button snake[]; //定义蛇按钮

int shu=0; //蛇的节数

int food[]; //食物数组

boolean result=true; //判定结果是输 还是赢

Thread thread; //定义线程

static int weix,weiy; //食物位置

boolean t=true; //判定游戏是否结束

int fangxiang=0; //蛇移动方向

int x=0,y=0; //蛇头位置

MyPanel()

{

setLayout(null);

snake=new Button[20];

food=new int [20];

thread=new Thread(this);

for(int j=0;j20;j++)

{

food[j]=(int)(Math.random()*99);//定义20个随机食物

}

weix=(int)(food[0]*0.1)*60; //十位*60为横坐标

weiy=(int)(food[0]%10)*40; //个位*40为纵坐标

for(int i=0;i20;i++)

{

snake[i]=new Button();

}

add(snake[0]);

snake[0].setBackground(Color.black);

snake[0].addKeyListener(this); //为蛇头添加键盘监视器

snake[0].setBounds(0,0,10,10);

setBackground(Color.cyan);

}

public void run() //接收线程

{

while(t)

{

if(fangxiang==0)//向右

{

try

{

x+=10;

snake[0].setLocation(x, y);//设置蛇头位置

if(x==weixy==weiy) //吃到食物

{

shu++;

weix=(int)(food[shu]*0.1)*60;

weiy=(int)(food[shu]%10)*40;

repaint(); //重绘下一个食物

add(snake[shu]); //增加蛇节数和位置

snake[shu].setBounds(snake[shu-1].getBounds());

}

thread.sleep(100); //睡眠100ms

}

catch(Exception e){}

}

else if(fangxiang==1)//向左

{

try

{

x-=10;

snake[0].setLocation(x, y);

if(x==weixy==weiy)

{

shu++;

weix=(int)(food[shu]*0.1)*60;

weiy=(int)(food[shu]%10)*40;

repaint();

add(snake[shu]);

snake[shu].setBounds(snake[shu-1].getBounds());

}

thread.sleep(100);

}

catch(Exception e){}

}

else if(fangxiang==2)//向上

{

try

{

y-=10;

snake[0].setLocation(x, y);

if(x==weixy==weiy)

{

shu++;

weix=(int)(food[shu]*0.1)*60;

weiy=(int)(food[shu]%10)*40;

repaint();

add(snake[shu]);

snake[shu].setBounds(snake[shu-1].getBounds());

}

thread.sleep(100);

}

catch(Exception e){}

}

else if(fangxiang==3)//向下

{

try

{

y+=10;

snake[0].setLocation(x, y);

if(x==weixy==weiy)

{

shu++;

weix=(int)(food[shu]*0.1)*60;

weiy=(int)(food[shu]%10)*40;

repaint();

add(snake[shu]);

snake[shu].setBounds(snake[shu-1].getBounds());

}

thread.sleep(100);

}

catch(Exception e){}

}

int num1=shu;

while(num11)//判断是否咬自己的尾巴

{

if(snake[num1].getBounds().x==snake[0].getBounds().xsnake[num1].getBounds().y==snake[0].getBounds().y)

{

t=false;

result=false;

repaint();

}

num1--;

}

if(x0||x=this.getWidth()||y0||y=this.getHeight())//判断是否撞墙

{

t=false;

result=false;

repaint();

}

int num=shu;

while(num0) //设置蛇节位置

{

snake[num].setBounds(snake[num-1].getBounds());

num--;

}

if(shu==15) //如果蛇节数等于15则胜利

{

t=false;

result=true;

repaint();

}

}

}

public void keyPressed(KeyEvent e) //按下键盘方向键

{

if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右键

{

if(fangxiang!=1)//如果先前方向不为左

fangxiang=0;

}

else if(e.getKeyCode()==KeyEvent.VK_LEFT)

{ if(fangxiang!=0)

fangxiang=1;

}

else if(e.getKeyCode()==KeyEvent.VK_UP)

{ if(fangxiang!=3)

fangxiang=2;

}

else if(e.getKeyCode()==KeyEvent.VK_DOWN)

{ if(fangxiang!=2)

fangxiang=3;

}

}

public void keyTyped(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

public void paint(Graphics g) //在面板上绘图

{

int x1=this.getWidth()-1;

int y1=this.getHeight()-1;

g.setColor(Color.red);

g.fillOval(weix, weiy, 10, 10);//食物

g.drawRect(0, 0, x1, y1); //墙

if(t==falseresult==false)

g.drawString("GAME OVER!", 250, 200);//输出游戏失败

else if(t==falseresult==true)

g.drawString("YOU WIN!", 250, 200);//输出游戏成功

}

}

class MyWindow extends Frame implements ActionListener//自定义窗口类

{

MyPanel my;

Button btn;

Panel panel;

MyWindow()

{

super("GreedSnake");

my=new MyPanel();

btn=new Button("begin");

panel=new Panel();

btn.addActionListener(this);

panel.add(new Label("begin后请按Tab键选定蛇"));

panel.add(btn);

panel.add(new Label("按上下左右键控制蛇行动"));

add(panel,BorderLayout.NORTH);

add(my,BorderLayout.CENTER);

setBounds(100,100,610,500);

setVisible(true);

validate();

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e)//按下begin按钮

{

if(e.getSource()==btn)

{

try

{

my.thread.start(); //开始线程

my.validate();

}

catch(Exception ee){}

}

}

}

求一个简单又有趣的JAVA小游戏代码

具体如下:

连连看的小源码

package Lianliankan;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组

JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮

JLabel fractionLable=new JLabel("0"); //分数标签

JButton firstButton,secondButton; //

分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮

int grid[][] = new int[8][7];//储存游戏按钮位置

static boolean pressInformation=false; //判断是否有按钮被选中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标

int i,j,k,n;//消除方法控制

代码(code)是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系。

对于字符和Unicode数据的位模式的定义,此模式代表特定字母、数字或符号(例如 0x20 代表一个空格,而 0x74 代表字符“t”)。一些数据类型每个字符使用一个字节;每个字节可以具有 256 个不同的位模式中的一个模式。

在计算机中,字符由不同的位模式(ON 或 OFF)表示。每个字节有 8 位,这 8 位可以有 256 种不同的 ON 和 OFF 组合模式。对于使用 1 个字节存储每个字符的程序,通过给每个位模式指派字符可表示最多 256 个不同的字符。2 个字节有 16 位,这 16 位可以有 65,536 种唯一的 ON 和 OFF 组合模式。使用 2 个字节表示每个字符的程序可表示最多 65,536 个字符。

单字节代码页是字符定义,这些字符映射到每个字节可能有的 256 种位模式中的每一种。代码页定义大小写字符、数字、符号以及 !、@、#、% 等特殊字符的位模式。每种欧洲语言(如德语和西班牙语)都有各自的单字节代码页。

虽然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失。

如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符。而且,在代码页间不停地转换需要花费大量的处理时间。

java 源代码注释

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class GameTest extends JFrame implements ActionListener{

/*

* 新建一个主面板(这个类可能是自定义的,本程序和API中没有)。

*/

MainPanel j=new MainPanel();

JButton jPreview;

JLabel label;

Container container;

JPanel panel;

/**

* 主函数

* @param args

*/

public static void main(String[] args) {

//运行程序

new GameTest();

}

/**

* 构造函数。

*

*/

public GameTest()

{

//新建一个标题为“拼图”的窗口

JFrame fr =new JFrame("拼图");

//获取窗口容器。

container=fr.getContentPane();

//创建菜单条

JMenuBar jMenuBar=new JMenuBar();

//以下初始化菜单,并且设置快捷键和添加监听器。

JMenu jMenuGame=new JMenu("游戏(G)");

jMenuGame.setMnemonic('g');

JMenuItem jMenuItemStart = new JMenuItem("开始(S)");

jMenuItemStart.setMnemonic('s');

jMenuItemStart.addActionListener(this);

JMenuItem jMenuItemExit=new JMenuItem("退出(E)");

jMenuItemExit.setMnemonic('e');

jMenuItemExit.addActionListener(this);

jMenuGame.add(jMenuItemStart);

jMenuGame.add(jMenuItemExit);

//初始化按钮并设置快捷键和添加监听器

JButton jChoice=new JButton("选图(X)");

jChoice.setMnemonic('x');

jChoice.addActionListener(this);

jPreview=new JButton("预览(P)");

jPreview.setMnemonic('p');

jPreview.addActionListener(this);

//将菜单和按钮添加到菜单条中

jMenuBar.add(jMenuGame);

jMenuBar.add(jChoice);

jMenuBar.add(jPreview);

//将菜单条设为该窗口的主菜单

fr.setJMenuBar(jMenuBar);

//将主面板添加到该窗口的容器中。

container.add(j);

//设置大小

fr.setSize(315,360 );

fr.setVisible(true);

//设置默认关闭方式。

fr.setDefaultCloseOperation(3);

}

/**

* 事件处理函数。

*/

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand()=="开始(S)")

{

j.Start();

}

if(e.getActionCommand()=="预览(P)")

{

j.setVisible(false);

panel=new JPanel();

Icon icon=new ImageIcon("pictrue/pic"+"_"+MainPanel.pictureID+".jpg");

label=new JLabel(icon);

label.setBounds(300, 300, 0, 0);

panel.add(label);

panel.setSize(300, 300);

panel.setVisible(true);

this.container.add(panel);

jPreview.setText("返回(P)");

}

if(e.getActionCommand()=="返回(P)")

{

panel.setVisible(false);

j.setVisible(true);

j.repaint();

jPreview.setText("预览(P)");

}

if(e.getActionCommand()=="退出(E)")

{

System.exit(0);

}

if(e.getActionCommand()=="选图(X)")

{

//初始化选择框,并提供选择。

Choice pic = new Choice();

pic.add("七里香");

pic.add("依然范特西");

pic.add("八度空间");

pic.add("十一月的肖邦");

pic.add("魔杰座");

pic.add("叶惠美");

pic.add("我很忙");

int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION);

if(i==JOptionPane.YES_OPTION)

{

//选择图片

MainPanel.pictureID=pic.getSelectedIndex()+1;

j.removeAll();

j.reLoadPicture();

j.repaint();

}

}

}

}

求java小游戏源代码

表1. CheckerDrag.java

// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}

关于java小游戏源代码带注释和简单的java程序代码带注释的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/2723.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载