首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >图像在移动时会有奇怪的行为吗?

图像在移动时会有奇怪的行为吗?
EN

Stack Overflow用户
提问于 2018-06-03 14:02:45
回答 1查看 42关注 0票数 0

该程序使用一个矩形,其中可以使用ImageJLabel中捕获Ball

问题出在Timer类中,在该类中,计数器对时间进行计数。当它计算的时候,时间会显示在JLabel上。但是,本应捕获BallImage每次都会跳到起始位置,从而造成抖动。

我试着去掉JLabel,改用System.out.println(),效果也不错。

也许Swing Timer不喜欢调用JLabel,可能的原因是什么?

代码语言:javascript
复制
SimpleDateFormat df = new SimpleDateFormat("mm:ss:SSS");
timeL.setText("Time  :" + df.format(duration - clockTime));
//System.out.println("Time  :" + df.format(duration - clockTime));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 05:13:01

代码被修改了一点,它像预期的那样工作,我希望它是正确的。

现在,图像可以独立于球的速度移动,并且图像停留在移动的位置。

要添加图片,请在项目文件夹中添加一个名为"resources“的新文件夹。我用Netbeans 8.2对其进行了测试。

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ImageMoveTest implements ActionListener {
 JPanel panel;
 Image rimg;
 Thread t;
 int x;
 int y;
 int v;
 int z;
 int n;

 public static void main(String[] args) throws InterruptedException,  IOException {
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
   try {
       new ImageMoveTest().startApp();
   } catch (InterruptedException ex) {
       Logger.getLogger(ImageMoveTest.class.getName()).log(Level.SEVERE,  null, ex);
   } catch (IOException ex) {
       Logger.getLogger(ImageMoveTest.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  });
 } 

 public void startApp() throws InterruptedException, IOException {
  panel = new DrawPanel();
  InputStream inStream =  this.getClass().getClassLoader().getResourceAsStream("i6.jpeg");
  BufferedImage img = ImageIO.read(inStream);
  rimg = img.getScaledInstance(150, 150, Image.SCALE_SMOOTH);
  MoveMouse mm = new MoveMouse();
  panel.addMouseMotionListener((MouseMotionListener) mm);

  JFrame f = new JFrame();
  f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  f.getContentPane().add(panel).setBackground(Color.white);
  f.setSize(750, 750);
  f.setResizable(false);
  f.setVisible(true);
  f.setLocationRelativeTo(null);
  MathC mc = new MathC();
  t = new Thread(mc);
  t.start();
 }

 class MoveMouse extends JPanel implements MouseMotionListener {
  @Override
  public void mouseDragged(MouseEvent e) {
   v=e.getX();  
   z=e.getY();
   this.repaint();
  }

  @Override
  public void mouseMoved(MouseEvent e) {

  }
 } 

 class MathC implements Runnable {
  @Override
  public void run() {
   while(true) {
    // x,y here
    x = (int) (Math.random() * panel.getWidth());
    y = (int) (Math.random() * panel.getHeight());
    panel.repaint();
     try {
       t.sleep(700);
     } catch (InterruptedException ex) {
       Logger.getLogger(ImageMoveTest.class.getName()).log(Level.SEVERE,  null, ex);
      }
     } 
    } 
   }
   @Override
   public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To  change body of generated methods, choose Tools | Templates.
   }

   class DrawPanel extends JPanel {
    public void update(Graphics g) {
     super.paintChildren(g);
    }   

    public void paintChildren( Graphics g) {
     super.paintChildren(g);
     drawIt(g);
     // repaint the backround to see the single circle moving
     // draw the Ball
     g.setColor(Color.red);
     g.fillOval(x, y, 35, 35);
     g.drawOval(x, y, 35, 35);
     this.repaint();
    }
    public void drawIt(Graphics g) {
     super.paintChildren(g);
     // draw Rectangle
     g.drawRect(0,0,this.getWidth(),this.getHeight());     
     // draw the Image
     g.drawImage(rimg, v, z, this);
     this.repaint();
    }
   }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50663710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档