用java设计一个华容道游戏

用java设计一个华容道游戏,第1张

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 = 0k<name.lengthk++)

{

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(y>h/2)

{

go(man,below)

}

if(y<h/2)

{

go(man,above)

}

if(x<w/2)

{

go(man,left)

}

if(x>w/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 = 0k<10k++)

{

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()

}

}

你程序都不弄来,怎么帮你完善啊??给你一个我自己做的局域网qq聊天工具吧!现在可以实现私聊和群聊,可以设置自己的昵称等一些简单的功能,你看能不能帮你些忙!

import java.net.*

import java.awt.*

import javax.swing.*

import java.awt.event.*

import java.util.*

import javax.swing.JDialog

public class QQ

extends JFrame

implements ActionListener,Runnable{ //定义qq界面所需的组件

static JPanel p1 = new JPanel(), p2 = new JPanel()

TextArea output = new TextArea("", 20, 18, TextArea.SCROLLBARS_BOTH),

input = new TextArea("", 20, 18, TextArea.SCROLLBARS_VERTICAL_ONLY)

JButton b_biaoqing = new JButton("表情")

JButton b_zhiti = new JButton("字体")

JButton b_zhitiys = new JButton("字体颜色")

JButton b_jilu = new JButton("清空消息")

JButton b_fasong = new JButton("发送")

JLabel lb1 = new JLabel(" 对方IP")

JTextField IPAdd = new JTextField("192.168.4.88", 15)

String s,kongzhi//用于控制和判断的变量

int port = 5858

InetAddress group = null

MulticastSocket socket = null

//群聊和私聊的旅基线程

Thread thread1=new Thread(this)

Thread thread2=new Thread(this)

JButton siliao = new JButton("私聊")

JButton qunliao = new JButton("群稿镇顷聊")

Color ys = new Color(157, 242, 173)

caidan cd = new caidan()//获得键陆菜单对象

public QQ() { //布局qq界面

super("小小聊天室")

setMenuBar(cd)

setResizable(false)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

b_jilu.addActionListener(this)

b_fasong.addActionListener(this)

b_biaoqing.addActionListener(this)

b_zhiti.addActionListener(this)

b_zhitiys.addActionListener(this)

siliao.addActionListener(this)

qunliao.addActionListener(this)

Container cc = getContentPane()

setBounds(150, 150, 600, 510)

cc.setLayout(new BorderLayout())

output.setBackground(Color.white)

output.setForeground(Color.blue)

output.setBounds(4, 0, 420, 250)

output.setFont(new Font("楷体", Font.PLAIN, 14))

output.setEditable(false)

p1.setLayout(null)

p1.setBackground(new Color(220, 220, 220))

p1.add(output)

b_biaoqing.setBounds(4, 254, 60, 30)

p1.add(b_biaoqing)

b_zhiti.setBounds(65, 254, 60, 30)

p1.add(b_zhiti)

b_zhitiys.setBounds(126, 254, 90, 30)

p1.add(b_zhitiys)

input.setFont(new Font("楷体", Font.PLAIN, 14))

input.setBackground(Color.white)

input.setForeground(Color.blue)

input.setBounds(4, 290, 420, 125)

p1.add(input)

b_jilu.setBounds(4, 420, 120, 30)

p1.add(b_jilu)

b_fasong.setBounds(360, 420, 60, 30)

p1.add(b_fasong)

p1.setBackground(ys)

cc.add(p1, "Center")

lb1.setFont(new Font("楷体", Font.BOLD, 18))

lb1.setBackground(ys)

IPAdd.setEditable(false)

p2.setBackground(ys)

p2.setLayout(new GridLayout(10, 1, 1, 10))

p2.add(lb1, "Center")

p2.add(IPAdd)

p2.add(siliao)

p2.add(qunliao)

cc.add(p2, "East")

setVisible(true)

}

//执行按钮动作的方法

public void actionPerformed(ActionEvent e) {

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

String ss = input.getText()

if (kongzhi == "sl"){

if (ss.equals("")) {

AboutQQ test = new AboutQQ(this)

}

else {

sendData1()

}

}

else if (kongzhi == "ql")

if (ss.equals("")) {

AboutQQ test = new AboutQQ(this)

}

else {

sendData2()

}

else

{

JOptionPane.showMessageDialog(this,"对不起!你还没选择聊天方式,请先选择聊天方式!","提醒",JOptionPane.WARNING_MESSAGE)

}

}

else if (e.getSource() == b_zhiti) {

ziti zt = new ziti(this)

input.setFont(zt.f)

output.setFont(zt.f)

}

else if (e.getSource() == b_zhitiys) {

Color newcolor = JColorChooser.showDialog(this, "调色板",

input.getForeground())

input.setForeground(newcolor)

output.setForeground(newcolor)

}

else if (e.getSource() == b_jilu) {

output.setText("")

}

else if (e.getSource() == siliao) {

String ip = JOptionPane.showInputDialog("请输入对方的IP地址:","192.168.4.13")

if (ip!=null) {

IPAdd.setText(ip)

}

kongzhi="sl"

if(!(thread1.isAlive())){

thread1=new Thread(this)

}

try{

thread1.start()

}catch(Exception eee){

}

}

else if (e.getSource() == qunliao){

thread1.interrupt()

int n=JOptionPane.showConfirmDialog(null,"你确定要群聊吗?你的消息会被所有人看到!!","警告",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE)

JOptionPane.showMessageDialog(this,"你最好先设置好你的昵称,然后再进行聊天!","温馨提醒",JOptionPane.WARNING_MESSAGE)

if(n==JOptionPane.YES_OPTION)

kongzhi="ql"

if(!(thread2.isAlive())){

thread2=new Thread(this)

}

try{

thread2.start()

}catch(Exception eee){}

}

}

