熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

典型多線程小實例:球的運動

2013-11-23 19:57:49  來源: Java高級技術 
    /**
    *一個典型的多線程小實例球的運動
    *幾乎每一處都有注釋只要一點時間就能看懂
    *適合初學者學習交流
    */
    import javaawt*;
    import javaawtevent*;
    import javaxswing*;
    import javaxswingevent*;

    public class ball
    {
    public static void main(String args[])
    {
    new frame();
    }
    }
    class frame extends JFrame implements ActionListenerChangeListener
    {
    JPanel jp_HuaBan;
    JPanel jp_YuLan;
    JSlider js_red;
    JSlider js_green;
    JSlider js_blue;
    JSlider js_size;
    int x=y=;
    int ball_size=;

    public frame()
    {
    super(ball);

    //創建畫板
    jp_HuaBan=new JPanel();
    jp_HuaBansetBounds();
    jp_HuaBansetBackground(Colorwhite);

    //創建球的予覽框
    jp_YuLan=new JPanel();
    jp_YuLansetBounds();
    jp_YuLansetBackground(Colorwhite);

    //創建一個文本框作為狀態欄
    JTextField jt_status=new JTextField(請選擇球的顏色大小然後點擊按鈕);
    jt_statussetBounds();
    jt_statussetEditable(false);

    //創建紅藍三個標簽
    JLabel jl_red=new JLabel();
    jl_redsetBounds();
    JLabel jl_green=new JLabel();
    jl_greensetBounds();
    JLabel jl_blue=new JLabel();
    jl_bluesetBounds();
    JLabel jl_size=new JLabel(大小);
    jl_sizesetBounds();

    //創建紅藍三個滑塊
    js_red=new JSlider(SwingConstantsHORIZONTAL);
    js_redsetBounds();
    //填充滑塊
    js_redputClientProperty(JSliderisFilledBooleanTRUE);
    //繪制勾號標記
    js_redsetPaintTicks(true);
    //主要的勾號標記大小
    js_redsetMajorTickSpacing();
    //次要的勾號標記大小
    js_redsetMinorTickSpacing();
    //顯示主要刻度的數字標記
    js_redsetPaintLabels(true);
    //添加監聽者
    js_redaddChangeListener(this);

    js_green=new JSlider(SwingConstantsHORIZONTAL);
    js_greensetBounds();
    js_greenputClientProperty(JSliderisFilledBooleanTRUE);
    js_greensetPaintTicks(true);
    js_greensetMajorTickSpacing();
    js_greensetMinorTickSpacing();
    js_greensetPaintLabels(true);
    js_greenaddChangeListener(this);
    js_blue=new JSlider(SwingConstantsHORIZONTAL);
    js_bluesetBounds();
    js_blueputClientProperty(JSliderisFilledBooleanTRUE);
    js_bluesetPaintTicks(true);
    js_bluesetMajorTickSpacing();
    js_bluesetMinorTickSpacing();
    js_bluesetPaintLabels(true);
    js_blueaddChangeListener(this);
    //創建球的大小的滑塊
    js_size=new JSlider(SwingConstantsHORIZONTAL);
    js_sizesetBounds();
    js_sizeputClientProperty(JSliderisFilledBooleanTRUE);
    js_sizesetPaintTicks(true);
    js_sizesetMajorTickSpacing();
    js_sizesetMinorTickSpacing();
    js_sizesetPaintLabels(true);
    js_sizeaddChangeListener(this);

    //創建按鈕
    JButton jb=new JButton(ball);
    jbsetBounds();
    jbaddActionListener(this);

    //將所有對象添加到窗體
    Container c=thisgetContentPane();
    csetLayout(null);
    cadd(jp_HuaBan);
    cadd(jp_YuLan);
    cadd(jl_red);
    cadd(jl_green);
    cadd(jl_blue);
    cadd(jl_size);
    cadd(js_red);
    cadd(js_green);
    cadd(js_blue);
    cadd(js_size);
    cadd(jb);
    cadd(jt_status);
    //設置窗體的位置和大小
    thissetBounds();
    thissetResizable(false);
    thissetVisible(true);
    thisaddWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    Systemexit();
    }
    });
    }

    public void actionPerformed(ActionEvent e)
    {
    Color ball_color=new Color(js_redgetValue()
    js_greengetValue()js_bluegetValue());
    rball r=new rball(jp_HuaBanjs_sizegetValue()ball_color);
    rstart();
    lball l=new lball(jp_HuaBanjs_sizegetValue()ball_color);
    lstart();
    }

    public void stateChanged(ChangeEvent e)
    {
    //獲得jp_YuLan的畫筆並在jp_YuLan的中心畫個圓
    Graphics g=jp_YuLangetGraphics();
    gsetColor(Colorwhite);
    gfillOval(xyball_sizeball_size);
    x=js_sizegetValue()/;
    y=js_sizegetValue()/;
    ball_size=js_sizegetValue();
    gsetColor(new Color(js_redgetValue()
    js_greengetValue()js_bluegetValue()));
    gfillOval(xyball_sizeball_size);
    gdispose();
    }
    }

    class lball extends Thread
    {
    //從窗口左面出現的球
    JPanel jp_HuaBan;
    int ball_size;
    Color ball_color;

    public lball(JPanel jint iColor c)
    {
    //獲得畫板的句柄
    jp_HuaBan=j;
    //獲得球的大小
    ball_size=i;
    //獲得球的顏色
    ball_color=c;
    }

    public void run()
    {
    Graphics g=jp_HuaBangetGraphics();
    int x=y=;
    //計算畫球時X軸Y軸的最大坐標
    int HuaBan_x=ball_size;
    int HuaBan_y=ball_size;
    //球移動的增量
    int x_increase=y_increase=;
    while(true)
    {
    //將上一次畫的球擦掉
    gsetColor(Colorwhite);
    gfillOval(xyball_sizeball_size);
    //畫球
    gsetColor(ball_color);
    x=x+x_increase;
    y=y+y_increase;
    gfillOval(xyball_sizeball_size);
    //判斷球是否到達了邊界若到達了則轉向
    if(x<= || x>=HuaBan_x) x_increase=x_increase;
    if(y<= || y>=HuaBan_y) y_increase=y_increase;
    try
    {
    //休眠一段時間
    Threadsleep();
    }
    catch(Exception e){}
    }
    }
    }

    class rball extends Thread
    {
    //從窗口左面出現的球
    JPanel jp_HuaBan;
    int ball_size;
    Color ball_color;

    public rball(JPanel jint iColor c)
    {
    jp_HuaBan=j;
    ball_size=i;
    ball_color=c;
    }

    public void run()
    {
    Graphics g=jp_HuaBangetGraphics();
    int x=ball_sizey=;
    int HuaBan_x=x;
    int HuaBan_y=ball_size;
    int x_increase=y_increase=;
    while(true)
    {

    gsetColor(Colorwhite);
    gfillOval(xyball_sizeball_size);
    gsetColor(ball_color);
    x=x+x_increase;
    y=y+y_increase;
    gfillOval(xyball_sizeball_size);
    if(x<= || x>=HuaBan_x) x_increase=x_increase;
    if(y<= || y>=HuaBan_y) y_increase=y_increase;
    try
    {
    Threadsleep();
    }
    catch(Exception e){}
    }
    }
    }


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27730.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.