0

The Program use a rectangle in which a Ball can be caught from a JLabel with an Image.

The problem is in the Timer class, in which a counter counts, the time. While it counts the time is shown on a JLabel. But the Image that should catch the Ball, jumps every time to the start position, creating a tremble.

I tried to remove the JLabel and use instead System.out.println(), and it worked as well.

Perhaps the Swing Timer doesn't like calls for JLabel, what could be the cause?

SimpleDateFormat df = new SimpleDateFormat("mm:ss:SSS");
timeL.setText("Time  :" + df.format(duration - clockTime));
//System.out.println("Time  :" + df.format(duration - clockTime));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
loadP
  • 404
  • 1
  • 4
  • 15
  • You really, really, need to embrace the date/time APIs available since Java 8. See [Java format hour and min](https://stackoverflow.com/questions/50419165/java-format-hour-and-min/50419562#50419562) for one way to format a `Duration` – MadProgrammer Jun 03 '18 at 06:08
  • 1
    Oh, you're probably fighting the layout manager - really wouldn't use a `JLabel` for this. Simply use a image and possibly a `Rectangle` to make hit detection easier – MadProgrammer Jun 03 '18 at 06:11
  • Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Jun 03 '18 at 06:12
  • Yes that's good to use this API instead. But in this Line long secs = duration.minusMinutes(mins).toSeconds(); I get the Error Message: toSeconds() has private access in Duration, incompatible Types BigDecimal can not be converted to long. I don't know well why. I Use Netbeans 8.2 and java/javac 9 – loadP Jun 03 '18 at 16:37
  • The Image moves now, but it's a bit slow. – loadP Jun 04 '18 at 06:13

1 Answers1

0

The Code was modified a bit, it works as expected, I hope it's correct.

Now the Image may be moved around independently of the Ball's Speed, and the Image stays at the moved position.

To add the Picture, add in the Project Folder a new Folder, named "resources". I tested it with Netbeans 8.2 .

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();
    }
   }
  }
loadP
  • 404
  • 1
  • 4
  • 15