//发送及接受数据的方法

void sendData1(){

try {

Calendar rightNow = Calendar.getInstance()//获取当前系统日期和时间

int hour = rightNow.get(Calendar.HOUR_OF_DAY)//获取当前时间的整点数

int year = rightNow.get(Calendar.YEAR)

int month = rightNow.get(Calendar.MONTH) + 1

int day = rightNow.get(Calendar.DATE)

int minute = rightNow.get(Calendar.MINUTE)

int second = rightNow.get(Calendar.SECOND)

String msg = input.getText()

if (msg.equals("")) {

return

}

input.setText("")

String ad = IPAdd.getText()

InetAddress tea = InetAddress.getLocalHost()

String asd = tea.getHostAddress()//发送方的IP地址

output.append(cd.nicheng1 + "(" + asd + ") " + year

+ "-" + month + "-"

+ day + " "

+ hour + ":"

+ minute + ":"

+ second + "\n" + " " + msg

+ "\n")

msg = cd.nicheng2 + "(" + asd + ") " + year

+ "-" + month + "-"

+ day + " "

+ hour + ":"

+ minute + ":"

+ second + "\n" + " " + msg + "\n"

InetAddress address = InetAddress.getByName(ad)

byte[] message = msg.getBytes()

DatagramPacket packet = new DatagramPacket(message, message.length,

address,

9999)

DatagramSocket socket = new DatagramSocket()

socket.send(packet)

}

catch (Exception e) {

}

}

void sendData2(){

Calendar rightNow = Calendar.getInstance()//获取当前系统日期和时间

int hour = rightNow.get(Calendar.HOUR_OF_DAY)//获取当前时间的整点数

int year = rightNow.get(Calendar.YEAR)

int month = rightNow.get(Calendar.MONTH) + 1

int day = rightNow.get(Calendar.DATE)

int minute = rightNow.get(Calendar.MINUTE)

int second = rightNow.get(Calendar.SECOND)

try {

group = InetAddress.getByName("239.255.8.0")

socket = new MulticastSocket(port)

socket.setTimeToLive(1)

socket.joinGroup(group)

s = input.getText()

if (s.equals("")) {

return

}

String ss=(cd.nicheng1+" " + year

+ "-" + month + "-"

+ day + " "

+ hour + ":"

+ minute + ":"

+ second + "\n" + " " + s

+ "\n")

input.setText("")

DatagramPacket packet = null

byte date[] = ss.getBytes()

packet = new DatagramPacket(date, date.length, group, port)

socket.send(packet)

}

catch (Exception e) {

System.out.println("Error:" + e)

}

}

//等待接受数据的方法

