/** *一个典型的多线程小实例球的运动 *几乎每一处都有注释只要一点时间就能看懂 *适合初学者学习交流 */ 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){} } } } |