Results 1 to 2 of 2
  1. #1
    shaungoater is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default Selecting parts of an image

    Hi i have had a look on the forum and have found no topics that answer my problem. If this is not so then my apologies. I would like to know if there are methods available (or another way) so that it is possible to read in an image and then select a part of it so that a new image,of the selected part, can be created and passed on to other methods/classes.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class Subimages extends JPanel {
        Rectangle clip = new Rectangle(50,50,150,150);
        Point origin = new Point();
        BufferedImage image;
    
        public Subimages(BufferedImage image) {
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            origin.x = (getWidth() - image.getWidth())/2;
            origin.y = (getHeight() - image.getHeight())/2;
            g.drawImage(image, origin.x, origin.y, this);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.red);
            g2.draw(clip);
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(image.getWidth(), image.getHeight());
        }
    
        private void createSubimage() {
            // Wait for gui to settle down.
            new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch(InterruptedException e) {
                        System.out.println("wait interrupted");
                    }
                    int x = clip.x - origin.x;
                    int y = clip.y - origin.y;
                    int w = clip.width;
                    int h = clip.height;
                    BufferedImage subimage = image.getSubimage(x, y, w, h);
                    BufferedImage copy = GraphicsEnvironment.getLocalGraphicsEnvironment().
                                         getDefaultScreenDevice().
                                         getDefaultConfiguration().
                                         createCompatibleImage(w, h);
                    System.out.println("copy type = " + copy.getType());
                    Graphics2D g2 = copy.createGraphics();
                    g2.drawImage(subimage, 0, 0, null);
                    g2.dispose();
                    JOptionPane.showMessageDialog(null, new ImageIcon(copy), "", -1);
                }
            }).start();
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/bison.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            Subimages test = new Subimages(image);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new JScrollPane(test));
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.createSubimage();
        }
    }

Similar Threads

  1. JFileChooser example (selecting a directory)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-03-2009, 01:25 PM
  2. Replies: 2
    Last Post: 02-18-2008, 04:24 AM
  3. Replies: 3
    Last Post: 01-05-2008, 11:13 PM
  4. selecting a record in database
    By ramachandran in forum New To Java
    Replies: 0
    Last Post: 10-25-2007, 07:06 AM
  5. Jtree - making parts editable
    By kmarie in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2007, 02:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •