一个用多线程画图的程序,我弄了好久都没有弄出来,搞的一点信心都没有了。希望高手们帮忙啊!!
源程序如下:
import java.applet.*;
import java.awt.*;
import java.lang.Thread;/**
 * @author ****
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class Paint extends Applet implements Runnable 
{      
       
    class Left extends Thread
    {
        Graphics g1 = getGraphics();
        
        
        public void paintOval(Graphics g)
        {
            g.drawOval(70, 70, 10, 10);
        }
        
        public void run()
{
           
   paintOval(g1);
}
} class Right extends Thread
{
    Graphics g2 = getGraphics();
    
    public void paintRect(Graphics g)
    {
        g.drawRect(150, 70, 10, 10);
    } 
    
    
public void run()
{
    paintRect(g2);
            
}
} public void run()
{
          }
    
public void start()
{
    Left t1 = new Left();
Right t2 = new Right();
t1.start();
t2.start();
}}

解决方案 »

  1.   

    下面是我改写的程序 在eclipse下运行没有反应:
    public class Paint extends Applet implements Runnable 
    {      
        Graphics g1 = getGraphics();  
        Left t1 = new Left();
    Right t2 = new Right();

        class Left extends Thread
        {
            public void run()
    {
         paint(g1);
                
    }
        }
        class Right extends Thread
        {
            public void run()
    {
        paint(g1);
                
    }
        }
        
        public void paint(Graphics g)
        {
         if(Thread.currentThread() == t1)
         {
         g.drawOval(70, 70, 10, 10);
         }
         else
             if(Thread.currentThread() == t2)
             {
              g.drawRect(150, 70, 10, 10);
             }
        }
        
        public void run()
        {
             
        
        }
        
        public void start()
        {
        
        
         t1.start();
         t2.start();
        }
    }
      

  2.   

    // <applet code=Paint.class width=500 height=400></applet>
    import java.applet.*;
    import java.awt.*;
    import java.lang.Thread;public class Paint extends Applet implements Runnable 
    {      
        Graphics g1;// = getGraphics();  此处更改到start方法中,否则会出现空指针引用异常
        Left t1 = new Left();
    Right t2 = new Right();// 更改run方法使之重复画,因为一旦当前线程为main线程的时,paint将输出空白
        class Left extends Thread
        {
            public void run()
    {
    try {
          while(true){
          paint(g1);
          sleep(100);
          }
          }catch(InterruptedException e){
         
          e.printStackTrace();
          }
                
    }
        }
    // 同上
        class Right extends Thread
        {
            public void run() 
    {
    try {
    while(true){
         paint(g1);
         sleep(100);
         }   
         }catch(InterruptedException e)
         {
         e.printStackTrace();
         }
                
    }
        }
        
        public void paint(Graphics g)
        {
    // 当前线程是t1时,画椭圆
         if(Thread.currentThread() == t1)
         {
         g.drawOval(70, 70, 100, 100);
         }
    // 否则当前线程是t2时,画矩形
         else
             if(Thread.currentThread() == t2)
             {
              g.drawRect(150, 70, 200, 210);
             }
    // 否则什么都不画,也就是线程为main线程时
        }
        
        public void run()
        {
             
        
        }
        
        public void start()
        {
        
         g1 = getGraphics();
         t1.start();
         t2.start();
        }
    }
      

  3.   

    谢谢您 yuzl123
    现在感觉java博大精深啊 !