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.

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36

Thread: display text for 1 sec than remove txt java

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: display text for 1 sec than remove txt java

    What is the second part?
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: display text for 1 sec than remove txt java

    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

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: display text for 1 sec than remove txt java

    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.

  4. #29
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: display text for 1 sec than remove txt java

    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?

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: display text for 1 sec than remove txt java

    how can i tell timer_class2 to change showMsg value to false.
    The second timer MUST have its own action listener method. It should not share a listener.
    In its listener showMsg can be set false.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: display text for 1 sec than remove txt java

    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
              {
              }
     
        }
    }

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: display text for 1 sec than remove txt java

    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.

  8. #33
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: display text for 1 sec than remove txt java

    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
              { }
     
        }
    }

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: display text for 1 sec than remove txt java

    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.

  10. #35
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: display text for 1 sec than remove txt java

    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.

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: display text for 1 sec than remove txt java

    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.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Remove JTable row that read txt file records
    By sajjad4563 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 30th, 2012, 02:22 PM
  2. How to remove any BOM from UTF-8 encoded text files
    By efluvio in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 14th, 2012, 01:45 PM
  3. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  4. Array month/day/hour/min/sec incomplete java code
    By mightyking in forum Collections and Generics
    Replies: 12
    Last Post: September 18th, 2011, 08:46 PM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM