0

What I'm trying to do is simple. I have a JLayeredPane with two panels inside of it. Panel 2 (higher Z index) has a transparent background and only displays a Line2D element that goes from Y = 0 to Y = max. I need the X value to increment every so many milliseconds, and then redraw the line. I have everything set up to do so, except I can't figure out how to do the bar movement via timing.

I've done some research and every time I saw mentions of the timer class (Which I feel would be able to accomplish what I'm trying to do) people recommend not using it. I can't figure out an alternative to using the timer class in order to slide my bar across the screen.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1870954
  • 207
  • 3
  • 14
  • 1
    Your question is a bit off since you say "*people* recommend not using it", but provide no backup for this statement. Who is recommending not to use the Timer and what is their rationale? I would say try doing this with a Timer. If it doesn't work, come on back with your code. – Hovercraft Full Of Eels Apr 22 '13 at 21:36
  • 1
    Are you referring to `java.util.Timer` or `javax.swing.Timer` (e.g. [1](http://stackoverflow.com/a/9772978/418556), [2](http://stackoverflow.com/a/16040330/418556))? The latter is fine, the former requires a little extra work to ensure that GUI updates are done on the EDT. Please link to the best example of this information you refer to. Your question is currently confusing. – Andrew Thompson Apr 23 '13 at 02:00
  • Thanks for the comments guys, I'll look into swing's timer and come back once I have some code. I tried to recreate the searches that recommended against using the timer, but I wasn't able to find it. I'll come back with links if I get them! – user1870954 Apr 23 '13 at 14:44

1 Answers1

0

Hope this code helps you. It does exactly what you want.

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Arrays;
import java.awt.EventQueue;
import javax.swing.JFrame;

public class FloorPlaner extends JFrame {
     public int x=0;

     public FloorPlaner(){ 
          super("FloorPlaner");

          requestFocus(); 

          setContentPane(new DrawingPane());

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

          setSize(400, 400);

          setResizable(true); 

          setVisible(true); 

          while (true) { 
              x++;
              repaint();
              try {
              Thread.sleep(40); //25 FPS
              } catch(InterruptedException bug) {
              Thread.currentThread().interrupt();
              System.out.println(bug);
              }
          }
     }


     class DrawingPane extends JPanel { //Where you actually draw on
        public void paintComponent(Graphics g) { //Drawing method
           g.drawLine(x,0,x,400);
        }   
     }
     public static void main(String args[]) {
            new FloorPlaner(); //Start it
     }
}
Luatic
  • 8,513
  • 2
  • 13
  • 34