java窗口多球运动代码 java窗口多球运动代码怎么写
用JAVA实现多线程编写,使得许多小球在界面内循环跳动
下面这段代码应该符合你的需求
创新互联公司专注于徽州网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供徽州营销型网站建设,徽州网站制作、徽州网页设计、徽州网站官网定制、重庆小程序开发服务,打造徽州网络公司原创品牌,更为您提供徽州网站排名全网营销落地服务。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BounceThread
{ public static void main(String[] args)
{ JFrame frame = new BounceThreadFrame();
frame.show();
}
}
class BounceThreadFrame extends JFrame
{ public BounceThreadFrame()
{ setSize(300, 200);
setTitle("Bounce");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Start",
new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ Ball b = new Ball(canvas);
b.start();
}
});
addButton(p, "Close",
new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}
public void addButton(Container c, String title,
ActionListener a)
{ JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
private JPanel canvas;
}
class Ball extends Thread
{ public Ball(JPanel b) { box = b; }
public void draw()
{ Graphics g = box.getGraphics();
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void move()
{ if (!box.isVisible()) return;
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.getSize();
if (x 0)
{ x = 0; dx = -dx; }
if (x + XSIZE = d.width)
{ x = d.width - XSIZE; dx = -dx; }
if (y 0)
{ y = 0; dy = -dy; }
if (y + YSIZE = d.height)
{ y = d.height - YSIZE; dy = -dy; }
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void run()
{ try
{ draw();
for (int i = 1; i = 1000; i++)
{ move();
sleep(5);
}
}
catch(InterruptedException e) {}
}
private JPanel box;
private static final int XSIZE = 10;
private static final int YSIZE = 10;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
}
JAVA做一个作业,制作一个window框里面有三个小球碰撞,碰到边框或者小球变色加反弹,错误代码求指点
没轮差配办法了 只能注庆简册腊指一个马甲了
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class yes {
public static void main(String args[])
{
Mywindow win=new Mywindow();
Ball qiu1=new Ball(5,5);
Thread thread1=new Thread(qiu1);
Ball qiu2=new Ball(10,5);
Thread thread2=new Thread(qiu2);
Ball qiu3=new Ball(15,5);
Thread thread3=new Thread(qiu3);
thread1.start();
thread2.start();
thread3.start();
}
}
class Mywindow extends Frame {
Mywindow (){
setSize(350,350);
setVisible(true);
setBackground(Color.BLACK);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
class Ball extends Mywindow 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;
}
public void doColor(){
rgb=(int)(Math.random()*0xFFFFFF);
color=new Color(rgb);
}
public void paint(Graphics g){
g.setColor(color);
g.fillOval(x,y,50,50);
}
public void run(){
while(true){
if(x=0) {dx=5;doColor();}
else if((x+50)=getWidth()) {dx=-5; doColor();}
if(y=0) {dy=5;doColor();}
else if((y+50)=getHeight()) {dy=-5; doColor();}
x=x+dx;
y=y+dy;
repaint();
try{Thread.sleep(50);}
catch(InterruptedException e) {;}
}
}
}
java小球碰撞窗体边缘来回反弹的代码
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实现,在屏幕内一个小球随机移动
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;import com.sun.midp.lcdui.DisplayDeviceAccess;
public class Main extends MIDlet {
public static Display display;
public static GameCanvas Game;
public static MIDlet mid;
public Main() {
Game=new GameCanvas();
display=Display.getDisplay(this);
mid=this;
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {display.setCurrent(Game);}
}
Canvas类=========================import java.util.Random;import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class GameCanvas extends Canvas implements Runnable{
private int BallX=0;
private int BallY=0;
private int BallW=20;
private int BallH=20;
private boolean BallRun=true;
private byte BallDir=0;
private final byte Left=1;
private final byte Right=2;
private final int ScreenWith=150;
private final int ScreenHight=200;
private final int Speed = 5;
Random rand;
public GameCanvas(){
rand=new Random();
int state=rand.nextInt()%2;
BallDir=(byte)(state==0?Left:Right);
BallX=rand.nextInt()%240;
BallY=rand.nextInt()%320;
new Thread(this).start();
this.setFullScreenMode(true);
}
protected void paint(Graphics g) {
g.setColor(0);
g.fillRect(0, 0,this.getWidth(), this.getHeight());
g.setColor(255, 255,255);
g.fillArc(BallX, BallY,BallW, BallH, 0,360);
} public void BallRun(){
switch(BallDir){
case Left:
if(BallX0){BallX-=Speed;}
else{BallDir=Right;};break;
case Right:
if(BallXScreenWith){BallX+=Speed;}
else{BallDir=Left;}break;
}
if(BallY=0BallRun){
BallY-=Speed;
}else{BallRun=false;}
if(BallY=ScreenHight!BallRun){
BallY+=Speed;
}else{BallRun=true;}
}
public void run() {
while(true){
BallRun();
this.repaint();
try{Thread.sleep(100);br }catch(Exception ex){ex.printStackTrace();}
} }}
名称栏目:java窗口多球运动代码 java窗口多球运动代码怎么写
文章位置:http://ybzwz.com/article/ddppcsj.html