java applet 赛马小程序

昨天写看java书的时候,觉得闷,就写了个小程序自我娱乐一下。技术含量不高,只是自我娱乐而已。下面的代码只要编译出class文件,再写一个HTML格式的文件,调用Arc2Demo2.class 就能看了。

/*该程序实现一个跑马比赛程序,各个选手的速度随机控制
 */
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import javax.swing.*;
import java.lang.*;
public class Arc2Demo2 extends Applet implements Runnable{
 boolean boo;
 Thread th;
 int x;
 int x1;
 int x2;
 int x3;
 int x4;
 int x5;
 int x6;
 int x7;
 public void init()
 {
  th=new Thread(this);
  boo=true;
  x=0;
  x1 = 0;
  x2 = 0;
  x3 = 0;
  x4 = 0;
  x5 = 0;
  x6 = 0;
  x7 = 0;
 }
 public void start()
 {
  th.start();
 }
 public void paint(Graphics g)
 {
  Graphics2D g2=(Graphics2D)g;                 
  //g2.setPaint(Color.blue);
  Graphics2D g3 = (Graphics2D)g;
  //g3.setPaint(Color.pink);
  Graphics2D g4 = (Graphics2D)g;
  Graphics2D g5 = (Graphics2D)g;
  Graphics2D g6 = (Graphics2D)g;
  Graphics2D g7 = (Graphics2D)g;
  Graphics2D g8 = (Graphics2D)g;
  Graphics2D g9 = (Graphics2D)g;
  if(x<600&&x1<600&&x2<600&&x3<600&&x4<600&&x5<600&&x6<600&&x7<600)
  {
   if(boo)
   {
    g2.setPaint(Color.blue);
    g2.fill(new Arc2D.Double(x,20,50,50,30,310,Arc2D.PIE));                   //绘制饼形圆弧
    g3.setPaint(Color.pink);
    g3.fill(new Arc2D.Double(x1, 90, 50, 50, 30, 310, Arc2D.PIE));
    g4.setPaint(Color.red);
    g4.fill(new Arc2D.Double(x2,160,50,50,30,310,Arc2D.PIE));
    g5.setPaint(Color.green);
    g5.fill(new Arc2D.Double(x3, 230, 50, 50, 30, 310, Arc2D.PIE));
    g6.setPaint(Color.gray);
    g6.fill(new Arc2D.Double(x4, 300, 50, 50, 30, 310, Arc2D.PIE));
    g7.setPaint(Color.yellow);
    g7.fill(new Arc2D.Double(x5, 370, 50, 50, 30, 310, Arc2D.PIE));
    g8.setPaint(Color.orange);
    g8.fill(new Arc2D.Double(x6, 440, 50, 50, 30, 310, Arc2D.PIE));
    g9.setPaint(Color.black);
    g9.fill(new Arc2D.Double(x7, 510, 50, 50, 30, 310, Arc2D.PIE));
    boo=false;
   }
   else
   {
    g2.setPaint(Color.blue);
    g2.fill(new Arc2D.Double(x,20,50,50,0,350,Arc2D.PIE));                    //绘制饼形圆弧,通过改变圆弧的角度来实现形状的变换
    g3.setPaint(Color.pink);
    g2.fill(new Arc2D.Double(x1, 90, 50, 50, 0, 350, Arc2D.PIE));
    g4.setPaint(Color.red);
    g4.fill(new Arc2D.Double(x2, 160, 50, 50, 0, 350, Arc2D.PIE));
    g5.setPaint(Color.green);
    g5.fill(new Arc2D.Double(x3, 230, 50, 50, 0, 350, Arc2D.PIE));
    g6.setPaint(Color.gray);
    g6.fill(new Arc2D.Double(x4, 300, 50, 50, 0, 350, Arc2D.PIE));
    g7.setPaint(Color.yellow);
    g7.fill(new Arc2D.Double(x5, 370, 50, 50, 0, 350, Arc2D.PIE));
    g8.setPaint(Color.orange);
    g8.fill(new Arc2D.Double(x6, 440, 50, 50, 0, 350, Arc2D.PIE));
    g9.setPaint(Color.black);
    g9.fill(new Arc2D.Double(x7, 510, 50, 50, 0, 350, Arc2D.PIE));
    boo=true;
   }
   x+=10*Math.random()+1;                             //各个圆弧的速度由随机数控制
   x1 += 10 * Math.random() + 1;
   x2 += 10 * Math.random() + 1;
   x3 += 10 * Math.random() + 1;
   x4 += 10 * Math.random() + 1;
   x5 += 10 * Math.random() + 1;
   x6 += 10 * Math.random() + 1;
   x7 += 10 * Math.random() + 1;
  }
  else
  {
   //wait();
   if (x >= 600)
    g2.drawString("No.1 win the match!",20,590);         //输出赢的选手号码,并重新开始比赛
   if (x1 >= 600)
    g3.drawString("No.2 win the match!", 20, 590);
   if (x2 >= 600)
    g4.drawString("No.3 win the match!", 20, 590);
   if (x3 >= 600)
    g5.drawString("No.4 win the match!", 20, 590);
   if (x4 >= 600)
    g6.drawString("No.5 win the match!", 20, 590);
   if (x5 >= 600)
    g7.drawString("No.6 win the match!", 20, 590);
   if (x6 >= 600)
    g8.drawString("No.7 win the match!", 20, 590);
   if (x7 >= 600)
    g9.drawString("No.8 win the match!", 20, 590);
   //stop();
   x=0;
   x1 = 0;
   x2 = 0;
   x3 = 0;
   x4 = 0;
   x5 = 0;
   x6 = 0;
   x7 = 0;
  }
 }
 public void stop()
 { }
 public void run()
 {
  while(true)
  {
   repaint();
   try
   {
    Thread.sleep(500);
   }
   catch(InterruptedException e){}
  }
 }
 public static void main(String args[])
 {
  JFrame fr=new JFrame("2D演示");
  fr.getContentPane().setBackground(Color.white);
  Arc2Demo2 arc2=new Arc2Demo2();
  arc2.init();
  arc2.start();
  fr.add(arc2);
  fr.setSize(350,120);
  fr.setVisible(true);
  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

Java Applet是一种小程序,它运行在支持Java的浏览器中,用于创建动态的网络应用程序。Applet可以嵌入HTML页面中,并在用户的浏览器里运行,从而提供丰富的用户交互体验。Java Applet的主要特点和使用方式如下: 1. **小程序的嵌入**: Applet可以直接嵌入HTML页面中,通过`<applet>`标签进行嵌入。例如: ```html <html> <body> <applet code="MyApplet.class" width="300" height="300"> </applet> </body> </html> ``` 这里`code`属性指定了Applet类的名称,`width`和`height`属性定义了Applet在页面中的大小。 2. **生命周期方法**: 一个Applet类需要继承`Applet`类,并且可以重写一些生命周期方法,比如`init()`, `start()`, `stop()`, 和`destroy()`。这些方法分别在Applet的不同阶段被调用: - `init()`: Applet被创建时调用一次。 - `start()`: 浏览器加载Applet页面时调用,或者当Applet从stop状态恢复时调用。 - `stop()`: 当Applet不再可见或者浏览器切换到其他页面时调用。 - `destroy()`: 浏览器关闭或者Applet被显式销毁时调用。 3. **交互**: Applet可以通过`getParameter()`方法获取HTML页面传递的参数,并且可以使用`getDocumentBase()`和`getCodeBase()`等方法获取Applet的部署位置信息。Applet还可以创建图形用户界面(GUI)供用户交互。 4. **安全性限制**: 由于安全问题,现代浏览器已经不再支持AppletJava Applet在历史上曾经非常流行,但由于安全风险和缺乏更新,它已经被淘汰。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值