Path: csiph.com!1.us.feeder.erje.net!feeder.erje.net!2.eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: What do you think of my CropImage class Date: Thu, 21 Jul 2016 18:07:36 +0200 Organization: Decebal Computing Lines: 85 Message-ID: <87lh0v560n.fsf@Equus.decebal.nl> References: <87twfk93vu.fsf@Equus.decebal.nl> <8737n36ucx.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="12baf5125b19322b899c79c2e3878015"; logging-data="14123"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19+QTultF7c8m6/DV3FQijNznkRj8m2CbY=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:lXENHpNM6syo2nmCM6BfDYf+T9I= sha1:TCYSqpoxcjjaHC9WWmZX7Su/O6Y= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.java.programmer:35053 On Thursday 21 Jul 2016 15:35 CEST, Eric Sosman wrote: > On 7/21/2016 5:58 AM, Cecil Westerhof wrote: >> I was not completely clear :'-( so here some more info. >> >> I have no real experience with Swing and had an itch to scratch. >> That is why I wrote this program: to get some experience with >> Swing and get a little tool to make my live a little easier. >> [...] > > I have not studied your code closely, but one thing jumps out: > You're doing Swingthings on the wrong thread. Almost all of Swing > is thread-unsafe, and almost all manipulation of Swing objects > must occur on the Event Dispatching Thread, or EDT. Your code > breaches this requirement: > > - It constructs a pair of JLabels on whatever thread loads and > initializes the class. (And the JLabels are static, which is so > very odd as to be downright weird. Have you thought this through?) Not really no: I just wanted to know if I could solve the problem of cropping. ;-) But about the things being static. There is always exactly one, so what is the problem with things being static? If it is a real no-no I could change it. > - It constructs a bunch of Swing components on whatever thread > executes main(). Furthermore, it manipulates those components > (setting bounds, etc.), and even makes the JFrame visible -- all > while running on the same wrong thread. > > Don't Do That. See > > http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html I will look into it. I wanted to learn, now I have to. :-D > ... for a discussion of the issues, and how to deal with them. (I > rather suspect that the "flicker" you report may have its origins > in, er, wrong-threadedness.) No, it was that I used a Panel instead of a JPanel. I copied that part from somewhere of the internet. I found it a little bit strange that a Panel was used instead of a JPanel, but I thought “What do I know”? I changed it to JPanel and that problem went away. I had to add: super.paintComponent(g); to the paint function (otherwise the old one stays ‘below’ the new one and that is a problem when the new picture is smaller as the old one), but that is a small price to pay. :-D Next time I should listen to that little voice. > A few other random remarks: > > - You mention that "for me pack did not work," which is sort of > weird. I don't know what you tried before you settled on absolute > positioning, but I wonder: Could your layout troubles have anything > to do with the fact that CropImage extends Panel instead of JPanel? I am going to try this out. > - Maybe not, but then: What is the purpose of the DispImage class? > What does it accomplish that couldn't just as well be done in the > containing CropImage class (if the latter were a JPanel)? I created the DispImage class to display the cropped image. And I am going to use it to save it also. It is better to verify it before saving it. > - There's a handy MouseAdapter class that can eliminate some > verbosity in your MouseListener implementation. And something else I have to look into. I am happy to do, because I found it much to verbose, but I did not know a better way. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof