Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在线程中放置两个不同的任务_Java_Multithreading_Swing_Paint - Fatal编程技术网

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

Java 如何在线程中放置两个不同的任务,java,multithreading,swing,paint,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

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

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();
   }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
类MyImage扩展JFrame实现ActionListener
{
静态int xPixel=20;
图像我的图像,屏幕外图像;
影印;
JPanel p=新的JPanel();
按钮btn=新按钮(“bun”);
JFrame f=新的JFrame();
公共MyImage()
{
myImage=Toolkit.getDefaultToolkit().getImage(“mywineshoplogo.jpg”);
setExtendedState(JFrame.MAXIMIZED_两者);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
加(p);
p、 添加(btn);
moveImage();
btn.addActionListener(此);
}
公共空间更新(图g)
{
油漆(g);
}
公共空间涂料(图g)
{
int width=getWidth();
int height=getHeight();
if(offScreenImage==null)
{
offScreenImage=createImage(宽度、高度);
offScreenGraphics=offScreenImage.getGraphics();
}
//清除屏幕外的图像
offScreenGraphics.clearRect(0,0,宽度+1,高度+1);
//把你的图像从屏幕上画下来
offScreenGraphics.drawImage(myImage,xPixel,10,this);
//把你的图像从屏幕上画下来
//显示屏幕外的图像
g、 drawImage(屏幕外图像,0,0,this);
//显示屏幕外的图像
}
void moveImage()//从左向右移动
{
线程hilo=新线程(){
公开募捐{
试一试{
对于(int i=0;i<530;i++)
{
xPixel+=1;
重新油漆();
//然后为你的动画睡一会儿
尝试
{
睡眠(4);
}/*这将暂停50毫秒*/
捕捉(中断异常e)
{
System.err.println(“睡眠异常”);
}
}
}//试试看
捕获(例外情况除外){
//做点什么。。。
}
}
};
hilo.start();
}
/*void moveimg()//从右向左移动
{
对于(int i=529;i>0;i--)
{
如果(i==1)
{
moveImage();
}
xPixel-=1;
重新油漆();
//然后为你的动画睡一会儿
尝试
{
睡眠(40);
}//这将暂停50毫秒
捕捉(中断异常e)
{
System.err.println(“睡眠异常”);
}
} 
} */     
已执行的公共无效行动(行动事件ae)
{
尝试
{
如果(ae.getSource()==btn)
{
p、 挫折地面(颜色:红色);
}
}
捕获(例外e)
{
System.out.println(“错误”);
}
}
公共静态void main(字符串参数[])
{
MyImage me=新的MyImage();
}
}

每当你准备编写
线程时,在你的GUI代码中睡眠
,停止自己并引入一个在Swing的
计时器上安排的任务。这正是您的代码所需要的:将每个更新作为一个单独的计划任务安排在
计时器上
Timer
非常简单且易于使用,请参见示例。

您能否尝试在
paint
方法中注释掉
clearRect
?当然,图像会被涂抹在屏幕上,但至少你可以验证背景在最长4毫秒内不会变红,然后会被绘画方法覆盖…屏幕外图像的目的是什么?如果您的目标是双缓冲,这是无用的,因为Swing已经在内部为您完成了这项工作。顺便说一句,您不应该扩展
JFrame
,而应该扩展
JPanel
,覆盖
paintCOmponent
,而不是
paint
,并将该面板设置为JFrame的内容窗格。另外,请始终调用所有
paintXXX
方法的super方法,否则您将看到视觉故障