源码:
package E3;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class drawBall4_3 extends Panel implements Runnable, KeyListener {
private double x;
double dx;
private double y;
double dy;
private double x2,dx2;
private double y2,dy2;
private double diameter;
private int width=400;
private int height=400;
private Image im;
private int xdirection=2,ydirection=2;
private int xdirection2=2,ydirection2=2;
public drawBall4_3() {
x=5;
y=50;
dx=2;
dy=2;
x2=100;
y2=10;
dx2=1;
dy2=1;
diameter=30;
setBackground(Color.pink);
setPreferredSize(new Dimension(width, height));
addKeyListener(this);
}
public void gameStart(){
Thread gamethread = new Thread(this);
gamethread.start();
}
public void gamePaint() {//前屏显示
Graphics g;
g = this.getGraphics();
g.drawImage(im, 0, 0, null);//从后屏复制到前屏
g.dispose();//释放当前屏幕Graphcis对象
}
public void gameRender() {//离屏绘制
im = createImage(this.getWidth() , this.getHeight());//建立image对象
Graphics dbg = im.getGraphics();//获取Graphics对象
dbg.setColor(Color.pink);//设置绘图属性
dbg.fillRect(0, 0,width , height);
dbg.setColor(Color.blue);
dbg.fillOval((int)x, (int)y,(int)diameter,(int)diameter);//绘制图形
dbg.setColor(Color.yellow);
dbg.fillOval((int)x2, (int)y2,(int)diameter,(int)diameter);//绘制图形
}
public void gameUpdate() {
x+=dx;
y+=dy;
x2+=dx2;
y2+=dy2;
if(y<0)
dy=-dy;
if(y+30>400)
dy=-dy;
if(x<0)
dx=-dx;
if(x+30>400)
dx=-dx;
if(x2<0)
dx2=-dx2;
if(x2+30>400)
dx2=-dx2;
if(y2<0)
dy2=-dy2;
if(y2+30>400)
dy2=-dy2;
if ((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y) <= diameter*diameter) //当两球间的距离小于直径时,可认为两小球发生了碰撞
{
double degree = Math.atan((y - y2) / (x - x2)); //获取自己与发生碰撞的小球之间所形成的夹角,因为夹角只能在-pi/2-pi/2之间,所以还需判断两球的x坐标之间的关系
if (x > x2) //如果自己的x坐标大于发生碰撞的小球的x坐标,由数学知识可知自己应该往正向运动
{
dx = Math.cos(degree);
dy = Math.sin(degree);
x+=dx;
y+=dy;
x2+=dx2;
y2+=dy2;
}
else //如果自己的x坐标小于发生碰撞的小球的x坐标,由数学知识可知应该朝负向运动
{
dx =-Math.cos(degree);
dy = -Math.sin(degree);
x+=dx;
y+=dy;
x2+=dx2;
y2+=dy2;
}
double degree1 = Math.atan((y2- y) / (x2 - x)); //获取自己与发生碰撞的小球之间所形成的夹角,因为夹角只能在-pi/2-pi/2之间,所以还需判断两球的x坐标之间的关系
if (x2 > x) //如果自己的x坐标大于发生碰撞的小球的x坐标,由数学知识可知自己应该往正向运动
{
dx2 = Math.cos(degree);
dy2= Math.sin(degree);
x+=dx;
y+=dy;
x2+=dx2;
y2+=dy2;
}
else //如果自己的x坐标小于发生碰撞的小球的x坐标,由数学知识可知应该朝负向运动
{
dx2 =-Math.cos(degree);
dy2 =-Math.sin(degree);
x+=dx;
y+=dy;
x2+=dx2;
y2+=dy2;
}}
}
public void run() {
while(true){
gameUpdate();
gameRender();
gamePaint();
}
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}}
package E3;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class GameFrame4_3 {
public GameFrame4_3() {
JFrame app = new JFrame("GameFrame4_3");
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
app.setLocation(100, 100);
app.setLocationRelativeTo(null);
drawBall4_3 drawB = new drawBall4_3();
app.add(drawB);
app.add(drawB, BorderLayout.CENTER);
app.pack();
app.setVisible(true);
drawB.gameStart();
}
public static void main(String[] args) {
new GameFrame4_3();
}
}