package bar; import java.applet.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*; class ball
{int cx,cy,color;
ball(int color,int x,int y)
{this.color=color;
this.cx=x;
this.cy=y;
}
}
public class Game extends JApplet
{
public void init()
{
Container cp=getContentPane();
CBox pa=new CBox();
pa.setBackground(Color.orange);
cp.add(pa,BorderLayout.CENTER);
}
}
class CBox extends JPanel implements Runnable
{
int x1,y1,x2,y2;
Font f=new Font("TimesRoman",Font.BOLD,24);
Thread th1=null;
Thread th2=null;
public CBox()
{
th1=new Thread(this);
th1.start();
th2=new Thread(this);
th2.start();
}
public void run()
{
x1=0;
y1=330;
x2=960;
y2=330;
boolean m=true,n=false; //true为正向,false为反向
while(true)
{
if(x1 <=0)
{
m = !m;
}
if(x2>=960)
{
n = !n;
}
if(m==false)
x1=x1-5;
else
x1=x1+5;
if(n==true)
x2=x2-10;
else
x2=x2+10;
repaint(); try
{th1.sleep(60);}
catch(InterruptedException e){};
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.setFont(f);
Ellipse2D.Double e1=new Ellipse2D.Double(x1,y1,60,60);
g2.fill(e1);
g2.setColor(Color.blue);
Ellipse2D.Double e2=new Ellipse2D.Double(x2,y2,60,60);
g2.fill(e2);
}
} 简单的实现小球碰撞弹回 x1==x2时 m,n取反 未实现
求助 刚注册没分
谢谢
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*; class ball
{int cx,cy,color;
ball(int color,int x,int y)
{this.color=color;
this.cx=x;
this.cy=y;
}
}
public class Game extends JApplet
{
public void init()
{
Container cp=getContentPane();
CBox pa=new CBox();
pa.setBackground(Color.orange);
cp.add(pa,BorderLayout.CENTER);
}
}
class CBox extends JPanel implements Runnable
{
int x1,y1,x2,y2;
Font f=new Font("TimesRoman",Font.BOLD,24);
Thread th1=null;
Thread th2=null;
public CBox()
{
th1=new Thread(this);
th1.start();
th2=new Thread(this);
th2.start();
}
public void run()
{
x1=0;
y1=330;
x2=960;
y2=330;
boolean m=true,n=false; //true为正向,false为反向
while(true)
{
if(x1 <=0)
{
m = !m;
}
if(x2>=960)
{
n = !n;
}
if(m==false)
x1=x1-5;
else
x1=x1+5;
if(n==true)
x2=x2-10;
else
x2=x2+10;
repaint(); try
{th1.sleep(60);}
catch(InterruptedException e){};
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.setFont(f);
Ellipse2D.Double e1=new Ellipse2D.Double(x1,y1,60,60);
g2.fill(e1);
g2.setColor(Color.blue);
Ellipse2D.Double e2=new Ellipse2D.Double(x2,y2,60,60);
g2.fill(e2);
}
} 简单的实现小球碰撞弹回 x1==x2时 m,n取反 未实现
求助 刚注册没分
谢谢
解决不了再来问