Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.gui Subject: Re: How do I paint on an existing Panel? Date: Wed, 18 Jan 2012 10:55:10 -0800 Organization: A noiseless patient Spider Lines: 24 Message-ID: References: <4f16f997$0$2957$fa0fcedb@news.zen.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 18 Jan 2012 18:55:13 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="4099"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VdG2xMmHio5zSswqfDMIW2DESz+hwKJg=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <4f16f997$0$2957$fa0fcedb@news.zen.co.uk> Cancel-Lock: sha1:fEIqWzO3/e45W9QCnfor4ojUxR8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.gui:4966 On 1/18/2012 8:55 AM, A B wrote: > I expect this is a really obvious question; I must get round to getting > a bigger Java book! Part of my program starts by displaying a JPanel, > with an image put on it using paintComponent(). How do I then draw some > shapes over the top of that image when a particular button is clicked? I > can't just put them in paintComponent(), because they haven't to appear > at first. Would it be easier in an applet, rather than an application? > > I'm sure this comes up in the example programs in the JDK package, but > for some mysterious reason they won't work. Wrong version of IE, > probably; have to look into that. > You just need to disconnect the button action from the drawing. Make a second image. Then copy the base image to this second image. Redo paintComponent method to display the second image. When the button is clicked, update the 2nd image with the new shape. Then, in the button action method, ask for a repaint of the panel (call repaint() on it). Now your panel will be updated with the new shape.