public void run() {

if(Thread.currentThread()==thread1){//负责私聊的线程

try {

byte[] buffer = new byte[1024]

DatagramPacket packet = new DatagramPacket(buffer, buffer.length)

DatagramSocket socket = new DatagramSocket(9999)

while (true) {

socket.receive(packet)

String s = new String(packet.getData(), 0, packet.getLength())

output.append(s + "\n")

packet = new DatagramPacket(buffer, buffer.length)

}

}

catch (Exception e) {

}

}

if(Thread.currentThread()==thread2){//负责群聊的线程

try {

while (true) {

group=null

group = InetAddress.getByName("239.255.8.0")

socket = new MulticastSocket(port)

socket.joinGroup(group)

byte[] buffer = new byte[8192]

DatagramPacket packet=null

packet = new DatagramPacket(buffer, buffer.length, group, port)

socket.receive(packet)

String message = new String(packet.getData(), 0, packet.getLength())

output.append(message)

packet = new DatagramPacket(buffer, buffer.length)

}

}

catch (Exception e) {

}

}

}

//主方法

public static void main(String args[]) {

new QQ()

}

}

//提示对话框类

class AboutQQ

implements ActionListener {

private JLabel label

private JButton queding

private JDialog dialog

public AboutQQ(JFrame f) {

label = new JLabel("不能发送空消息")

queding = new JButton("Yes")

dialog = new JDialog(f, "提示", true)

dialog.setLocation(350, 380)

dialog.setSize(80, 115)

dialog.setResizable(false)

Container dialogPane = dialog.getContentPane()

dialogPane.setLayout(new FlowLayout(FlowLayout.CENTER, 80, 15))

dialogPane.add(label)

dialogPane.add(queding)

queding.addActionListener(this)

dialog.show()

}

public void actionPerformed(ActionEvent e) {

dialog.show(false)

}

}

//qq菜单类

class caidan

extends MenuBar

implements ActionListener {

String nicheng1 = "我"//定义默认昵称

String nicheng2 = "对方"

private Menu m1, m2, m3, m4, m5,m6

private MenuItem m11, m12, m13, m22, m23, m31, m51, m52, m53,m54, m55,m61,m62,m63,m64,m65,m66,m67,m68,m69,mm

public caidan() {

m1 = new Menu("聊天")

m2 = new Menu("娱乐")

m3 = new Menu("设置")

m4 = new Menu("工具")

m5 = new Menu("游戏")

m6=new Menu("背景颜色")

m11 = new MenuItem("短信聊天")

m12 = new MenuItem("视频聊天")

m13 = new MenuItem("文件传输")

m22 = new MenuItem("开心一刻")

m23 = new MenuItem("美景欣赏")

m51 = new MenuItem("贪吃蛇")

m52 = new MenuItem("拼图")

m53 = new MenuItem("围棋对弈")

m54 = new MenuItem("华容道")

m55 = new MenuItem("俄罗斯方块")

m31 = new MenuItem("我的昵称")

m61=new MenuItem("样式1")

m62=new MenuItem("样式2")

m63=new MenuItem("样式3")

m64=new MenuItem("样式4")

m65=new MenuItem("样式5")

m66=new MenuItem("样式6")

m67=new MenuItem("样式7")

m68=new MenuItem("样式8")

m69=new MenuItem("样式9")

mm=new MenuItem("默认")

m1.add(m11)m11.addActionListener(this)

m1.add(m12)m12.addActionListener(this)

m1.add(m13)m13.addActionListener(this)

m2.add(m5)

m2.add(m22)m22.addActionListener(this)

m2.add(m23)m23.addActionListener(this)

m5.add(m51)m51.addActionListener(this)

m5.add(m52)m52.addActionListener(this)

m5.add(m53)m53.addActionListener(this)

m5.add(m54)m54.addActionListener(this)

m5.add(m55)m55.addActionListener(this)

m3.add(m31)m31.addActionListener(this)

m3.add(m6)

m6.add(m61)m61.addActionListener(this)

m6.add(m62)m62.addActionListener(this)

m6.add(m63)m63.addActionListener(this)

m6.add(m64)m64.addActionListener(this)

m6.add(m65)m65.addActionListener(this)

m6.add(m66)m66.addActionListener(this)

m6.add(m67)m67.addActionListener(this)

m6.add(m68)m68.addActionListener(this)

m6.add(m69)m69.addActionListener(this)

m6.add(mm)mm.addActionListener(this)

add(m1)

add(m2)

add(m3)

add(m4)

}

public void actionPerformed(ActionEvent e) {

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

}

else if (e.getSource() == m52) {

}

else if (e.getSource() == m53) {

}

else if (e.getSource() == m54) {}

else if (e.getSource() == m55) {

}

else if (e.getSource() == m22) {

}

else if (e.getSource() == m23) {

}

else if (e.getSource() == m31){

String nicheng = JOptionPane.showInputDialog("请输入你的昵称:")

nicheng1 = nicheng

nicheng2 = nicheng

}

else if(e.getSource() == m61){

Color ys=new Color(77,192,221)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == m62){

Color ys=new Color(45,168,118)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == m63){

Color ys=new Color(245,163,238)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == m64){

Color ys=new Color(148,157,55)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == m65){

Color ys=new Color(240,162,142)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == m66){

Color ys=new Color(148,190,252)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == m67){

Color ys=new Color(253,181,196)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == m68){

Color ys=new Color(182,162,230)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == m69){

Color ys=new Color(237,250,116)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

else if(e.getSource() == mm){

Color ys=new Color(157,242,173)

QQ.p1.setBackground(ys)

QQ.p2.setBackground(ys)

}

}

}

