昨天写看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); }}