「java小球运动」java弹跳小球

博主:adminadmin 2022-12-26 11:18:07 58

本篇文章给大家谈谈java小球运动,以及java弹跳小球对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

JAVA用repaint方法在窗格内实现小球的来回碰撞,怎么在窗格边缘改变小球的运动方向呢?

public class DrawBall extends JFrame {

int x, y, width, height;

Color c;

int incX = 10;//X方向增量

int incY = 10;//Y方向增量

public DrawBall() {

super("寂寞高手不寂寞");

setSize(800, 600);

setVisible(true);

x = 0;

y = 0;

width = height = 50;

c = new Color(255, 0, 0);

}

public static void main(String[] args) {

DrawBall a = new DrawBall();

}

public void paint(Graphics g) {

Container pane = getContentPane();

Graphics pg = pane.getGraphics();

pg.setColor(Color.WHITE);

pg.fillRect(0, 0, pane.getWidth(), pane.getHeight());

//从这里开始改变小球的运动方向

if (x+width pane.getWidth() || x 0) {//X边界判断

incX *= -1; //增量方向反转

}

if (y+height pane.getHeight() || y 0) {//Y边界判断

incY *= -1;//增量方向反转

}

x = x + incX;

y = y + incY;

pg.setColor(c);

pg.fillOval(x, y, width, height);

try {

Thread.sleep(100);

} catch (InterruptedException E) {

};

repaint();

}

}

java实现一个小球的自由落体运动

你说的是模拟直线运动还是轨迹是抛物线的那种?

如何去模拟说白了就是要根据某种算法计算出物体运动的下一个坐标,做自由落体运动满足能量守恒定理,要把它实现出来关键是确定一个能量衰减(转化为其他能量)系数,就是物体以多少速度撞击然后以多少速度反弹,以多少角度撞击和以多少角度反弹,这个类似于镜面反射。

确定了以上这些,实现起来就不难了。

JAVA 编辑 小球在一个框里做连续的碰撞运动

/*

 * 一个在窗体中来回运动的圆.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 a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3uhPWrjRdmHTdmWR4nWn40ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHRYnHmYnHRYPW6knH63P1nz" target="_blank" class="baidu-highlight"java.awt.Color/a;

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 (cxrx-r  || cxrx+r) {

            dx = -dx;  

         }

         if (c1xr1x-r1  || c1xr1x+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;i8;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;

                                 }

        }

     }

}

JAVA 模拟小球自由落体和平抛运动

这个绝对可以的,我都试过了。请采纳,谢谢。

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class BallThread extends Applet implements Runnable{

Thread red, blue;

Graphics redPen, bluePen;

int t=0;

public void init(){

red = new Thread(this);

blue = new Thread(this);

redPen = getGraphics();

bluePen = getGraphics();

redPen.setColor(Color.red);

bluePen.setColor(Color.blue);

}

public void start(){

red.start();

blue.start();

}

public void run(){

while(true){

t=t+1;

if(Thread.currentThread()==red){

if(t100)t=0;

redPen.clearRect(0,0,110,400);

redPen.fillOval(50,(int)(1.0/2*t*9.8),15,15);

try{

red.sleep(40);

}catch(InterruptedException e){}

}else if(Thread.currentThread()==blue){

bluePen.clearRect(120,0,900,500);

bluePen.fillOval(120+7*t,(int)(1.0/2*t*9.8),15,15);

try{

blue.sleep(40);

}catch(InterruptedException e){}

}

}

}

}

java小球运行原理

高级语言运行过程。java小球运行原理是高级语言运行过程在程序真正运行在CPU上之前,必须要让OS的kernel理解在编辑器或者IDE里根据每种语言的语法规则敲入的源代码,kernel才能做出相关的调度,所以需要先将源代码转化成可执行的二进制文件,这个过程通常由编译器完成。

关于java小球运动和java弹跳小球的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-26,除非注明,否则均为首码项目网原创文章,转载请注明出处。