多线程练习(4个从对角出发并弹回的小球)

1.定义和基本写法:
定义:
在一个程序中,这些独立运行的程序片断叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”。多线程处理一个常见的例子就是用户界面。利用线程,用户可按下一个按钮,然后程序会立即作出响应,而不是让用户等待程序完成了当前任务以后才开始响应。

线程类的写法:
public class Name extends Thread(){
//重新其中的RUN方法
public void run(){
a();
}
}

创建线程对象的写法:
Namethread pt=new Namethread();
启动线程对象:
pt.start();
注意:一个线程对象只能启动一次,否则会报错。
线程中的操作都可执行,但顺序不可控。

2.基本思路和源代码:

示例程序:4个从对角出发并弹回的小球

基本思路:给显示框加一个按钮(加在一个JPanel上,格式默认为流式),再给按钮加一个监听器。在监听器中创建并开启4个线程。线程中有4个函数,各用来画4个方向的小球。基本相同,只有坐标的计算方式不同。开一个比较大的循环,不断地画圆,记录下每一个圆的位置,在画新的圆之前擦去旧的。(清除函数:a.clearrect)并且控制色彩,使它渐变。

程序代码:
[/code]
主函数:
import java.awt.Dimension;
import java.awt.Graphics;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ball extends JFrame{
boolean go=true;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ball b=new ball();
b.InitGUI();
}

private void InitGUI() {
// TODO Auto-generated method stub
this.setTitle("球");
this.setSize(new Dimension(600,600));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setVisible(true);
Graphics g=this.getGraphics();
JPanel JP=new JPanel();
JButton BU=new JButton("PUSH");
this.add(JP);
JP.add(BU);

BUAction l=new BUAction(g);
BU.addActionListener(l);
}

}
监听器BUAction:
[code="java"]

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BUAction implements ActionListener{
private Graphics g;
public BUAction(Graphics g){
this.g=g;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Printthred p1=new Printthred(g,1);
p1.start();
Printthred p2=new Printthred(g,2);
p2.start();
Printthred p3=new Printthred(g,3);
p3.start();
Printthred p4=new Printthred(g,4);
p4.start();
}

}
线程类Printthred:
[/code]
import java.awt.Color;
import java.awt.Graphics;

public class Printthred extends Thread{
int ii=0,jj=0,c1=0,c2=0,c3=0,c4=0;
private Graphics g;
private int type;
public Printthred(Graphics g,int type){
this.g=g;
this.type=type;
}
public void run(){
if(type==1)draw1();
if(type==2)draw2();
if(type==3)draw3();
if(type==4)draw4();
}
public void draw1(){
for (int i=0;i<1000;i++){
c1=c1+1;
g.setColor(new Color((100),(200),(c1)%256));
if(i>=300){
g.clearRect(300-(ii-300),300-(ii-300), 30, 30);
g.fillOval(300-(i-300), 300-(i-300), 30, 30);

}else
{ g.clearRect(ii, jj, 30, 30);
g.fillOval(i, i, 30, 30);

}
try{
Thread.sleep(10);
}catch(Exception ef){}
ii=i;jj=i;
}
}
public void draw2(){
for (int i=0;i<1000;i++){
c2=c2+1;
g.setColor(new Color((100),(200),(c2)%256));
if(i>=300){
g.clearRect(ii,300-(ii-300), 30, 30);
// g.setColor(Color.white);
// g.fillRect(ii,300-(ii-300), 30, 30);
g.fillOval(i, 300-(i-300), 30, 30);

}else
{ g.clearRect(ii, 600-jj, 30, 30);
g.fillOval(i, 600-i, 30, 30);

}
try{
Thread.sleep(10);
}catch(Exception ef){}
ii=i;jj=i;
}
}
public void draw3(){
for (int i=0;i<1000;i++){
c3=c3+1;
g.setColor(new Color((250),(c3)%256,(250)));
if(i>=300){
g.clearRect(300-(ii-300),ii, 30, 30);
g.fillOval(300-(i-300), i, 30, 30);

}else
{ g.clearRect(600-ii, jj, 30, 30);
g.fillOval(600-i, i, 30, 30);

}
try{
Thread.sleep(10);
}catch(Exception ef){}
ii=i;jj=i;
}
}
public void draw4(){
for (int i=0;i<1000;i++){
c4=c4+1;
g.setColor(new Color((250),(c4)%256,(250)));
if(i>=300){
g.clearRect(ii,ii, 30, 30);
g.fillOval(i,i, 30, 30);

}else
{ g.clearRect(600-ii,600-jj, 30, 30);
g.fillOval(600-i, 600-i, 30, 30);

}
try{
Thread.sleep(10);
}catch(Exception ef){}
ii=i;jj=i;
}
}
}

其他练习:跳动的图形。
基本思路:
把圆换成图片:
加图片方法示例:
ImageIcon pic=new ImageIcon("image/black.png");
Image img = pic.getImage();
g.drawImage(img, x,600-y,pic.getIconWidth(),pic.getIconHeigh(),null);
Y的坐标用SIN(X)算,并且扩大几倍。

源代码:
[code="java"]

线程:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;

public class Printthred2 extends Thread{
int ii=0,jj=0,c1=0,j,x,y;
private Graphics g;
private int type;
public Printthred2(Graphics g,int type){
this.g=g;
this.type=type;
}
public void run(){
if(type==1)draw1();
}
public void draw1(){
for (int i=0;i<100;i++){

c1=c1+1;
g.setColor(new Color((100),(200),(c1)%256));
x=x+i*2;
y=(int)(Math.sin(x)*200);
ImageIcon pic=new ImageIcon("image/black.png");
Image img = pic.getImage();
g.drawImage(img, x,600-y,pic.getIconWidth(),pic.getIconHeight(),null);
g.clearRect(ii,600-jj,pic.getIconWidth(),pic.getIconHeight());

try{
Thread.sleep(100);
}catch(Exception ef){}
ii=x;jj=y;
}
}

}

画图形的方法好麻烦啊……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值