这个程序功能:利用线程数组的概念实现图像的分割进度显示
以下这个程序能够正确的运行,能够实行理想中的功能;
怎样才能在init()或start()中取得图像的宽度和高度,使得坐标数组和线程数初始化更加合理?
在Updata(Graphics g)中肯定是行的,但是程序肯定是不会正确运行的!但是在start() or init()
中不能repaint()! 
num=(int)pic_width/displayheight;
num=nu;import java.awt.*;
import java.applet.Applet;public  class  mythread extends Applet implements Runnable
{  //公共变量部分
   
   int p,num=800,nu=800,displayheight=2,pic_height,pic_width;
   boolean b=true;
   Image pic;
   int myi;//线程的姓名
   Thread[] thread=new Thread[num];
   int i[]; 
    //坐标系部分
      int x1[]= new int[nu];
      int y1[]=new int[nu];

      int x2[]=new int[nu];
      int y2[]=new int[nu];

      int x3[]=new int[nu];
      int y3[]=new int[nu];
  
      int x4[]=new int[nu];
      int y4[]=new int[nu];  


     public void init()
     {  pic=getImage(getDocumentBase(),"mytest.jpg");
         /* pic_width=pic.getWidth(this);
                    pic_height=pic.getHeight(this);*/
                   
      System.out.println("init!");  
        
      }
public void start()
  {   System.out.println("stared!");
                  for(int i=0;i<num;i++)
     {
                        thread[i]=new Thread(this);
               thread[i].setName(String.valueOf(i));
      thread[i].start();
          }
  }
        
 
    public void stop()
 {       for(int i=0;i<num;i++)
     {
              thread[i].stop();
                 thread[i]=null;
              }
             }
           
        public   void run() 
     { 
  
      int  i[]=new int[num];
    
      try

for( i[(Integer.parseInt(Thread.currentThread().getName()))]=0;x3[Integer.parseInt(Thread.currentThread().getName())]<=800/*这个800是图像的宽度,我想用pic_width替代*/;i[(Integer.parseInt(Thread.currentThread().getName()))]++)
 {//屏幕目标坐标
 x1[Integer.parseInt(Thread.currentThread().getName())]=0;
 y1[Integer.parseInt(Thread.currentThread().getName())]=(int)(30+displayheight*(Integer.parseInt(Thread.currentThread().getName())));
 x2[Integer.parseInt(Thread.currentThread().getName())]=800;
 y2[Integer.parseInt(Thread.currentThread().getName())]= (int)y1[Integer.parseInt(Thread.currentThread().getName())]+displayheight);
 x3[Integer.parseInt(Thread.currentThread().getName())]=0;
 y3[Integer.parseInt(Thread.currentThread().getName())]=(int)(displayheight*(Integer.parseInt(Thread.currentThread().getName())));
 x4[Integer.parseInt(Thread.currentThread().getName())]=800;
 y4[Integer.parseInt(Thread.currentThread().getName())]= (int)(y3[Integer.parseInt(Thread.currentThread().getName())]+displayheight);

Thread.sleep((int)(Math.random()*1000));
synchronized(this)
         {  myi=Integer.parseInt(Thread.currentThread().getName());
 repaint();}

 }
 }
catch(InterruptedException e){}
//System.out.println("mythread  "+myi+"cancel running....");

}          public void update(Graphics g)
           {   
              g.drawImage(pic,x1[myi],y1[myi],x2[myi],y2[myi],x3[myi],y3[myi],x4[myi],y4[myi],this);
               
             }
}

