0

i have this code. in this code an image is moving left to right with moveImage method and moves right to left with moveimg method in the code. what i want now is to work a button event. there is a button in code, i want when i click the button it should do its job. but it's not doing.. here the code is:

    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
   {
      for (int i = 0; i < 530; i++)
      {

         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");
         }
      }
   }

/*   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();
   }
}
4

1 回答 1

0

您应该将动画移植到它们自己的线程中。这将阻止您的主线程由于其他进程而锁定。您正在调用Thread.sleep(40);主线程。这将始终影响您的唯一线程的吞吐量。

一个解法

因此,您应该创建一个新类,例如ImageMover. 此类可以在其构造函数中使用 Image 对象,并具有moveImage移动图像的方法,而不是您的解决方案moveImgand moveImage(非常令人困惑的东西)。

其次,你想要ImageMover扩展一个Thread. 这将迫使您编写一个run方法,如下所示:

public void run() {
    moveImage();
}

当你想让你的图像开始移动时,你应该调用ImageMover.start(). 不是 run

这会将方法放在不同的线程上,并应确保 GUI 的功能。

于 2013-04-11T09:35:59.230 回答