java小球碰撞窗体边缘来回反d的代码

java小球碰撞窗体边缘来回反d的代码,第1张

import java.awt.Color

import java.awt.Graphics

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

import java.util.Random

import javax.swing.JFrame

import javax.swing.JPanel

public class RunningBallDemo extends JFrame {

public static void main(String args[]) {

new RunningBallDemo()

}

public RunningBallDemo() {

Ball ballPanel = new Ball(5, 5)

getContentPane().add(ballPanel)

setBackground(Color.BLACK)

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0)

}

})

setSize(350, 350)

setVisible(true)

Thread thread1 = new Thread(ballPanel)

thread1.start()

}

}

class Ball extends JPanel implements Runnable {

int rgb = 0

Color color

int x, y

int dx = 5, dy = 5

Ball(int x, int y) {

this.x = x

this.y = y

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g)

setBackground(Color.BLACK)

g.setColor(color)

g.fillOval(x, y, 50, 50)

}

public void run() {

while (true) {

if (x <= 0) {

dx = 5

updateBallColor()

} else if ((x + 50) >= getWidth()) {

dx = -5

updateBallColor()

}

if (y <= 0) {

dy = 5

updateBallColor()

} else if ((y + 50) >= getHeight()) {

dy = -5

updateBallColor()

}

x = x + dx

y = y + dy

repaint()

try {

Thread.sleep(25)

} catch (InterruptedException e) {

}

}

}

public void updateBallColor() {

rgb = new Random().nextInt()

color = new Color(rgb)

}

}

/*

 * 一个在窗体中来回运动的圆.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=0i<8i++){

            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

                                 }

        }

     }

}

在Java编程中,可以使用Graphics类和Color类来实现小球自由落体反d变颜色的功能。可以使用Graphics类中的drawOval()方法来画出小球,并使用fillOval()方法来填充小球的颜色。可以使用Color类中的getRGB()方法来获取每次反d后的新颜色。另外,也可以使用Thread类来实现小球的自由落体反d。


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

原文地址: https://outofmemory.cn/yw/11359491.html

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

发表评论

登录后才能评论

评论列表(0条)

保存