Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By pbrockway2

Thread: g.drawstring removal

  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default g.drawstring removal

    Hello I have this method:

    Java Code:
    public void paint(Graphics g) {
            try {
                g.drawString("Hello", 500, 250);
                Thread.sleep(2000);
                g.drawString("How are you doing?", 200, 100);
                
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    I want to remove "hello" from the window before displaying "How are you doing". g.dispose did not work if that is even what is does. Any help would be great.

    Thanks
    Last edited by jwl; 10-20-2012 at 01:34 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: g.drawstring removal

    Never call Thread.sleep(...) on the event thread and never ever have anything like that within the paint method. If this is a Swing GUI, then I recommend

    • draw in the paintComponent method, not the paint method.
    • call the super's paintComponent method as the first call of your paintComponent override.
    • Have the String being drawn be a String variable, a field of the class, say called drawString.
    • Set drawString = "Hello" to start with.
    • Use a non-repeating Swing Timer set with a delay of 2000 to set the drawString variable to "how are you doing" and then call repaint().
    pbrockway2 likes this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: g.drawstring removal

    An instance of Graphics behaves - in many ways - like a stream like System.out. You throw things at it like "Hello" and they get rendered onto the screen.

    It's much more complex than a print stream: it has all sorts of state like "foreground colour", "current font", "transparency" etc. So it might better be thought of as a graphics context.

    Also it pays to bear in mind that outputting using an instance of Graphics is more like creating an oil painting than it is like typing the keys on an old fashioned typewriter. If we examine a typewritten page we can see exactly what characters were typed and in what order (more or less). There is a one-to-one correspondence between the elements of the output and the elements of the input. An oil painting is quite different! We see what looks for all the world like a haystack with the sun setting in the background but, up close, there are only daubs on paint on the canvas which make no obvious sense and have no clear order.

    It's the same with drawString("hello"). What gets rendered are many pixels of specific colour and only in the user's mind is the word reconstructed.

    I want to remove "hello" from the window before displaying "How are you doing".
    Basically you must do what the artist would do: remove the unwanted marks of colour from the screen (say by drawing a filled rectangle of background colour) before rendering the pixels that make up the altered image you want to appear. (The Graphics and Graphics2D API docs describe methods for this.)

    ---

    Painting code must be fast. And you never sleep() the thread that is doing the painting as you did in your code.

    This is linked with an interesting difference between painting a string and printing it on the console. When you use System.out.println() it is your code that decides what to print and when to print it. This is not so with painting your gui. The user may minimise or maximise the gui, or some other window that was obscuring it may be closed. Your gui (or parts of it) may be be repainted at times that are simply not under the control of your code. In a gui you retain control over what is painted but you give up control over when it happens to the Swing (or similar) system that controls the coordination of painting.

    From time to time your paint() will be called, but think of your code as merely being loaned or "time sharing" the thread. Be a polite guest and don't sleep() a thread that belongs to whatever system is coordinating the rendering of the gui as a whole.

    ---

    This complexity of gui painting over regular printing to the console gives rise to firmly established patterns of usage that must be followed and which aren't necessarily obvious. I would recommend the article Painting in AWT and Swing and as much of the Creating a GUI With JFC/Swing trail in Oracle's Tutorial as is needed to make sense of that article.
    Fubarable likes this.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: g.drawstring removal

    Quote Originally Posted by self View Post
    In a gui you retain control over what is painted but you give up control over when it happens to the Swing (or similar) system that controls the coordination of painting.
    As Fubarable says, timers are the key when you want to schedule things. You don't wait 2s then paint, rather you acknowledge the authority of the Swing system and ask for something to, please, happen in 2s. Two seconds later your application is informed and you can perform the update.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,421
    Rep Power
    20

    Default Re: g.drawstring removal

    Quote Originally Posted by jwl View Post
    g.dispose did not work if that is even what is does.
    The method does what the API says it does. Have you read the API for Graphics#dispose()? If there's anything there you don't understand, we'll be glad to explain it to you.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Chart borders removal
    By susieferrari in forum JavaFX
    Replies: 1
    Last Post: 09-24-2012, 10:47 AM
  2. Replies: 5
    Last Post: 10-24-2011, 09:00 PM
  3. java removal
    By aizen92 in forum Forum Lobby
    Replies: 0
    Last Post: 08-22-2011, 11:13 AM
  4. help with drawString
    By h3nch in forum AWT / Swing
    Replies: 5
    Last Post: 01-16-2010, 03:58 PM
  5. Removal of Homework Requests
    By CaptainMorgan in forum Suggestions & Feedback
    Replies: 14
    Last Post: 08-03-2008, 10:21 PM

Posting Permissions

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