Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Is this appropriate usage of Timer?

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Is this appropriate usage of Timer?

    What I am trying to do is ( just for purposes of learning java ):

    I want to have a Frame with a button on it and a green circle at top. When I click the button the green circle should leave a tail and move towards center. This is my code:


    Java Code:
    package myPackage;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    
    public class LearningTimer 
    {
    	JFrame frame;
    	Timer timer;
    	JButton button;
    	JPanel panel;
    	
    	int x;
    	int y;
    	
    	public static void main(String[] args)
    	{
    		LearningTimer object = new LearningTimer();
    		object.buildFrame();
    	}
    	
    	public void buildFrame()
    	{
    		frame = new JFrame("Learning Timer");
    		frame.setSize(300,300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		button = new JButton("Click me!");
    		button.addActionListener(new MyButtonActionListener());
    		
    		panel = new MyDrawPanel();
    		
    		frame.add(panel);
    		panel.add(button);
    		
    		frame.setVisible(true);
    	}
    	
    	class MyDrawPanel extends JPanel
    	{
    		public void paintComponent(Graphics g)
    		{
    			g.setColor(Color.green);
    			g.fillOval(x++, y++, 40, 40);
    			
    			if(x>50)
    			{
    				timer.stop();
    			}			
    		}
    	}
    	
    	class MyButtonActionListener implements ActionListener
    	{
    		@Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			timer = new Timer(50,new MyTimerActionListener());
    			timer.start();
    		}
    	}
    	
    	class MyTimerActionListener implements ActionListener
    	{
    		@Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			panel.repaint();
    		}
    	}
    
    }
    And the output:

    Is this appropriate usage of Timer?-paintcomponent.png

    So it works ok, as I expect.

    My question is:

    I am doing the right thing, but am i doing the right way ?

    Is this appropriate usage of Timer ?

    Can I handle this situation with only one Listener class and less code ? Advices ?

    Thank you!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is this appropriate usage of Timer?

    See comments in your code, in particular those prefaced with // !!

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class LearningTimer {
       public static final int X_MAX = 100; //!!
       JFrame frame;
       Timer timer;
       JButton button;
       JPanel panel;
    
       int x;
       int y;
    
       public static void main(String[] args) {
          LearningTimer object = new LearningTimer();
          object.buildFrame();
       }
    
       public void buildFrame() {
          frame = new JFrame("Learning Timer");
          frame.setSize(300, 300);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          button = new JButton("Click me!");
          button.addActionListener(new MyButtonActionListener());
    
          panel = new MyDrawPanel();
    
          frame.add(panel);
          panel.add(button);
    
          frame.setVisible(true);
       }
    
       class MyDrawPanel extends JPanel {
          public void paintComponent(Graphics g) {
             super.paintComponent(g); // !! don't forget the super method
             // !! so that your JPanel can refresh the background
    
             g.setColor(Color.green);
             // !! don't put program logic inside of paint or paintComponent
             // g.fillOval(x++, y++, 40, 40);
    
             g.fillOval(x, y, 40, 40);
    
             // !! don't put program logic inside of paint or paintComponent
             // if (x > 50) {
             // timer.stop();
             // }
          }
       }
    
       class MyButtonActionListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             timer = new Timer(50, new MyTimerActionListener());
             timer.start();
          }
       }
    
       class MyTimerActionListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             // !! program logic goes here
             if (x > X_MAX) {
                timer.stop();
             } else {
                x++;
                y++;
                panel.repaint();
             }
          }
       }
    
    }
    camickr likes this.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Is this appropriate usage of Timer?

    Thank you Fubarable.

    So I need two listener classes right ?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is this appropriate usage of Timer?

    Quote Originally Posted by fatabass View Post
    Thank you Fubarable.

    So I need two listener classes right ?
    You're welcome, and yes, you will need one ActionListener for your JButton and one for your Swing Timer.

Similar Threads

  1. CPU usage without JNI
    By Eranga in forum Advanced Java
    Replies: 2
    Last Post: 12-13-2011, 03:25 AM
  2. Timer And Timer Task
    By Ashish_jain in forum New To Java
    Replies: 3
    Last Post: 06-22-2011, 10:41 AM
  3. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-05-2010, 12:15 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  5. Timer usage in java program
    By sandeeprao.techno in forum New To Java
    Replies: 5
    Last Post: 01-24-2008, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •