编写JAVA程序。一个球在四边的框里运动,碰到边就反弹,设置开始和结束按钮。非常感谢

 我来答
信玄居士72a5251
推荐于2016-02-09 · TA获得超过4406个赞
知道大有可为答主
回答量:3257
采纳率:100%
帮助的人:1839万
展开全部
/*
 * 一个在窗体中来回运动的圆.java
 *
 * Created on 2005年10月5日, 下午1:02
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package javaapplication1;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.applet.*;
import java.awt.*;
import java.math.*;
/**
 *
 * @author Bachelorlrz
 */
public class 在窗体中来回运动的圆 extends java.applet.Applet implements java.lang.Runnable {
    int cx,cy,c1x,c1y;    //圆的坐标
    int cw,ch;            //圆的宽高
    int bx,by,bw,bh;      //背景的坐标和宽高
    int dx,dx1;           //圆的运动
    int r,rx,ry;          //圆的半径和位置 
    int r1,r1x,r1y;
    Thread u_thread;
    
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public void init() {
        rx=150;             ry=160;
        bw=500;             bh=400;
        r1x=bw/2-cw*2;        r1y=bh/2-ch;
        r=60;               r1=200;
        cx =rx;             cy =ry;
        c1x=r1x;            c1y=r1y;
        cw=30;              ch=30;
        bx=2;               by=2;
        dx=1;               dx1=2;         
        // TODO start asynchronous download of heavy resources
    }
    
    // TODO overwrite start(), stop() and destroy() methods

    public void update(java.awt.Graphics g) {
       
        super.paint(g);
        g.setColor(Color.red);
        g.drawRect(bx,by,bw,bh);
        g.setColor(Color.BLACK);
        g.fillRect(bx+2,by+2,bw-4,bh-4);
        g.drawString("在窗体中来回运动的圆", bw/2-60, bh/2);
          if (cx<rx-r  || cx>rx+r) {
            dx = -dx;  
         }
         if (c1x<r1x-r1  || c1x>r1x+r1) {
            dx1 = -dx1;  
         }
        
            cx =cx+dx;
            cy =(int)(dx*Math.sqrt(r*r-(cx-rx)*(cx-rx)))+ry;
            c1x =c1x+dx1;
            c1y =(int)(dx1/2*Math.sqrt(r1*r1-(c1x-r1x)*(c1x-r1x))/2);
       
       // g.drawArc(cx, cy, cw, ch, 0, 360);
        
        for(int i=0;i<8;i++){
            if (i%5 == 0){
                g.setColor(Color.black);                
            }else if ( i%5== 1) {
                g.setColor(Color.GREEN);
            }else if(i%5==2){
                g.setColor(Color.RED);
            }else if( i%5 ==3){
                g.setColor(Color.pink);
            }else {
                g.setColor(Color.orange);
            }                       
            g.drawLine(bx,by, cx+10,cy+i+10);
            g.drawLine(bx+bw,by+bh,cx+10,cy+i+10);
            g.drawLine(bx+bw,by, cx+10,cy+i+10);
            g.drawLine(bx,by+bh, cx+10,cy+i+10);
            
            g.drawLine(bx,by,-cx+bw-bx-cw+10,cy+i+10);
            g.drawLine(bx+bw,by+bh,-cx+bw-bx-cw+10,cy+i+10);
            g.drawLine(bx+bw,by,-cx+bw-bx-cw+10,cy+i+10);
            g.drawLine(bx,by+bh,-cx+bw-bx-cw+10,cy+i+10);
            
            g.drawLine(bx,by, c1x+10,c1y+r1y+i+10);
            g.drawLine(bx+bw,by+bh,c1x+10,c1y+r1y+i+10);
            g.drawLine(bx+bw,by, c1x+10,c1y+r1y+i+10);
            g.drawLine(bx,by+bh, c1x+10,c1y+r1y+i+10);
            
            g.drawLine(bx,by, r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
            g.drawLine(bx+bw,by+bh,r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
            g.drawLine(bx+bw,by, r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
            g.drawLine(bx,by+bh,r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
            
            g.drawArc(cx+i, cy+i, cw-i*2, ch-i*2, 0, 360);
            g.drawArc(-cx+bw-bx-cw+i, cy+i, cw-i*2, ch-i*2, 0, 360);
            g.drawArc(c1x+i, c1y+r1y+i, cw-i*2, ch-i*2, 0, 360);
            g.drawArc(r1x+r1+cw-c1x+i, -c1y+r1y+i, cw-i*2, ch-i*2, 0, 360);
        }
    }
               public void start(){      
        if (u_thread == null)
        {
            u_thread  = new Thread(this);
            u_thread.start();
        }
    }
    public void run() {
        while(true){                              
                               repaint(); 
                               try{
                                     u_thread.sleep(10);
                                   }
                         catch (InterruptedException e){
                           return;
                                 }
        }
     }
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式