java⼩球反弹公式,java实现会反弹的⼩球⽰例package tsct;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowStateListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Main extends JFrame implements Runnable
{
int x=40;
int y=30;
int width=50;
int height=50;
boolean xAdd=true;
boolean yAdd=true;
public void paint(Graphics g)
{
super.paint(g);
//g.fillRect(x, y, width, height);
g.d);
g.fillOval(x, y, width, height);
}
int i=1;
@Override
public void run()
{
// TODO Auto-generated method stub
while(true)
{
if(yAdd)
y+=15;javaswing实现购买
y-=15;
if(y>=Height()-height||y<=25) yAdd=!yAdd;
if(xAdd)
x+=30;
else
x-=30;
if(x>=Width()-width||x<=5) xAdd=!xAdd;
repaint();
try
{
Thread.sleep(40);
i++;
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//        Main main=new Main();
//
/
/        main.setVisible(true);
//        main.run();
Main main=new Main();
main.setVisible(true);
main.setBounds(50, 50, 800, 600); Thread thread=new Thread(main); thread.start();
try
Image ad(new File(Resource("").toString())); }
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。