//字体对话框类

class ziti

implements ActionListener, ItemListener {

private JLabel lb1 = new JLabel("字体"), lb2 = new JLabel("字形"),

lb3 = new JLabel("大小")

JTextField text1 = new JTextField("楷体"), text2 = new JTextField("PLAIN"),

text3 = new JTextField("14", 2)

java.awt.List list1 = new java.awt.List(6, false),

list2 = new java.awt.List(6, false), list3 = new java.awt.List(6, false)

private JButton b_queding = new JButton("确定"), b_quxiao = new JButton("取消")

String zixing[] = {

"常规", "斜体", "粗体"}

String daxiao[] = {

"8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26",

"28", "34"}

private JDialog dialog

Font f

String aa = "楷体"

int cc = 14

//字体对话框类的构造方法

public ziti(JFrame f) {

dialog = new JDialog(f, "字体", true)

dialog.setLocation(150, 150)

dialog.setSize(400, 230)

dialog.setResizable(false)

Container dd = dialog.getContentPane()

dd.setLayout(null)

lb1.setBounds(4, 2, 30, 20)

dd.add(lb1)

lb2.setBounds(150, 2, 30, 20)

dd.add(lb2)

lb3.setBounds(250, 2, 30, 20)

dd.add(lb3)

text1.setBounds(4, 24, 140, 20)

dd.add(text1)

text2.setBounds(150, 24, 95, 20)

dd.add(text2)

text3.setBounds(250, 24, 50, 20)

dd.add(text3)

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment()

String fontName[] = ge.getAvailableFontFamilyNames()

for (int i = 0i <fontName.lengthi++) {

list1.add(fontName[i])

}

for (int j = 0j <zixing.lengthj++) {

list2.add(zixing[j])

}

for (int k = 0k <daxiao.lengthk++) {

list3.add(daxiao[k])

}

list1.setBounds(4, 46, 140, 130)

dd.add(list1)

list2.setBounds(150, 46, 95, 130)

dd.add(list2)

list3.setBounds(250, 46, 50, 130)

dd.add(list3)

b_queding.setBounds(315, 60, 60, 20)

dd.add(b_queding)

b_quxiao.setBounds(315, 100, 60, 20)

dd.add(b_quxiao)

b_queding.addActionListener(this)

b_quxiao.addActionListener(this)

list1.addItemListener(this)

list2.addItemListener(this)

list3.addItemListener(this)

dialog.show()

}

//响应按钮事件的方法

public void actionPerformed(ActionEvent e) {

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

aa = text1.getText()

cc = Integer.parseInt(text3.getText())

if (list2.getSelectedIndex() == 0) {

f = new Font(aa, Font.PLAIN, cc)

}

else if (list2.getSelectedIndex() == 1) {

f = new Font(aa, Font.ITALIC, cc)

}

else if (list2.getSelectedIndex() == 2) {

f = new Font(aa, Font.BOLD, cc)

}

else {

f = new Font(aa, Font.PLAIN, cc)

}

dialog.show(false)

}

else {

dialog.show(false)

}

}

//响应列表框事件的方法

public void itemStateChanged(ItemEvent e) {

String name1 = list1.getSelectedItem()

String name2 = list2.getSelectedItem()

String name3 = list3.getSelectedItem()

if (name1 != null) {

text1.setText(name1)

}

if (name2 != null) {

text2.setText(name2)

}

if (name3 != null) {

text3.setText(name3)

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12544205.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存