解决方案 »

  1.   

    我们的教科书上是这样写的repaint()是再次调运public void paint(Graphics g)方法的。
    而你问的其他问题我也不知道。还请大虾们给你回复了,我们共同学习。^_^
      

  2.   

    小改了一下你的程序,主要是实现了Pic_height以及Pic_width的获取,去掉一些无用的变元以及一些重复的无用的语句,最后重新排版了一下。另外我觉得thread.stop()用得不是很稳妥,看看有没有哪位大虾可以改进一下。程序代码如下:
    import java.awt.*;
    import java.applet.Applet;public  class  mythread extends Applet implements Runnable
    {  //公共变量部分
       
       int num=800,displayheight=2,pic_height,pic_width;
       Image pic;
       int myi;//线程的姓名
       Thread[] thread=new Thread[num];
        //坐标系部分
       int x1[]=new int[num];
       int y1[]=new int[num];   int x2[]=new int[num];
       int y2[]=new int[num];   int x3[]=new int[num];
       int y3[]=new int[num];
      
       int x4[]=new int[num];
       int y4[]=new int[num];     public mythread()
       {
       super();
       }
       public void init()
       {
       pic=getImage(getCodeBase(),"mytest.jpg");
           System.out.println(getCodeBase());
           MediaTracker mt=new MediaTracker(this);
           mt.addImage(pic,0);
           try{
            mt.waitForID(0);
           }catch(Exception ex){
            ex.printStackTrace();
            System.out.println("Picture is not loaded right!");
           }
           pic_width=pic.getWidth(this);
           pic_height=pic.getHeight(this);
           this.setSize(pic_width,pic_height);
           }
       public void start()
       {
       for(int i=0;i<num;i++)
           {
       thread[i]=new Thread(this);
               thread[i].setName(String.valueOf(i));
               thread[i].start();
           }
       }
       public void stop()
       {
       for(int i=0;i<num;i++)
       {
       try{
       thread[i].stop();
                 }catch(Exception ex)
                 {
                  ex.printStackTrace();
                 }
                 thread[i]=null;
       }
       }
       public void run() 
       {
       int temp=Integer.parseInt(Thread.currentThread().getName());
           try
           {
            while(true)
            {//屏幕目标坐标
            x1[temp]=0;
            y1[temp]=(int)(displayheight*(temp));
            x2[temp]=pic_width;
                   y2[temp]= (int)(y1[temp]+displayheight);
                   x3[temp]=0;
                   y3[temp]=y1[temp];
                   x4[temp]=pic_width;
                   y4[temp]=y2[temp];
                   Thread.sleep((int)(Math.random()*1000));
                   synchronized(this)
                   {
                    myi=temp;
                    if(y2[temp]<=pic_height)
                       repaint();
                   }
              }
           }
           catch(InterruptedException e){
            e.printStackTrace();
           }
           System.out.println("mythread  "+myi+"cancel running....");
       }   public void update(Graphics g)
       {
       g.drawImage(pic,x1[myi],y1[myi],x2[myi],y2[myi],x3[myi],y3[myi],x4[myi],
                            y4[myi],this);
       }
    }
      

  3.   

    哦,发上去还是有点乱,再贴一次。小改了一点点。
    import java.awt.*;
    import java.applet.Applet;public  class  mythread extends Applet implements Runnable
    {  //公共变量部分
       
       int num=800,displayheight=2,pic_height,pic_width;
       Image pic;
       int myi;//线程的姓名
       Thread[] thread=new Thread[num];
        //坐标系部分
       int x1[]=new int[num];
       int y1[]=new int[num];   int x2[]=new int[num];
       int y2[]=new int[num];   int x3[]=new int[num];
       int y3[]=new int[num];
      
       int x4[]=new int[num];
       int y4[]=new int[num];     public mythread()
       {
       super();
       }
       public void init()
       {
       pic=getImage(getDocumentBase(),"mytest.jpg");
           MediaTracker mt=new MediaTracker(this);
           mt.addImage(pic,0);
           try{
            mt.waitForID(0);
           }catch(Exception ex){
            ex.printStackTrace();
            System.out.println("Picture is not loaded right!");
           }
           pic_width=pic.getWidth(this);
           pic_height=pic.getHeight(this);
           this.setSize(pic_width,pic_height);
           }
       public void start()
       {
       for(int i=0;i<num;i++)
           {
       thread[i]=new Thread(this);
               thread[i].setName(String.valueOf(i));
               thread[i].start();
           }
       }
       public void stop()
       {
       for(int i=0;i<num;i++)
       {
       try{
       thread[i].stop();
                 }catch(Exception ex)
                 {
                  ex.printStackTrace();
                 }
                 thread[i]=null;
       }
       }
       public void run() 
       {
       int temp=Integer.parseInt(Thread.currentThread().getName());
           try
           {
           x1[temp]=0;
              y1[temp]=(int)(displayheight*(temp));
           x2[temp]=pic_width;
              y2[temp]= (int)(y1[temp]+displayheight);
              x3[temp]=0;
              y3[temp]=y1[temp];
              x4[temp]=pic_width;
              y4[temp]=y2[temp];
              Thread.sleep((int)(Math.random()*1000));
              while(true)
              {//屏幕目标坐标
                   synchronized(this)
                   {               
                    myi=temp;
                    if(y2[temp]<=pic_height)
                       repaint();
                   }
              }
           }catch(InterruptedException e){
            e.printStackTrace();
           }
           System.out.println("mythread  "+myi+"cancel running....");
       }   public void update(Graphics g)
       {
       g.drawImage(pic,x1[myi],y1[myi],x2[myi],y2[myi],x3[myi],y3[myi],x4[myi],y4[myi],this);
       }
    }
      

  4.   

    我刚自学JAVA不到一个月!能否将你改进的部分注释一下,有好多生词不认识!
      

  5.   

    翻了许多的资料,终于知道你写的是什么了!其实也很简单,就是MediaTracker在后台启动几个线程来加载图像!  将pic=getImage(getDocumentBase(),  "mytest.jpg  ");  改成pic=getImage(this.getDocumentBase(),  "mytest.jpg  "); 在本对象中取得图像的规格信息,其实你引起一个严重的错误,你把我的程序简单化了,不错,看起来清晰易懂,但是你还不知道我为什么要这样写!
                int  temp=Integer.parseInt(Thread.currentThread().getName());  
                 try  
                 {  
                           x1[temp]=0;  
                       y1[temp]=(int)(displayheight*(temp));  
                           x2[temp]=pic_width;  
                       y2[temp]=  (int)(y1[temp]+displayheight);  
                       x3[temp]=0;  
                       y3[temp]=y1[temp];  
                       x4[temp]=pic_width;  
                       y4[temp]=y2[temp];  
                       Thread.sleep((int)(Math.random()*1000));  
    就是上面这几行,这些线程全部启动,就会不断的修改TEMP的值,那么最后一个执行线程将会得到正确坐标系的值!我那样写的目的是:为了各线程互不干扰,各自在自已的存储区域内进行操
    作,避让了线程的同步!
          我开始也是像你这样写的,我把坐标系的值输出来一看,值混乱,因为是同时执行的!
          我觉得我这个程序是个垃圾!但是我在这个程序中知道线程是怎样运行的了!
          还有一个问题我也解决了!多线程的停止,在线程内部加一个结束标志就行了,各个线程运行同时检测这个标志位,如果为TRUE,运行!否则停止!我就不多说了!大家共同进步!这个问题OK了!
      

  6.   

    呵呵,欢迎楼主共同讨论。说实话,光看楼主之前的问题,我实在不大清楚楼主想要完成的功能是什么?
    关于那个temp的问题,我的理解还是每个线程有自己的一个temp,至于说你输出的temp很混乱,那么是由于sleep的作用,因为每个线程睡眠的时间不一样。注意这个temp是个局部变量(作用域只是这个函数),对于每个线程来说,执行这个函数等于都自己申请了一个局部变量。