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 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: display text for 1 sec than remove txt java

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

    Default display text for 1 sec than remove txt java

    i have two animation going on screen.
    one animation is a rect going to right. i dont want this animation to stop
    2nd animation is message coming on screen for 1 sec and removeing it.

    right now 1st animation works fine but doesnt diplay. its be iam seting message="". but how can i display for 1 sec.

    public class ...
    ...
    int x = 0;
    int y = 0;
    int dx = 1;
    String message = "hellow world";
    ...
     
    public void start()
        {
            ...
            if(timer_class == null)
            {
                Timer timer_class = new Timer(10, this); 
            }
            else
            {
                timer_class.stop();
            }
            timer_class.start();                 
        }
     
     
     
    public void actionPerformed(ActionEvent e)
        {
           x+=dx; //make 1st animation
     
                message = "";
     
            repaint();
        }
     
     
        public void paint(Graphics g)
        {
              g.drawRect(x,y,10,10);   //i dont want this animation to stop
     
                  if(!message.equal(""))  //this should only come on screen for 1sec
                    {
            g.drawString(message, 600, 200);
            }
        }   
    }


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

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

    Use a Timer.
    Show message
    Start timer for desired time
    when timer calls its listener, clear message
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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

    1)Use a Timer. --check
    2)Show message --check
    3)Start timer for desired time --(not sure but i think its this)
    Timer timer_class = new Timer(10, this);
    4)when timer calls its listener, clear message
    can you plz explain this part lil more to me. right now i am clear message in actionperformed function.

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

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

    To clear the message, don't draw it when the paint() method is called.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    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 but isnt only way to display message on screen is by "g.drawString();" in paint method

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

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

    If you don't call the drawString() method the String won't be drawn.
    If you do call drawString() the String will be drawn (It will be visible if the color is different from the background color)
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    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

    so i should drawstring() in paint method and after 1 sec change color same as background?

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

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

    If you draw the same message two times, the first with a different color than the background and the second with the same color as the background, I'd think you'd see the results of the first drawing and nothing after the second one.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    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

    so what you are saying is that i shouldnt draw string in paint method. bc onces you draw in paint() than you cant undo.

    and i should use JLabel? and draw in init() function?

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

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

    you are saying is that i shouldnt draw string in paint method
    Not at all. I was only talking about code in the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    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

    ok let me try again:
    to display drawstring on screen for 1 sec than clearing it. i should do this: right?
    1)Use a Timer.
    2)Show message
    3)Start timer for desired time
    4)when timer calls its listener, clear message
    1 - i am using timer so check
    2 - i am drawing in paint() g.drawstring so check
    3 - i think this is Timer timer_class = new Timer(10, this);
    4 - this step i am not sure

    let me know which step iam wrong.

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

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

    Make a small simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    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

    but i told you the problem why would you need to see it?

    the problem is that i cant clear string after 1 sec. right now if i run this code. 1st animation works fine but 2nd animation i can print string on screen but i dont know how to clear it after 1 sec. as said in title and thread.

    than u said i need to:
    1)Use a Timer.
    2)Show message
    3)Start timer for desired time
    4)when timer calls its listener, clear message
    than i said:
    1 - i am using timer so check
    2 - i am drawing in paint() g.drawstring so check
    3 - i think this is Timer timer_class = new Timer(10, this);
    4 - this step i am not sure
    let me know which step iam wrong.
    and now you are saying that you dont understand the problem? but than why would tell me something if you dont understand the problem?

    iam sure you are trying to help but this is wild goose chase.

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

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

    If you want more help: Make a small simple program that compiles, executes and shows the problem.
    Talking about techniques isn't working. We need code for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    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

    ok. all i need help wth is to make string clear after 1 sec and no change to 1st rect animation.


    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";
     
    Timer timer_class;
     
     
    public void init()
    {
    	setSize(900, 500);
    }
     
     
    public void start()
        {
     
            if(timer_class == null)
            {
                 timer_class = new Timer(30, this); 
            }
            else
            {
                timer_class.stop();
            }
            timer_class.start();                 
        }
     
     
     
    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
     
              g.drawString(message, 600, 200);    //this should only come on screen for 1sec
     
        }   
    }

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

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

    Where are any of the techniques that were talked about? I don't see anything in this code that attempts to clear the message after some time.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    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

    ok. all i need help wth is to make string clear after 1 sec and no change to 1st rect animation.


    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";
     
    Timer timer_class;   //for 1st animation
    Timer timer_class2;  //for 2nd animation
     
    public void init()
    {
    	setSize(900, 500);
    }
     
     
    public void start()
        {
    	timer_class2 = new Timer(50, this);
     
            if(timer_class == null)
            {
                 timer_class = new Timer(30, this); 
            }
            else
            {
                timer_class.stop();
            }
            timer_class.start();                 
        }
     
     
     
    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
     
     
             timer_class2.start();
                	  g.drawString(message, 600, 200);
                	  //i dont know how to clear this
                	  timer_class2.stop(); 	  
        }   
    }

    i am thinking of making two timer. one for each. that way one animation will slow down 30 and other 50.

    also i dont know how to clear this string

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

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

    how to clear this string
    Would NOT drawing the String be the same as clearing the string? Control the drawing of the String with a if statement.

    There should NOT be code to start a thread every time the paint() method is called. Define and start a separate timer just like the first one except its listener will tell the paint() method NOT to draw() the String by setting a variable that the paint() method can test to see if the String should be drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    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

    i think iam started to get it now.

    i had a question on:
    except its listener will tell the paint() method NOT to draw() the String by setting a variable that the paint() method can test to see if the String should be drawn.

    i am having some difficult seeing how to clear it. les say if i doing testing in paint method and i draw it for 1 sec. but that will still stay there. only way i see to clear string is by change the color same as bg color.

    or i can do this in paint method:

    if(!message.equal(""))
    {
    g.drawString(message, 600, 200);
    }
    and to clear i can do message = "" ?

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

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

    Use the if statement to skip doing the draw to "clear" the message.
    The way to tell a method what to do could be by setting a boolean:
    boolean showMsg = true; // used to tell paint when to draw a String
    ..

    in timer:
    showMsg = false; // stop drawing String

    Test value of showMsg in paint() to control when to draw
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    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

    i can test if timer is running if its running than print message and stop timer. if timer is stop than print "" string?
     if(timer_class2.isRunning() == true) 
                {
                	 g.drawString(message, 600, 200);
                	 timer_class2.stop();
                }
              if (timer_class2.isRunning() == false)
              {
                     message="";
            	  g.drawString(message,600, 200);
              }

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

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

    A boolean is simpler.

    NOTE: The code does not need to test if true and then test if false
    if(condition) {
    // here when true
    }else{
    // here when false
    }
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    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

    ah i c so in paint method i can do something like this to test:

    if(showMsg == true)
    {
    g.drawString(message, 600, 200);
    }
    else
    g.drawString("", 600, 200);
    }
    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

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

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

    Drawing an empty String seems a waste of time.
    You don't need the == true with a boolean variable. The variable itself has a value of true or false.
    if(showMsg)
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    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

    i c. how about the 2nd part?

Page 1 of 2 12 LastLast

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