JAVA 编辑 小球在一个框里做连续的碰撞运动
答案:7 悬赏:60 手机版
解决时间 2021-04-06 12:54
- 提问者网友:蓝莓格格巫
- 2021-04-05 18:26
JAVA 编辑 小球在一个框里做连续的碰撞运动
最佳答案
- 五星知识达人网友:迟山
- 2021-04-05 19:27
建个项目
建个叫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;
}
}
}
}
建个叫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;
}
}
}
}
全部回答
- 1楼网友:鱼忧
- 2021-04-06 01:21
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.*;
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;
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
dx = -dx;
}
if (c1x
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;
}
}
}
}
- 2楼网友:迟山
- 2021-04-06 01:04
哦 这个是什么
- 3楼网友:污到你湿
- 2021-04-05 23:39
用Jbox2d,建站world边界位置,设置弹力为1(永远不会损耗)
- 4楼网友:末日狂欢
- 2021-04-05 22:29
晕 这种题很无聊 ! JAVA小应用程序基本不用了!
- 5楼网友:舊物识亽
- 2021-04-05 21:09
没时间了说说思路吧
添加文本框
TextField tf=new TextField(12);
画一个球 作为进度条
设定位置 随机数
控制,位置
添加文本框
TextField tf=new TextField(12);
画一个球 作为进度条
设定位置 随机数
控制,位置
- 6楼网友:人间朝暮
- 2021-04-05 20:04
三年前写的,那时候只能把它做出来.对付着看下吧.
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();
}
};
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