java线程第二节 多线程小球碰撞(粗糙)
UI
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
public class UI extends JFrame{
private int x=60,y=60,size=50;
public static void main(String[] args) {
new UI().initUI();
}
public void initUI() {
this.setSize(800,600);
this.setTitle("线程游戏");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
FlowLayout fly=new FlowLayout();
this.setLayout(fly);
JButton jbu=new JButton("开始");
this.add(jbu);
this.setVisible(true);
ButtonListener Listener=new ButtonListener(this.getGraphics());
jbu.addActionListener(Listener);
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
}
ButtonListener
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonListener implements ActionListener{
private Graphics g;
private int x=60,y=60,size=50;
public ButtonListener(Graphics g) {
this.g=g;
}
@Override
public void actionPerformed(ActionEvent e) {
AddThread ad=new AddThread(g);
ad.start();
}
}
AddThread
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class AddThread extends Thread{
UI ui=new UI();
private Graphics g;
private int x,y=50,sizex=50,sizey=50;
private int oldx,oldy,flag=0;
private int a,b;
Random ran = new Random();
public AddThread(Graphics g) {
super();
this.g = g;
x=(int)(Math.random()*800);
System.out.println("x="+x);
a=ran.nextInt(2);
if(a==0) {
sizex=sizex*-1;
}else {
sizex=sizex*1;
}
b=ran.nextInt(2);
x=50*ran.nextInt(16);
}
@Override
public void run()
{
for(int i=0;i<500;i++)
{
try {
Thread.sleep(80);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(y<=600&&flag==1)
{
g.setColor(ui.getBackground());
g.fillOval(oldx, oldy, 50, 50);
g.setColor(Color.BLACK);
g.fillOval(x+=sizex, y-=50, 50, 50);
oldx=x;
oldy=y;
if(x==800)
{
flag=2;
sizey=-1*sizey;
}else if(x==0) {
flag=3;
sizey=-1*sizey;
}
}
else if(y>=0&&flag==4)
{
g.setColor(ui.getBackground());
g.fillOval(oldx, oldy, 50, 50);
g.setColor(Color.BLACK);
g.fillOval(x+=sizex, y+=50, 50, 50);
if(x==800)
{
flag=2;
sizey=-1*sizey;
}else if(x==0) {
flag=3;
sizey=-1*sizey;
}
oldx=x;
oldy=y;
}
else if(x<=800&&flag==2)
{
g.setColor(ui.getBackground());
g.fillOval(oldx, oldy, 50, 50);
g.setColor(Color.BLACK);
g.fillOval(x+=(sizex*-1), y+=sizey, 50, 50);
if(y==600) {
flag=1;
System.out.println("flag="+1);
sizex=-1*sizex;
}else if(y==0){
flag=4;
sizex=-1*sizex;
}
oldx=x;
oldy=y;
}
else if(x>=0&&flag==3)
{
System.out.println("左边框碰撞");
g.setColor(ui.getBackground());
g.fillOval(oldx, oldy, 50, 50);
g.setColor(Color.BLACK);
g.fillOval(x+=(sizex*-1), y+=sizey, 50, 50);
if(y==600) {
flag=1;
System.out.println("flag="+1);
sizex=-1*sizex;
}else if(y==0){
flag=4;
sizex=-1*sizex;
}
oldx=x;
oldy=y;
}
else if(flag==0)
{
g.setColor(ui.getBackground());
g.fillOval(oldx, oldy, 50, 50);
System.out.println("x="+x+" y="+y);
if(sizex<0&&sizey<0) {
System.out.println("sizex="+sizex+" sizey="+sizey);
return;
}
else if(x<800&&x>0&&y<600&&y>0){
g.setColor(Color.BLACK);
g.fillOval(x+=sizex, y+=50, 50, 50);
oldx=x;
oldy=y;
System.out.println("sizex="+sizex+" sizey="+sizey);
System.out.println("flag="+flag);
System.out.println("x="+x+" y="+y);
if(x==800||x==0)
{
flag=2;
System.out.println("flag="+flag);
}
else if(y==600||y==0)
{
flag=1;
System.out.println("flag="+flag);
}
}
}
}
}
}