Hello everybody

I would like to load an image from my documents and display it in a pannel and select a part of my image a rectangle for example and save it, I saw your response Ezzaral it's very interesting but the problem is that I can not integrate it with what I have.

Please I need your help

Recommended Answers

All 5 Replies

Please post the code you are having problems with.

Here is the main window where I select my image from a JFileChooser it works well and below is the class where I display my image handpicked and I can draw a rectangle selected, but I want when I selected it, it draws only the part selected as in paint and then I have to save the selected image part I'm stuck here please help me :s

@SuppressWarnings("unused")
public class MainFenetre extends JFrame implements ActionListener,MouseListener,MouseMotionListener{
JMenuBar menuBar = new JMenuBar();    // MENUBAR

    JMenu test1 = new JMenu("Fichier");
    JMenu test2 = new JMenu("Edition");

    JMenuItem item1 = new JMenuItem("Ouvrir une image");
    JMenuItem item2 = new JMenuItem("Modifier une image");
    JPanel p=new JPanel();

    private BufferedImage img;
    private int x1, y1, x2, y2;

    public MainFenetre(){

        this.setTitle("Afficher Image");
        Container x=this.getContentPane();
        this.setBounds(300,100,600,400);
        x.setLayout(new BorderLayout());
        x.add(p);

        this.menuBar.add(test1);
        this.menuBar.add(test2);

        this.test1.add(item1);
        this.test2.add(item2);

        this.setJMenuBar(menuBar);

        item1.addActionListener(this);
        item2.addActionListener(this);

        this.addMouseListener(this);
        this.addMouseMotionListener(this);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==item1){
        JFileChooser chooser = new JFileChooser();//création dun nouveau Jfilechosser
        chooser.setApproveButtonText("Choix du fichier..."); //intitulé du bouton
        chooser.showOpenDialog(null); //affiche la boite de dialogue

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){

            File f= chooser.getSelectedFile();
            try {
                String completeFileName =  f.getAbsolutePath();
                ImageFrame g= new ImageFrame(completeFileName);
                g.setVisible(true);
                BufferedImage im = ImageIO.read(f);


            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    }





import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class ImageFrame extends JFrame {

    public ImageFrame(String s){

        setSize(1280, 1024); 
        setTitle("Test"); 
        setContentPane(new AfficheImage(s)); 
        getContentPane().setLayout(new BorderLayout()); 
        this.setVisible(true);


    }

    class AfficheImage extends JPanel implements MouseListener,MouseMotionListener{
        Rectangle selection;
        Point anchor;

        Image im;
        public AfficheImage(String s){

            im = getToolkit().getImage(s); 
             addMouseListener(this);
             addMouseMotionListener(this);
        }

        public void paintComponent(Graphics g) 
        { 
        super.paintComponent(g); 
        g.drawImage(im, 0, 0, getWidth(), getHeight(), this); 
         if (selection!=null){

             Graphics2D g2d = (Graphics2D)g;
             g2d.draw(selection);
         }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            selection.setBounds( (int)Math.min(anchor.x,e.getX()), (int)Math.min(anchor.y,e.getY()),
                    (int)Math.abs(e.getX()-anchor.x), (int)Math.abs(e.getY()-anchor.y));
            repaint();

        }

        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
             anchor = e.getPoint();
             selection = new Rectangle(anchor);

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
            selection = null;
            repaint();

        } 

        private void initComponents() {
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            pack();
        }

    }



}

please help me i need to select just a portion of my image dinamically using mouse as in paint its work but i cant save the rectangle (by clicking on it)

You have code to track the Rectangle defined by the mouse drag operation. I haven't checked that, but you also clear that Rectangle when the mouse is released, so then you are left with nothing.
When the mouse is released, instead of clearing the variable, you can use that selection Rectangle's coordinates in a call to BufferedImage's getSubimage method to create a new Image with just that part that was in the Rectangle.

Thanks James i will try it hope that works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.