Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: What do you think of my CropImage class Date: Thu, 21 Jul 2016 08:30:59 -0700 Organization: A noiseless patient Spider Lines: 115 Message-ID: References: <87twfk93vu.fsf@Equus.decebal.nl> <8737n36ucx.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:31:01 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="fc10d1b501433caa198830294d826923"; logging-data="4927"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Ct8FMyjJeSZeYjOWyBnaM" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <8737n36ucx.fsf@Equus.decebal.nl> Cancel-Lock: sha1:/H/dAgtZOp3ZKb8drCd0pgzgwgI= Xref: csiph.com comp.lang.java.programmer:35047 I think Eric is right about why you are getting the flicker and other issues. That event dispatch thread is very important and mixing AWT and Swing components is usually problematic. I'm thinking you could have a menu of box sizes and select one to crop your image. Here is CropComponent rewritten and cleaned up a bit to use a fixed size box instead of the usual mouse dragged variable box. import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import java.net.*; import javax.imageio.*; import javax.swing.*; public class CropComponent2 extends JPanel { BufferedImage bi; boolean mouseDown; int mouseX,mouseY; public CropComponent2(BufferedImage bi) { this.bi = bi; setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight())); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { mouseDown = true; mouseX = me.getX(); mouseY = me.getY(); if (mouseX < 50) mouseX = 50; if (mouseX > bi.getWidth() - 50) mouseX = bi.getWidth() - 50; if (mouseY < 50) mouseY = 50; if (mouseY > bi.getHeight() - 50) mouseY = bi.getHeight() - 50; repaint(); } public void mouseReleased(MouseEvent me) { mouseDown = false; BufferedImage subImage = bi.getSubimage(mouseX-50,mouseY-50,100,100); 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(); repaint(); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); if (mouseX < 50) mouseX = 50; if (mouseX > bi.getWidth() - 50) mouseX = bi.getWidth() - 50; if (mouseY < 50) mouseY = 50; if (mouseY > bi.getHeight() - 50) mouseY = bi.getHeight() - 50; repaint(); } }); } public void paintComponent(Graphics g2D) { Graphics2D g = (Graphics2D)g2D; g.drawImage(bi,0,0,null); if (mouseDown) { g.setStroke(new BasicStroke(1.0f,BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND,1.0f,new float[] {2.0f,3.0f},1.0f)); g.setColor(Color.WHITE); g.drawRect(mouseX-50,mouseY-50,100,100); } } public static void main(final String[] args) { EventQueue.invokeLater(() -> { try { BufferedImage bi; try { bi = ImageIO.read(new URL(args[0])); } catch (MalformedURLException murle) { bi = ImageIO.read(new File(args[0])); } JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CropComponent2 cc = new CropComponent2(bi); JScrollPane sp = new JScrollPane(cc); f.add(sp,BorderLayout.CENTER); f.pack(); f.setVisible(true); } catch (IOException ioe) { ioe.printStackTrace(); } }); } } -- Knute Johnson