Path: csiph.com!news.mixmin.net!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: Wed, 20 Jul 2016 10:02:43 -0700 Organization: A noiseless patient Spider Lines: 165 Message-ID: References: <87twfk93vu.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 20 Jul 2016 17:02:45 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="fc10d1b501433caa198830294d826923"; logging-data="23378"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rANIcoj8K0Bau7Ynn2RaW" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <87twfk93vu.fsf@Equus.decebal.nl> Cancel-Lock: sha1:CCcmScu7br2MdG+Jd5ee/C+ovew= Xref: csiph.com comp.lang.java.programmer:35010 On 7/20/2016 00:15, Cecil Westerhof wrote: > I need to crop some images and decide to write a Java program for it. > At the moment it is very rudimentary, I plan to add some > functionalities and because I have very little experience with GUI > comments on how to implement the GUI better are welcome. > > > At the moment it is build with my camera's in mind and cropping for > InstaGram, but when it is seen as useful I will make it more generic. > > > I have a few problems, the most important are: > - After loading the first or a new image the first click makes the > image disappear and painted again. Subsequent clicks do not have > this problem. We'd need to see the code > - The original photo can be rotated. How can I find this out and make > the cropped image also rotated? That's in the image meta data (I have no clue how to find it) > - How can I add comment to an image? You want to draw on the image or add meta data? > - After saving the cropped image I want to display it also. What would > be the best way to do this? In another window maybe > Some of the functionalities I want to add (other ideas are welcome): > - Working with different sets of cropping. > - Possibility to resize. (Select an area of 2.160x2.160 and crop it to > 1.080x1.080). > - Working with a database. > Here's a crop component that I wrote years ago, I don't think I ever used it for anything but I can't remember. 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 CropComponent extends JPanel { BufferedImage bi,subImage; boolean mouseDown; int startX,startY; int endX,endY; int x,y,w,h; public CropComponent(BufferedImage bi) { this.bi = bi; setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight())); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { mouseDown = true; startX = me.getX(); startY = me.getY(); } public void mouseReleased(MouseEvent me) { mouseDown = false; subImage = CropComponent.this.bi.getSubimage(x,y,w+1,h+1); 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(); x = y = w = h = 0; repaint(); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent me) { endX = Math.max(0,me.getX()); endX = Math.min(endX,CropComponent.this.bi.getWidth()-1); endY = Math.max(0,me.getY()); endY = Math.min(endY,CropComponent.this.bi.getHeight()-1); if (startX < endX && startY < endY) { x = startX; y = startY; w = endX-startX; h = endY-startY; } else if (startX > endX && startY > endY) { x = endX; y = endY; w = startX-endX; h = startY-endY; } else if (startX < endX && startY > endY) { x = startX; y = endY; w = endX-startX; h = startY-endY; } else if (startX > endX && startY < endY) { x = endX; y = startY; w = startX-endX; h = endY-startY; } 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(x,y,w,h); } } public static void main(final String[] args) { Runnable r = new Runnable() { public void run() { BufferedImage bi = null; try { 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); CropComponent cc = new CropComponent(bi); JScrollPane sp = new JScrollPane(cc); f.add(sp,BorderLayout.CENTER); f.pack(); f.setVisible(true); } catch (IOException ioe) { ioe.printStackTrace(); } } }; EventQueue.invokeLater(r); } } -- Knute Johnson -- Knute Johnson