Results 1 to 5 of 5
Thread: g.drawstring removal
- 10-20-2012, 12:15 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
g.drawstring removal
Hello I have this method:
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.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(); } }
ThanksLast edited by jwl; 10-20-2012 at 12:34 AM.
-
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().
- 10-20-2012, 01:35 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
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.)I want to remove "hello" from the window before displaying "How are you doing".
---
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.
- 10-20-2012, 01:42 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: g.drawstring removal
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.
- 10-20-2012, 04:04 AM #5
Similar Threads
-
Chart borders removal
By susieferrari in forum JavaFXReplies: 1Last Post: 09-24-2012, 09:47 AM -
How can I click on an image from drawstring()?
By rippon in forum Java 2DReplies: 5Last Post: 10-24-2011, 08:00 PM -
java removal
By aizen92 in forum Forum LobbyReplies: 0Last Post: 08-22-2011, 10:13 AM -
help with drawString
By h3nch in forum AWT / SwingReplies: 5Last Post: 01-16-2010, 02:58 PM -
Removal of Homework Requests
By CaptainMorgan in forum Suggestions & FeedbackReplies: 14Last Post: 08-03-2008, 09:21 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks