Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: What do you think of my CropImage class Date: Thu, 21 Jul 2016 11:13:16 -0400 Organization: A noiseless patient Spider Lines: 36 Message-ID: References: <87twfk93vu.fsf@Equus.decebal.nl> <87vazz71ct.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 21 Jul 2016 15:13:29 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="22e5ab429c5a4bb738666b9c52e40d69"; logging-data="988"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nxGMTXZ7RwL6ENjzu1TxA" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: Cancel-Lock: sha1:iaTS00x2zbtrSbC6ThVLEa/yT2Y= Xref: csiph.com comp.lang.java.programmer:35046 On 7/21/2016 10:58 AM, Knute Johnson wrote: > On 7/21/2016 03:05, Cecil Westerhof wrote: >> >>> Runnable r = new Runnable() { >>> public void run() { >>> try { >>> ImageIO.write(subImage,"JPEG",new >>> File("crop.jpg")); >>> } catch (IOException ioe) { >>> ioe.printStackTrace(); >>> } >>> } >>> }; >>> new Thread(r).start(); >> >> Why are you doing this in a separate thread? >> > > So if you have a large image that takes a second or two to write it > won't block the Event Dispatch Thread and mess up the mouse movement. A slightly slicker way to do this is with a SwingWorker, the advantage being that it can inform the GUI when the save completes. You could then display a JDialog if there was a problem, or arrange to display a busy cursor while the save runs and restore the original cursor afterward, or ... SwingWorker is, however, a bit more complex than a "fire and forget" Thread, so CW might choose to stick with the simpler alternative for now and revisit the fancier one later on. http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM