线程动画屏保小球碰撞c语言,线程小动画之屏保--模仿小球碰撞反弹(一)
相信大家都對屏幕保護中的小球碰撞動畫深有印象吧,許多色彩繽紛的小球在屏幕上滾動,碰撞,然后一直重復如此過程......現在就讓我們自己嘗試去做一下吧,或許你做的比它的要更好呦!
不必說,最重要也是最先弄的,自然是要產生小球啦,因此就要先創建一個球的線程類。這里先放上一些關于線程創建的知識,有助于大家鞏固記憶啦。
線程的創建有兩種方式,分別為繼承Thread類(已經實現了Runnable接口)與實現Runnable接口。
1.Thread類
常用方法包括:start()、interrupt()、join()、run()方法等。start()方法用于啟動線程,run()方法為線程實現功能的方法,可以根據需求覆寫run()方法。
構造函數8個,常用4個:
Thread thread=new Thread();????????????????????????????????? //無參的Thread類構造函數
Thread thread=new Thread(Runnable simple);?????????????????? //參數為實現Runnable接口類對象的構造函數
Thread thread=new Thread("ThreadName");????????????????????? //參數為線程名稱的構造函數
Thread thread=new Thread(Runnable simple,"ThreadName");????? //無參的Thread類構造函數
2.Runnable接口
它只有一個方法就是run()方法,實現Runnable()接口后必須覆寫run()方法。
嗯,如上所說,先創建一個球的線程類:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import javax.swing.JPanel;
public class MyThread extends Thread{
private JPanel panel;
private int x,y;//坐標位置
private int radius=50;//球半徑
private int vx,vy;//橫縱坐標該變量
private Color color;
public MyThread(JPanel panel)
{
this.panel=panel;
Random random=new Random();//創建一個隨機類變量
vx=random.nextInt(20)+10;
vy=random.nextInt(10)+5;
radius=random.nextInt(10)+20;
color=new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
}
//重寫run的方法
public void run()
{
Random random=new Random();
//Graphics g=panel.getGraphics();//獲得窗體界面
Graphics2D g2=(Graphics2D)panel.getGraphics();
panel.setBackground(Color.BLACK);
while(true)
{
try{
Thread.sleep(100);//調用線程睡眠方法,讓線程能暫緩進行
}catch(InterruptedException e ){
e.printStackTrace();
}
//覆蓋小球先前的軌跡
g2.setColor(panel.getBackground());//獲得窗體背景顏色
g2.fillOval(x, y, 2*radius, 2*radius);
x+=vx;
y+=vy;
g2.setColor(color);
g2.fillOval(x, y, 2*radius, 2*radius);
//若小球碰撞到面板邊緣,則反彈
if(x<0||x>panel.getWidth()-2*radius)
{
vx=-vx;
}
if(y<0||y>panel.getHeight()-2*radius)
{
vy=-vy;
}
}
}
}
這里大概要注意兩點吧:
1.要覆蓋小球先前的軌跡,不然所有時刻的軌跡都會顯現在窗體上。
2.考慮小球出界時的情況,設定反彈條件
之后當然就是寫一個測試類
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestFrame {
private JPanel centerpanel;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestFrame tf=new TestFrame();
tf.init();
}
//初始化界面的方法
public void init()
{
JFrame jf=new JFrame("線程入門");
jf.setSize(800, 600);
jf.setDefaultCloseOperation(3);
jf.setResizable(false);
JPanel northpanel=new JPanel();
northpanel.setSize(800, 200);
JButton jbu=new JButton("開始");
//jbu.setPreferredSize(new Dimension(50,50));
jbu.addActionListener(al);
northpanel.add(jbu);
jf.add(northpanel,BorderLayout.NORTH);
centerpanel=new JPanel();
jf.add(centerpanel,BorderLayout.CENTER);
jf.setVisible(true);
}
ActionListener al=new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
MyThread thread=new MyThread(centerpanel);//創建一個線程
thread.start();//啟動線程
}};
}
提醒下的是,最好創建兩個面板,北邊面板放按鈕,中間面板當畫布,這里小球運動時就不會影響到按鈕的輪廓。
這樣操作完后,就能產生許許多多的小球啦。
總結
以上是生活随笔為你收集整理的线程动画屏保小球碰撞c语言,线程小动画之屏保--模仿小球碰撞反弹(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【兼容封装】addEventListen
- 下一篇: Rainmeter雨滴天气-(永不过时版