Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: What is my Timer constructor complaining about ?

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

    Default What is my Timer constructor complaining about ?

    Java Code:
    package myPackage;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Timer;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class SimpleGui1B
    {
    	
    	JButton button,button2,button3;
    	JFrame frame;
    	MyDrawPanel myPanel;
    	
    	int x;
    	int y;
    	
    
    	
    	public void go()
    	{
    		Timer timer = new Timer(1000, new MyTimerActionListener());
    
    		myPanel = new MyDrawPanel();
    		
    		frame = new JFrame();
    		button = new JButton("click me");
    		button2 = new JButton("click me");
    		button3 = new JButton("click me");
    		
    		frame.add(myPanel);
    		
    		button.addActionListener(new MyTimerActionListener());
    		button2.addActionListener(new MyTimerActionListener());
    		button3.addActionListener(new MyTimerActionListener());
    		
    		myPanel.add(button);
    		myPanel.add(button2);
    		myPanel.add(button3);
    
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setSize(300,300);
    
    		frame.setVisible(true);
    	}
    	
    	public static void main(String[] args)
    	{
    		SimpleGui1B gui = new SimpleGui1B();
    		gui.go();
    	}
    	
    
    
    
    	class MyTimerActionListener implements ActionListener 
    	{
    		public void actionPerformed(ActionEvent e) 
    		  {
    	
    	
    			for(int i = 0; i<100; i++)
    			{
    				x++;
    				y++;
    				System.out.println(i);
    				myPanel.repaint();
    				frame.repaint();
    			}
    			
    			JButton temp = (JButton) e.getSource();
    			temp.setText("CLICKED");
    			temp.setEnabled(false);
    	
    		  }
    	}
    	
    	class MyDrawPanel extends JPanel
    	{
    		public void paintComponent(Graphics g)
    		{
    			g.setColor(Color.green);
    			g.fillOval(x, y, 40, 40);
    			
    			System.out.println("REPAINTING THE COMPONENT");
    		}
    	}
    }
    For the line that has

    Timer timer = new Timer(1000,new MyTimerActionListener());

    I get:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The constructor Timer(int, SimpleGui1B.MyTimerActionListener) is undefined

    at myPackage.SimpleGui1B.go(SimpleGui1B.java:34)
    at myPackage.SimpleGui1B.main(SimpleGui1B.java:29)

    What is wrong with this, can anyone help ?

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

    Default Re: What is my Timer constructor complaining about ?

    The Java core classes have two Timer classes ,and you are using the wrong one, a util Timer:

    import java.util.Timer;

    Instead you want to use a Swing Timer:

    import javax.swing.Timer;

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is my Timer constructor complaining about ?

    Wrong Timer; you want the javax.swing.Timer, not the java.util.Timer.

    kind regards,

    Jos

    edit: way too slow, but it's Sunday so it doesn't count ;-)
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: What is my Timer constructor complaining about ?

    Thank you..

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: What is my Timer constructor complaining about ?

    Quote Originally Posted by JosAH View Post
    edit: way too slow, but it's Sunday so it doesn't count ;-)
    Some things never change

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is my Timer constructor complaining about ?

    Quote Originally Posted by DarrylBurke View Post
    Some things never change
    It was Fubarable's fault; honest; I didn't do anything so I'm innocent.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: What is my Timer constructor complaining about ?

    You can't blame me. I live in a Grolsch-free zone (much to my lament).

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is my Timer constructor complaining about ?

    Quote Originally Posted by Fubarable View Post
    You can't blame me. I live in a Grolsch-free zone (much to my lament).
    What did you do to be punished like that?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  2. Problems with Timer Constructor
    By Yids in forum AWT / Swing
    Replies: 3
    Last Post: 03-11-2011, 05:35 AM
  3. Problems with Timer Constructor
    By Yids in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 04:56 PM
  4. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 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
  •