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.setColor(Color.red);
g.fillOval(x, y, width, height);
}
int i=1;
@Override
public void run()
{
// TODO Auto-generated method stub
while(true)
{
if(yAdd)
y+=15;
else
y-=15;
if(y>=this.getHeight()-height||y<=25)
yAdd=!yAdd;
if(xAdd)
x+=30;
else
x-=30;
if(x>=this.getWidth()-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 image=ImageIO.read(new File(Main.class.getResource("").toString()));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
您可能感兴趣的文章:
- java实现小球碰撞反弹
- 用Java实现小球碰壁反弹的简单实例(算法十分简单)
- 原生JS实现多个小球碰撞反弹效果示例
- 用Java实现小球碰壁反弹(算法十分简单)
- java程序利用HttpSessionListener实现统计在线人数(示例代码)
- java调用vbs,实现打开关闭excel操作小示例
- 分别在XML文件、Java代码中实现动画效果 Animation(带示例)
- YAML的Java实现——JYAML基本原理与示例(2)导入YAML格式文件
- java实现随机动态的小球
- 用java读写ini配置文件的原因以及实现 示例
- java 之 编码实现Filter学习示例
- YAML的Java实现——JYAML基本原理与示例(3)YAML对文件流的处理
- Java实现最简单的拖拽代码示例
- YAML的Java实现——JYAML基本原理与示例(1)导出数据为YAML格式文件
- Java实现时间日期格式转换示例
- LoadRunner Java Vuser 实现 Get/Post 示例
- 简单的用java实现读/写文本文件的示例
- Java数据结构之简单链表的定义与实现方法示例
- 使用JAVA实现签名验证示例程序
- YAML的Java实现——JYAML基本原理与示例(3)YAML对文件流的处理