JAVA 编辑 小球在一个框里做连续的碰撞运动 20
如题,并且是随机发的球,是说建一个框在框的边线上随机发出一个球,球做直线运动,当遇的边线时,成角度的反弹,遇到另一条线再成角度的反弹,如此循环下去思路我知道,就是不知道怎...
如题,并且是随机发的球,
是说建一个框 在框的边线上随机发出一个球,球做直线运动,当遇的边线时,成角度的反弹,遇到另一条线 再成角度的反弹,如此循环下去
思路我知道,就是不知道怎么写啊, 我才学了半个月啊
麻烦给写出来啊 展开
是说建一个框 在框的边线上随机发出一个球,球做直线运动,当遇的边线时,成角度的反弹,遇到另一条线 再成角度的反弹,如此循环下去
思路我知道,就是不知道怎么写啊, 我才学了半个月啊
麻烦给写出来啊 展开
展开全部
/*
* 一个在窗体中来回运动的圆.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="https://www.baidu.com/s?wd=java.awt.Color&tn=44039180_cpr&fenlei=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 (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;
}
}
}
}
展开全部
建个项目
建个叫test的文件
建一个叫test的class文件,将代码复制进去就好
package test;
import java.awt.*;
import java.applet.*;
public class test extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
int X, Y, moveX, moveY, width, height;
Thread newThread;
Image OffScreen;
Graphics drawOffscreen;
public void init() {
X = 0;
Y = 0;
moveX = 10; //水平移动的速度
moveY = 15; //垂直移动速度
width = getSize().width ; //窗口的宽度
System.out.println("width = " + width);
height = getSize().height ;
System.out.println("height = " + height);
OffScreen = createImage(width, height); //创建背景
drawOffscreen = OffScreen.getGraphics();
}
public void start()
{
newThread = new Thread(this);
newThread.start();
}
public void stop()
{
newThread = null;
}
public void paint(Graphics g)
{
drawOffscreen.setColor(Color.black); // 设置背景色
drawOffscreen.fillRect(0, 0, width, height); // 填充所在区域
drawOffscreen.setColor(Color.white); // 设置球的颜色
drawOffscreen.fillOval(X, Y, 15, 15); //画球
g.drawImage(OffScreen, 0, 0, this); // 画背景
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
while (newThread != null)
{
repaint();
try
{
Thread.sleep(50);
}
catch (InterruptedException E) {
}
X = X + moveX;
Y = Y + moveY;
if (X >= (width - 15))
{
X = width - 15;
moveX = -moveX;
}
if (X <= 0)
{
X = 0;
moveX = -moveX;
}
if (Y >= (height - 15))
{
Y = height - 15;
moveY = -moveY;
}
if (Y <= 0)
{
Y = 0;
moveY = -moveY;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用Jbox2d,建站world边界位置,设置弹力为1(永远不会损耗)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没时间了说说思路吧
添加文本框
TextField tf=new TextField(12);
画一个球 作为进度条
设定位置 随机数
控制,位置
添加文本框
TextField tf=new TextField(12);
画一个球 作为进度条
设定位置 随机数
控制,位置
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
三年前写的,那时候只能把它做出来.对付着看下吧.
import java.awt.event.*;
import java.awt.*;
public class QicaiXiaoqui extends Thread{
private Frame f;
private Label l1,l2,l3,l4;
private Button b1,b2,b3,b4;
private Panel p1,p2;
private ThreadGroup th;
private int i=0;
public QicaiXiaoqui(){
th=new ThreadGroup("one");
f=new Frame();
l1=new Label();
l2=new Label();
l3=new Label();
l4=new Label("");
b1=new Button("start");
b2=new Button("pause");
b3=new Button("continue");
b4=new Button("stop");
p1=new Panel();
p2=new Panel();
f.setLayout(null);
p1.setLayout(null);
p2.setLayout(new GridLayout());
l1.setBounds(0,0,600,55);
l2.setBounds(0,0,30,470);
l3.setBounds(570,0,30,470);
l4.setBounds(0,445,600,30);
p2.setBounds(0,473,600,30);
p1.setBounds(30,55,545,390);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(l4);
f.add(p2);
f.add(p1);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
l1.setBackground(new Color(239,143,159));
l2.setBackground(new Color(239,143,159));
l3.setBackground(new Color(239,143,159));
l4.setBackground(new Color(239,143,159));
b2.setBackground( new Color(209,000,251));
b1.setBackground(new Color(255,169,002));
b3.setBackground(new Color(255,100,255));
b4.setBackground(new Color(29,116,046));
p1.setBackground(Color.green);
b2.setEnabled(false);
b3.setEnabled(false);
f.setResizable(false);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
th.stop();
f.setVisible(false);
f.dispose();
}
});
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Thread x=new Thread(th,QicaiXiaoqui.this);
x.start();
i++;
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b1.setEnabled(false);
b3.setEnabled(true);
th.suspend();
b2.setEnabled(false);
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b1.setEnabled(true);
b2.setEnabled(true);
th.resume();
b3.setEnabled(false);
}
});
b4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
th.stop();
f.setVisible(false);
f.dispose();
System.out.println("stop");
}
});
f.setSize(600,500);
f.setVisible(true);
}
public void run(){
b2.setEnabled(true);
l2.setText(" "+i);
l3.setText(" "+i);
Button bb=new Button();
bb.setBounds(250,380,10,10);
p1.add(bb);
//ムユノォ
int r=(int)(Math.random()*254+1);
int g=(int)(Math.random()*254+1);
int b=(int)(Math.random()*254+1);
bb.setBackground(new Color(r,g,b));
int rx=(int)(Math.random()*-10)+5;
int ry=(int)(Math.random()*10);
while(true){
int x=bb.getX();
int y=bb.getY();
if(x<=0){
rx=-rx;
}
else if(x>=520){
rx=-rx;
}
else if(y<=0){
ry=-ry;
}
else if(y>=385){
ry=-ry;
}
else if(rx==0){
rx=-3;
}
else if(ry==0){
ry=3;
}
try{
Thread.sleep(2);
}
catch(InterruptedException e){
e.printStackTrace();
}
bb.setLocation(x-=rx,y-=ry);
}
}
public static void main(String[] args){
QicaiXiaoqui c=new QicaiXiaoqui();
}
};
import java.awt.event.*;
import java.awt.*;
public class QicaiXiaoqui extends Thread{
private Frame f;
private Label l1,l2,l3,l4;
private Button b1,b2,b3,b4;
private Panel p1,p2;
private ThreadGroup th;
private int i=0;
public QicaiXiaoqui(){
th=new ThreadGroup("one");
f=new Frame();
l1=new Label();
l2=new Label();
l3=new Label();
l4=new Label("");
b1=new Button("start");
b2=new Button("pause");
b3=new Button("continue");
b4=new Button("stop");
p1=new Panel();
p2=new Panel();
f.setLayout(null);
p1.setLayout(null);
p2.setLayout(new GridLayout());
l1.setBounds(0,0,600,55);
l2.setBounds(0,0,30,470);
l3.setBounds(570,0,30,470);
l4.setBounds(0,445,600,30);
p2.setBounds(0,473,600,30);
p1.setBounds(30,55,545,390);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(l4);
f.add(p2);
f.add(p1);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
l1.setBackground(new Color(239,143,159));
l2.setBackground(new Color(239,143,159));
l3.setBackground(new Color(239,143,159));
l4.setBackground(new Color(239,143,159));
b2.setBackground( new Color(209,000,251));
b1.setBackground(new Color(255,169,002));
b3.setBackground(new Color(255,100,255));
b4.setBackground(new Color(29,116,046));
p1.setBackground(Color.green);
b2.setEnabled(false);
b3.setEnabled(false);
f.setResizable(false);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
th.stop();
f.setVisible(false);
f.dispose();
}
});
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Thread x=new Thread(th,QicaiXiaoqui.this);
x.start();
i++;
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b1.setEnabled(false);
b3.setEnabled(true);
th.suspend();
b2.setEnabled(false);
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b1.setEnabled(true);
b2.setEnabled(true);
th.resume();
b3.setEnabled(false);
}
});
b4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
th.stop();
f.setVisible(false);
f.dispose();
System.out.println("stop");
}
});
f.setSize(600,500);
f.setVisible(true);
}
public void run(){
b2.setEnabled(true);
l2.setText(" "+i);
l3.setText(" "+i);
Button bb=new Button();
bb.setBounds(250,380,10,10);
p1.add(bb);
//ムユノォ
int r=(int)(Math.random()*254+1);
int g=(int)(Math.random()*254+1);
int b=(int)(Math.random()*254+1);
bb.setBackground(new Color(r,g,b));
int rx=(int)(Math.random()*-10)+5;
int ry=(int)(Math.random()*10);
while(true){
int x=bb.getX();
int y=bb.getY();
if(x<=0){
rx=-rx;
}
else if(x>=520){
rx=-rx;
}
else if(y<=0){
ry=-ry;
}
else if(y>=385){
ry=-ry;
}
else if(rx==0){
rx=-3;
}
else if(ry==0){
ry=3;
}
try{
Thread.sleep(2);
}
catch(InterruptedException e){
e.printStackTrace();
}
bb.setLocation(x-=rx,y-=ry);
}
}
public static void main(String[] args){
QicaiXiaoqui c=new QicaiXiaoqui();
}
};
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询