What is the second part?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
What is the second part?
If you don't understand my answer, don't ignore it, ask a question.
in timer:
showMsg = false; // stop drawing String
2nd part is to change showMeg to false after 1 sec. right?
i tried puting (showMeg = false) in actionPerformed, paint method in if statment, in start method after timer_class2.start(). but it just print in else statment
If showMsg is true the message should be drawn
If showMsg is false the message should not be drawn
showMsg will stay true until the one sec timer's listener method sets it false
You'll have to post the code so I can see what it does.
If you don't understand my answer, don't ignore it, ask a question.
import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Timer; import javax.swing.JApplet; public class main extends JApplet implements ActionListener { int x = 30; int y = 30; int dx = 1; String message = "hellow world"; boolean showMsg = true; Timer timer_class; Timer timer_class2; public void init() { setSize(900, 500); } public void start() { if(timer_class == null) { timer_class = new Timer(30, this); timer_class2 = new Timer(30, this); } else { timer_class.stop(); timer_class2.stop(); } timer_class.start(); timer_class2.start(); showMsg = false; } public void actionPerformed(ActionEvent e) { x+=dx; //make 1st animation repaint(); } public void paint(Graphics g) { super.paint(g); g.drawRect(x,y,10,10); //i dont want this animation to stop if(showMsg) //this should only come on screen for 1sec { g.drawString(message, 600, 200); } else { } } }
this is what i have so far. right now it doesnt print any thing bc i am setting showMsg to false. i am now sure how can i tell timer_class2 to change showMsg value to false.
i am thinking may be there is some function that get timer_class2 time?
The second timer MUST have its own action listener method. It should not share a listener.how can i tell timer_class2 to change showMsg value to false.
In its listener showMsg can be set false.
If you don't understand my answer, don't ignore it, ask a question.
like this? still no change
import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Timer; import javax.swing.JApplet; public class main extends JApplet implements ActionListener { int x = 30; int y = 30; int dx = 1; String message = "hellow world"; boolean showMsg = true; Timer timer_class; Timer timer_class2; public void init() { setSize(900, 500); } public void start() { if(timer_class == null) { timer_class = new Timer(30, this); timer_class2 = new Timer(30, this); } else { timer_class.stop(); timer_class2.stop(); } timer_class.start(); timer_class2.start(); } public void actionPerformed(ActionEvent e) { x+=dx; //make 1st animation repaint(); } public void actionPerformed2(ActionEvent e) { showMsg = false; repaint(); } public void paint(Graphics g) { super.paint(g); g.drawRect(x,y,10,10); //i dont want this animation to stop if(showMsg) //this should only come on screen for 1sec { g.drawString(message, 600, 200); } else { } } }
Where is the separate listener for the second timer? You can't makeup names for listener methods. This method is not used for anything:public void actionPerformed2(ActionEvent e)
You should add println() statements to methods to be sure a method is being called.
Try defining an inner class for the needed listener.
If you don't understand my answer, don't ignore it, ask a question.
o so something like this?
import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Timer; import javax.swing.JApplet; public class main extends JApplet { int x = 30; int y = 30; int dx = 1; String message = "hellow world"; boolean showMsg = true; Timer timer_class; Timer timer_class2; public void init() { setSize(900, 500); } public void start() { if(timer_class == null) { timer_class = new Timer(30, this); timer_class2 = new Timer(30, this); } else { timer_class.stop(); timer_class2.stop(); } timer_class.start(); timer_class2.start(); } public class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { { x+=dx; //make 1st animation repaint(); } } public class MyActionListener2 implements ActionListener { public void actionPerformed2(ActionEvent event) { showMsg = false; }} public void paint(Graphics g) { super.paint(g); g.drawRect(x,y,10,10); //i dont want this animation to stop if(showMsg) //this should only come on screen for 1sec { g.drawString(message, 600, 200); } else { } } }
The code needs to create instances of the classes and use them in the Timer constructors.
If you don't understand my answer, don't ignore it, ask a question.
thanks for trying but what you saying is not working for me.
if any one else looking for this solution try this,
When you set the text, set a counter to 100. In your timer's actionPerformed dcrement the counter. When it gets to zero reset the text.
its simple and works great.
Which part don't you understand?
create instances of the ActionListener classes:
For example: AClass ac = new AClass();
Use those instances in the calls to the Timer constructor
... new Timer(1234, ac);
If you don't understand my answer, don't ignore it, ask a question.