java华容道代码的思路 数字华容道java代码

java编程的思路

java基本编程思路:

成都创新互联公司,专注为中小企业提供官网建设、营销型网站制作、成都响应式网站建设、展示型成都网站制作、成都做网站等服务,帮助中小企业通过网站体现价值、有效益。帮助企业快速建站、解决网站建设与网站营销推广问题。

第一步:写框架(内层和外层):

public class 类名{//外层

public static void main(String[ ] args){//内层

}

}

第二步:看题目中是否有“接收”“输入”“录入”等字眼,则导入util包,创建Scanner扫描仪对象:

import  java.util.Scanner;//写在文件的第一行

Scanner  input = new Scanner(System.in);//写在main方法中的第一行

第三步:编写main方法中的主体代码

A、接收、输入、录入:

System.out.println(“提示信息”);

数据类型  变量名=__扫描数据___;

说明:扫描不同类型的数据,使用不同的扫描方法。以下介绍了常用的类型的扫描方法。

int型     使用nextInt();

String型     使用next ()或nextLine();

double型      使用nextDouble();

B、计算:算术运算等

C、输出:

System.out.println(输出的内容);

或者

System.out.println(“提示内容:”+输出的内容);

用java编程 华容道游戏

import java.awt.*;

import java.awt.event.*;

public class MoveExample

{

public static void main(String args[])

{

new Hua_Rong_Road();

}

}

class Person extends Button implements FocusListener

{

int number;

Color c = new Color(255,245,170);

Person(int number,String s)

{

super(s);

setBackground(c);

this.number = number;

c = getBackground();

addFocusListener(this);

}

public void focusGained(FocusEvent e)

{

setBackground(Color.red);

}

public void focusLost(FocusEvent e)

{

setBackground(c);

}

}

class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener

{

Person person[] = new Person[10];

Button left,right,above,below;

Button restart = new Button("重新开始");

public Hua_Rong_Road()

{

init();

setBounds(100,100,320,360);

setVisible(true);

validate();

addWindowListener( new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

}

public void init()

{

setLayout(null);

add(restart);

restart.setBounds(100,320,120,25);

restart.addActionListener(this);

String name[] = {"曹操","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵"};

for(int k = 0;kname.length;k++)

{

person[k] = new Person(k,name[k]);

person[k].addMouseListener(this);

person[k].addKeyListener(this);

add(person[k]);

}

person[0].setBounds(104,54,100,100);

person[1].setBounds(104,154,100,50);

person[2].setBounds(54,154,50,100);

person[3].setBounds(204,154,50,100);

person[4].setBounds(54,54,50,100);

person[5].setBounds(204,54,50,100);

person[6].setBounds(54,254,50,50);

person[7].setBounds(204,254,50,50);

person[8].setBounds(104,204,50,50);

person[9].setBounds(154,204,50,50);

person[9].requestFocus();

left = new Button();

right = new Button();

above = new Button();

below = new Button();

add(left);

add(right);

add(above);

add(below);

left.setBounds(49,49,5,260);

right.setBounds(254,49,5,260);

above.setBounds(49,49,210,5);

below.setBounds(49,304,210,5);

validate();

}

public void keyTyped(KeyEvent e){}

public void keyReleased(KeyEvent e){}

public void keyPressed(KeyEvent e)

{

Person man = (Person)e.getSource();

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

{

go(man,below);

}

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

{

go(man,above);

}

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

{

go(man,left);

}

if(e.getKeyCode()==KeyEvent.VK_RIGHT)

{

go(man,right);

}

}

public void mousePressed(MouseEvent e)

{

Person man = (Person)e.getSource();

int x = -1,y = -1;

x = e.getX();

y = e.getY();

int w = man.getBounds().width;

int h = man.getBounds().height;

if(yh/2)

{

go(man,below);

}

if(yh/2)

{

go(man,above);

}

if(xw/2)

{

go(man,left);

}

if(xw/2)

{

go(man,right);

}

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void go(Person man,Button direction)

{

boolean move = true;

Rectangle manRect = man.getBounds(); //什么意思??

int x = man.getBounds().x; //又不懂了

int y = man.getBounds().y;

if(direction==below)

{

y = y+50;

}

else if(direction==above)

{

y = y-50;

}

else if(direction==left)

{

x = x-50;

}

else if(direction==right)

{

x = x+50;

}

manRect.setLocation(x,y);

Rectangle directionRect = direction.getBounds();

for(int k = 0;k10;k++)

{

Rectangle personRect = person[k].getBounds();

if((manRect.intersects(personRect))(man.number!=k))

{

move = false;

}

}

if(manRect.intersects(directionRect))

{

move = false;

}

if(move==true)

{

man.setLocation(x,y);

}

}

public void actionPerformed(ActionEvent e)

{

dispose();

new Hua_Rong_Road();

}

}

这是我们课本上的,颜色不一样,其他的都差不多,不过是用awt组件写的,你应该是要用swing写的吧,照这个改改吧...

用java设计一个华容道游戏

import java.awt.*;

import java.awt.event.*;

public class MoveExample //主类

{

public static void main(String args[]) //定义主方法

{

new Hua_Rong_Road(); //创建对象

}

}

class Person extends Button implements FocusListener

{

int number;

Color c = new Color(128,128,128);

Person(int number,String s)//构造方法

{

super(s);//调用父类s的构造方法

setBackground(c);//设置组件的背景色

this.number = number;//调用当前的number

c = getBackground();

addFocusListener(this);//添加焦点事件监听器

}

public void focusGained(FocusEvent e)//焦点事件触发

{

setBackground(Color.blue);

}

public void focusLost(FocusEvent e)//焦点事件失去

{

setBackground(c);

}

}

class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener

{

Person person[] = new Person[10];//person类的数组

Button left,right,above,below;

Button restart = new Button("重新开始");

public Hua_Rong_Road() //华容道的构造方法

{

init(); //初始化

setBounds(100,100,320,360);//设置窗口在屏幕上出现位置,和窗口大小

setVisible(true);//设置窗口可见

setResizable(true);//设置窗口可调节

validate();//刷新

addWindowListener( new WindowAdapter()//获得窗口事件监视器

{

public void windowClosing(WindowEvent e)//窗口正在被关闭时,窗口监视器调用该方法

{

System.exit(0);

}

}

);

}

public void init()

{

setLayout(null);//设置默认布局

add(restart);//添加重新开始

restart.setBounds(100,320,120,25);//重新开始按钮大小

restart.addActionListener(this);//事件源获得监视器

String name[] = {"曹操","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵"};

for(int k = 0;kname.length;k++)

{

person[k] = new Person(k,name[k]);//给按钮添加名字

person[k].addMouseListener(this);//每个按钮都注册鼠标事件

person[k].addKeyListener(this);//每个按钮都注册键盘事件

add(person[k]);//添加人物

}

person[0].setBounds(104,54,100,100);

person[1].setBounds(104,154,100,50);

person[2].setBounds(54,154,50,100);

person[3].setBounds(204,154,50,100);

person[4].setBounds(54,54,50,100);

person[5].setBounds(204,54,50,100);

person[6].setBounds(54,254,50,50);

person[7].setBounds(204,254,50,50);

person[8].setBounds(104,204,50,50);

person[9].setBounds(154,204,50,50);//为每个人物按钮设置位置和大小

person[9].requestFocus();//把焦点先设置在这个按钮上

left = new Button();//画出游戏界面边框,并用定义的left,right,above,below控制大小

right = new Button();

above = new Button();

below = new Button();

add(left);

add(right);

add(above);

add(below);

left.setBounds(49,49,5,260);

right.setBounds(254,49,5,260);

above.setBounds(49,49,210,5);

below.setBounds(49,304,210,5);

validate();//刷新

} //完成界面布局

public void keyTyped(KeyEvent e){}

public void keyReleased(KeyEvent e){}

public void keyPressed(KeyEvent e)//响应键盘事件,按键,释放键,按下和释放组合

{

Person man = (Person)e.getSource();//获得事件源

if(e.getKeyCode()==KeyEvent.VK_DOWN)//响应用户按下方向光标的操作;用KeyEvent类中的getkeycode()判断哪个键被按下

{

go(man,below); //go方法控制移动

}

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

{

go(man,above);

}

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

{

go(man,left);

}

if(e.getKeyCode()==KeyEvent.VK_RIGHT)

{

go(man,right);

}

}

public void mousePressed(MouseEvent e)

{

Person man = (Person)e.getSource();

int x = -1,y = -1;

x = e.getX();

y = e.getY();

int w = man.getBounds().width;

int h = man.getBounds().height;

if(yh/2)

{

go(man,below);

}

if(yh/2)

{

go(man,above);

}

if(xw/2)

{

go(man,left);

}

if(xw/2)

{

go(man,right);

}

}

public void mouseReleased(MouseEvent e){}//鼠标事件

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void go(Person man,Button direction)

{

boolean move = true;

Rectangle manRect = man.getBounds();

int x = man.getBounds().x;

int y = man.getBounds().y;

if(direction==below)//向各个方向移动

{

y = y+50;

}

else if(direction==above)

{

y = y-50;

}

else if(direction==left)

{

x = x-50;

}

else if(direction==right)

{

x = x+50;

}

manRect.setLocation(x,y);

Rectangle directionRect = direction.getBounds();

for(int k = 0;k10;k++)

{

Rectangle personRect = person[k].getBounds();

if((manRect.intersects(personRect))(man.number!=k))//如果覆盖就不移动

{

move = false;

}

}

if(manRect.intersects(directionRect))

{

move = false;

}

if(move==true)

{

man.setLocation(x,y);

}

}

public void actionPerformed(ActionEvent e)

{

dispose();

new Hua_Rong_Road();

}

}


网页题目:java华容道代码的思路 数字华容道java代码
网页地址:http://abwzjs.com/article/dosdcge.html