如何在线程中放置两个不同的任务

时间:2013-05-10 11:11:45

标签: java multithreading swing paint

我有以下代码。在这段代码中,我有一个从左到右移动的图像和一个有事件的按钮。但我想把这两个任务都放在线程中。这样它才能正常工作。这段代码的问题是按钮事件在到达最右边点之前不起作用。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyImage extends JFrame implements ActionListener
{
   static int xPixel = 20;
   Image myImage, offScreenImage;
   Graphics offScreenGraphics;
   JPanel p = new JPanel();
   Button btn = new Button("bun");
   JFrame f = new JFrame();


   public MyImage()
   {
      myImage = Toolkit.getDefaultToolkit().getImage("mywineshoplogo.jpg");
      setExtendedState(JFrame.MAXIMIZED_BOTH);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      add(p);
      p.add(btn);
      moveImage();
      btn.addActionListener(this);
   }

   public void update(Graphics g)
   {
      paint(g);
   }

   public void paint(Graphics g)
   {
      int width = getWidth();
      int height = getHeight();
      if (offScreenImage == null)
      {
         offScreenImage = createImage(width, height);
         offScreenGraphics = offScreenImage.getGraphics();
      }
// clear the off screen image  
      offScreenGraphics.clearRect(0, 0, width + 1, height + 1);
// draw your image off screen  
      offScreenGraphics.drawImage(myImage, xPixel, 10, this);
// draw your image off screen  
// show the off screen image  
      g.drawImage(offScreenImage, 0, 0, this);
// show the off screen image  
   }

   void moveImage()   //left to right move
   {
  Thread hilo = new Thread() {

  public void run() {
 try {

      for (int i = 0; i < 530; i++)
      {

         xPixel += 1;
         repaint();
        // then sleep for a bit for your animation  
         try
         {
            Thread.sleep(4);
         } /* this will pause for 50 milliseconds */

         catch (InterruptedException e)
         {
            System.err.println("sleep exception");
         }
      }
  } //try
  catch (Exception ex) {
      // do something...
  }
  }
  };
  hilo.start();
   }

/*   void moveimg()   // right to left move
   {
      for (int i = 529; i > 0; i--)
      {
         if (i == 1)
         {
            moveImage();
         }
         xPixel -= 1;
         repaint();
// then sleep for a bit for your animation  
         try
         {
            Thread.sleep(40);
         } // this will pause for 50 milliseconds 

         catch (InterruptedException e)
         {
            System.err.println("sleep exception");
          }
      } 
   } */     

   public void actionPerformed(ActionEvent ae)
   {
      try
      {
         if (ae.getSource() == btn)
         {
            p.setBackground(Color.RED);
         }
      }
      catch (Exception e)
      {
         System.out.println("error");
      }
   }

   public static void main(String args[])
   {
      MyImage me = new MyImage();
   }
}

1 个答案:

答案 0 :(得分:2)

每当您要在GUI代码中编写Thread.sleep时,请停下来并介绍在Swing Timer上安排的任务。这正是您的代码所需要的:将每个更新安排为Timer上的单独计划任务。 Timer非常简单易用,请参阅this official Oracle tutorial