Results 1 to 5 of 5
Thread: Timer issues.
- 05-21-2011, 05:43 AM #1
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
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.
btw I didn't need to implement anything, I tried and it keep't complaining that it was already done in the AJFrame.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); } } }
- 05-21-2011, 06:03 AM #2
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.
- 05-21-2011, 06:39 AM #3
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.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.Java Code:// if (condition == true) { if (condition) { // if (condition == false) { if (!condition) {
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:
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.I want to be able to press any key then for it to repaint
db
- 05-21-2011, 06:51 AM #4
Member
- Join Date
- May 2011
- Posts
- 12
- Rep Power
- 0
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 :/
- 05-21-2011, 07:23 AM #5
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.I just find that cannot find symbol issue a bit strange.
As I said, go through the tutorials. Programming is much more fun when you have an idea what you're doing.
db
Similar Threads
-
Stopping a Timer from Inside the timer
By krishnan in forum Java AppletsReplies: 2Last Post: 10-04-2010, 11:15 PM -
Timer help
By Kinyo in forum New To JavaReplies: 15Last Post: 03-15-2009, 02:37 AM -
EJB Timer
By mrjunsy in forum Advanced JavaReplies: 0Last Post: 08-22-2008, 04:09 PM -
EJB Timer
By mrjunsy in forum New To JavaReplies: 0Last Post: 08-04-2008, 06:47 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks