。。。
于是我拼命的写了快三天60个小时的Java Applet程序,边学边用。Java 真是好学易懂,虽
然从来没有接触过,但还是学了就能用,有限的时间总算勉强搞定了。
有点笨,那会儿不知道Java代码用什么来写,于是用文本编辑器几百行下来,其实已经调试得很累。调试完了倒是有点欣慰。
//
--------------------------------------BallsGame0.9-----------------------------------------------
//
-==这是一个抛球的小游戏项目,为运行于浏览器的Java Applet程序==-
//
-==调试环境:Windows XP,SDK版本:j2sdk-1_4_2_17 浏览器:Mozilla Firefox
//
-==Socket问题:即Sever端为VC++写的,通过Socket进行通讯时存在问题,因为二者传递的数据格式存不一样
//
--最新修改时间2008.04.02
//
**************************************************************************************************
import
java.io.
*
;
import
java.net.
*
;
import
java.awt.
*
;
import
java.awt.event.
*
;
import
java.applet.
*
;
//
-----------------------------------------------------------------------
public
class
BallsGame_Browser
extends
Applet
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private ButtonControls Buttons;
private ControlsMassWind Labels;
private ResultLabel results;
public void init() //Java Applet 初始化函数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//setLayout(null);
setLayout(new BorderLayout());
add("North", Buttons=new ButtonControls()); //按钮
add("Center", Labels = new ControlsMassWind()); //标签
add("South", results=new ResultLabel());
}
public void destroy()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
public void start()//Java Applet 启动函数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Buttons.setEnabled(true);
Labels.setEnabled(true);
results.setEnabled(true);
}
public void stop()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Labels.setEnabled(false);
results.setEnabled(false);
Buttons.setEnabled(false);
}
public void processEvent(AWTEvent e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (e.getID() == Event.WINDOW_DESTROY)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.exit(0);
}
}
public static void main(String args[]) //Main()函数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
BallsGame_Browser ball = new BallsGame_Browser();
ball.init();
ball.start();
}
}
//
class BallsGame_Browser
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
-----------------Socket通讯类--------
class
TSocket
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private Socket tSock;
private final int sockPort=5555; //端口号
//DataOutputStream os; DataInputStream is;
private String Line;
public String SendSock(String strData)//方法打包 :-发送- 接收
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//ConnectSock();
SendString(strData);//发送数据
Line =RecvString();//接收数据
//try{ tSock.close();} catch(IOException e) { System.out.println("RecvString error:"+e); }
return Line;
}
public String ConnectSock()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
final int timeOut=3000;//定义超时时间
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tSock=new Socket();
tSock.connect(new InetSocketAddress("127.0.0.1",sockPort),timeOut);
//tSock=new Socket("127.0.0.1",sockPort);//与服务器建立连接
return "";
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
catch(SocketTimeoutException e) ...{ return "SocketError!"; }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
catch(IOException e) ...{ return "SocketError!"; }
}
public void SendString(String stri) //发送字符串的方法
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int i=Integer.parseInt(stri);
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DataOutputStream os=new DataOutputStream(tSock.getOutputStream());
//DataInputStream is=new DataInputStream(tSock.getInputStream());
//os.writeChars(stri); //向服务器输送string
//os.writeUTF(stri);
os.writeInt(i);
os.flush();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
catch(IOException e) ...{System.out.println("SendString error:"+e); }
}
public String RecvString() //接收字符串的方法
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//DataOutputStream os=new DataOutputStream(m_JSock.getOutputStream());
DataInputStream is=new DataInputStream(tSock.getInputStream());
//Line = is.readUTF();
Line=String.valueOf(is.readInt()); //从服务器读来的信息
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
catch(IOException e) ...{ System.out.println("RecvString error:"+e); }
return Line;
}
}
//
end class TSocket
//
---------------MControls----------------------
class
ButtonControls
extends
Panel
implements
ActionListener,Runnable
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private volatile Thread timerThread; //添加线程对象
private TSocket tsock;//添加Socket类对象,用于与服务器端通讯
public static String m_leftTime="60";
private String m_force="60", m_angle="30", m_mass="5", m_windSpeed="0", m_nextBallMass="5",
m_nextWindSpeed="1", m_score="0", m_totalScore="0", m_distance="0";
private TextField textForce, textAngle; Label labelForce,labelAngle;
private Button BeginButton,EndButton,OKButton ;
public ButtonControls()//构造函数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
BeginButton = new Button("开始"); BeginButton.addActionListener(this); add(BeginButton); BeginButton.setEnabled(true);
EndButton = new Button("结束"); EndButton.addActionListener(this); add(EndButton); EndButton.setEnabled(false);
add(labelForce=new Label("力量:",Label.RIGHT));
add(textForce = new TextField("", 5));
add(labelAngle=new Label("角度:",Label.RIGHT));
add(textAngle = new TextField("", 5));
OKButton= new Button("确定"); OKButton.addActionListener(this); add(OKButton); OKButton.setEnabled(false);
}
public void RandWindMass()//下一回合的产生随机数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// String.valueOf(num): int to Str //str to int: Integer.parseInt(String s)
int i;
i=(int)( 11 * Math.random())-5;
m_nextWindSpeed=String.valueOf(i); //风速-5到5
i=(int)( 10 * Math.random())+1;
m_nextBallMass=String.valueOf(i); //质量大小1-10
}
public void ScoreCount() //计算球抛出距离和得分的函数
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//边界检查:
if((Double.parseDouble(m_force))>150 || (Double.parseDouble(m_force))<0) m_force="150";
if((Double.parseDouble(m_angle))>90 || (Double.parseDouble(m_angle))<0) m_angle="90";
final double PI=3.1515926;//定义常量PI
//计算路程
//路程 S= [ fcosa /m + v " ] * fsina/5m 得分10 - abs(5 - s) * 10
m_distance=String.valueOf( (double)(((Double.parseDouble(m_force))* Math.cos((Double.parseDouble(m_angle))*PI/180)/(Double.parseDouble(m_mass))
+ (Double.parseDouble(m_windSpeed)))* (Double.parseDouble(m_force))* Math.sin((Double.parseDouble(m_angle))*PI/180)/(5*(Double.parseDouble(m_mass)))) );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//计算本次得分和总得分 10 - abs(5 - s) * 10
if(Double.parseDouble(m_distance)>=4 && Double.parseDouble(m_distance)<=6)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
m_score=String.valueOf((int)(10-Math.abs((5-Double.parseDouble(m_distance))*10) ));
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{ m_score=String.valueOf(0); }
m_totalScore = String.valueOf( Integer.parseInt(m_totalScore) + Integer.parseInt(m_score));
}
//-=====================消息响应函数=====================-//重要
public void actionPerformed(ActionEvent ev)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
String label = ev.getActionCommand(); //String str;
if(label.equals("确定"))//如果是 "确定"按钮的事件
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(Integer.parseInt(m_leftTime) <= 0)//如果剩余时间为0
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
BeginButton.setEnabled(true);
EndButton.setEnabled(false);
OKButton.setEnabled(false);
}
m_force=textForce.getText().trim();//取得玩家的输入,力量和角度
m_angle=textAngle.getText().trim();
ScoreCount(); //计算所得结果
//进入下一个回合,数据更新
//RandWindMass();
m_windSpeed=m_nextWindSpeed;
m_mass=m_nextBallMass;
RandWindMass();//产生下一次的随机变量给玩家
//刷新标签:
ControlsMassWind.UpdateLabel(m_windSpeed,m_mass,m_nextWindSpeed,m_nextBallMass);
ResultLabel.UpdateLabel(m_distance,m_leftTime,m_score,m_totalScore);
textForce.setText(m_force);
textAngle.setText(m_angle);
}
else if(label.equals("开始")) //"开始"按钮
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
m_force="40"; m_angle="30"; m_totalScore="0"; m_score="0";
RandWindMass();//产生一个回合的随机数
m_windSpeed=m_nextWindSpeed;
m_mass=m_nextBallMass;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
RandWindMass();//产生下一个回合的随机数
BeginButton.setEnabled(false);
EndButton.setEnabled(true);
OKButton.setEnabled(true);
//传递和更新数据和标签
ControlsMassWind.UpdateLabel(m_windSpeed,m_mass,m_nextWindSpeed,m_nextBallMass);
ResultLabel.UpdateLabel("0",m_leftTime,"0","0");
textForce.setText(m_force);
textAngle.setText(m_angle);
timerThread = new Thread(this);//添加线程对象
timerThread.start(); //启动线程;
}
else if(label.equals("结束"))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
BeginButton.setEnabled(true);
EndButton.setEnabled(false);
OKButton.setEnabled(false);
m_leftTime="0";
ResultLabel.leftTime.setText(ButtonControls.m_leftTime);
timerThread = null; //结束分线程
}
}
//-----------------------------------------------------------------------------
public void run() //-------多线程执行函数---
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
m_leftTime="60";//初始化时间变量
int intTime=Integer.parseInt(ButtonControls.m_leftTime);
tsock=new TSocket();
//Thread me = Thread.currentThread();
String strTime="60";
if(tsock.ConnectSock()!="SocketError")//Socket连接服务器端
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
strTime=tsock.SendSock(strTime);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ResultLabel.leftTime.setText("SocketError");
BeginButton.setEnabled(true);
EndButton.setEnabled(false);
OKButton.setEnabled(false);
}
while (timerThread == Thread.currentThread()) //执行分线程
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(intTime <= 0)// 如果时间已经为0
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
BeginButton.setEnabled(true);
EndButton.setEnabled(false);
OKButton.setEnabled(false);
ResultLabel.leftTime.setText("时间到!");
break;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
timerThread.sleep(1000);
--intTime;
ButtonControls.m_leftTime=String.valueOf(intTime);
ResultLabel.leftTime.setText(ButtonControls.m_leftTime);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
catch (InterruptedException e) ...{ ; }
}
}//end while
}//end run
}
//
end class ButtonControls
class
ControlsMassWind
extends
Panel
//
风速和质量 标签类
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public static Label nowWind,nowMass,nextWind,nextMass,term;
public ControlsMassWind()//标签布局和显示
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
add(term=new Label(" "));
add(term=new Label(" "));
//setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
add(term=new Label(" 当前回合: [球大小]:",Label.RIGHT)); //term.setAlignment(Label.RIGHT);
add(nowMass=new Label("")); nowMass.setText(" ");
add(term=new Label("[风速]:",Label.RIGHT)); //term.setAlignment(Label.RIGHT);
add(nowWind=new Label("")); nowWind.setText(" ");
add(term=new Label(" 下一回合: [球大小]:",Label.RIGHT)); //term.setAlignment(Label.RIGHT);
add(nextMass=new Label("")); nextMass.setText(" ");
add(term=new Label("[风速]:",Label.RIGHT)); //term.setAlignment(Label.RIGHT);
add(nextWind=new Label("")); nextWind.setText(" ");
}
public static void UpdateLabel(String str_nowWind,String str_nowMass,String str_nextWind,String str_nextMass)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
nowWind.setText(str_nowWind);
nowMass.setText(str_nowMass);
nextWind.setText(str_nextWind);
nextMass.setText(str_nextMass);
}
}
//
end class ControlsMassWind
class
ResultLabel
extends
Panel
//
结果 标签类
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public static Label distance,leftTime,score,totalScore,term;
public ResultLabel()//标签布局和显示
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
add(term=new Label("[射程]:",Label.RIGHT));
add(distance=new Label(" "));
add(term=new Label("[时间]:",Label.RIGHT));
add(leftTime=new Label(" "));
add(term=new Label("[得分]:",Label.RIGHT));
add(score=new Label(" "));
add(term=new Label("[总分]:",Label.RIGHT));
add(totalScore=new Label(" "));
}
public static void UpdateLabel(String str_distance,String str_leftTime,String str_score,String str_totalScore)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
distance.setText(str_distance);
leftTime.setText(str_leftTime);
score.setText(str_score);
totalScore.setText(str_totalScore);
}
}
//
end class ResultLabel
从从未接触过到快速的学习和开发应用, 是因为我在这方面凑合着算个人才,还是有一天我也将因此而失业?
难道,我也选择了一个靠青春吃饭的行业? 我总也感到危机。只是我依然相信,毕业了 一身良好的技术背景不会是错误的选择--没文化也就算了,没钱也无所谓,只是男人总得有个混的资本,好好混也就是了。 安身立命,纵饿死亦不觉枉然。