Results 1 to 5 of 5

Thread: Timer issues.

  1. #1
    Grid_iso is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default Timer issues.

    So I want to be able to press any key then for it to repaint, and I think I need the timer to do so but its not working I get this issue "AJPanel.java:21: cannot find symbol
    symbol : constructor Timer(int,AJPanel)
    location: class javax.swing.Timer
    t = new Timer(30,this);"
    but I've seen so many people write timer the same way without this issue and I have created a "Timer" :/ so basically whats going on here and how would I go about pressing anyway to make a statement false? here the attempt code.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class AJPanel extends JPanel {
    	private Timer t;
    	private Background aBckGrnd;
    	private titleScreen screen;
    	private boolean showTitleScreen;
    
     	public AJPanel() {
    		setBackground(Color.white);
    		aBckGrnd = new Background();
    		screen = new titleScreen();
    		//addKeyListener(this);
    		showTitleScreen = true;
    		t = new Timer(30,this);
    	}
    
    	public void keyPressed(KeyEvent e) {
    		t.start();
    		showTitleScreen = false;
    		repaint();
    	}
    		
       	public void paintComponent(Graphics g) {
     	    super.paintComponent(g);
    		if(showTitleScreen = true) {
    			screen.draw(g);
    		}else {
    			aBckGrnd.draw(g);
    		}
    		
    	}
    }
    btw I didn't need to implement anything, I tried and it keep't complaining that it was already done in the AJFrame.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    What is the timer actually doing? Is there a reason you need the timer and cannot just skip that part of the equation?

    /Speculation.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Moved form New to Java.

    There are many things wrong with your code. You probably need to go through a tutorial:
    The Java™ Tutorials

    -- Have you read the API for the Timer class? What's the Type of the second parameter to the constructor? Is the current object of that Type?
    -- (Maybe should precede the previous point) Do you know the meaning of this, as a Java keyword?
    -- The conditional in the if statement within paintComponent(...) assigns true to showTitleScreen, so it will never enter the else block. You do know that = is an assignment and == is a comparison, or don't you? Also, it is never needed nor desirable to compare a boolean with true or false.
    Java Code:
    // if (condition == true) {
    if (condition) {
    
    // if (condition == false) {
    if (!condition) {
    Adding a key listener to a JPanel won't work 'off the shelf' since a JPanel is not by default focusable, and key events are transmitted to the focused component. That can be overcome by calling setFocusable(true), but has a shortcoming in that a JPanel doesn't provide visible feedback in respect of whether or not it is focused.

    Far better to use key bindings.
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    Like Dark, I fail to see where a Timer comes into the picture of your stated intent:
    I want to be able to press any key then for it to repaint
    Of course, with key bindings you would have to bind to every key on the keyboard, which may result in some ugly code. What are you actually trying to achieve? Describe the problem, not any approach you may have considered a possible solution.

    db

  4. #4
    Grid_iso is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    your right the code does have some issues and may not need a timer just yet. I just find that cannot find symbol issue a bit strange. I'm starting work on a game for a project and need to be able to have a game screen in which explains everything ("screen") which can with the touch of a button jump to the background of the game ("aBckGrnd"). I just thought that you might need the timer to activate repaint :/

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

    Default

    I just find that cannot find symbol issue a bit strange.
    I don't. The Timer constructor you use takes two parameters, an int and an ActionListener. That's not what you're supplying when attempting to invoke that constructor.

    As I said, go through the tutorials. Programming is much more fun when you have an idea what you're doing.

    db

Similar Threads

  1. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  2. Timer help
    By Kinyo in forum New To Java
    Replies: 15
    Last Post: 03-15-2009, 02:37 AM
  3. EJB Timer
    By mrjunsy in forum Advanced Java
    Replies: 0
    Last Post: 08-22-2008, 04:09 PM
  4. EJB Timer
    By mrjunsy in forum New To Java
    Replies: 0
    Last Post: 08-04-2008, 06:47 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Tags for this Thread

Posting Permissions

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