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

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

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();
        x.setLayout(new BorderLayout());







    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        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);
                BufferedImage im = ImageIO.read(f);

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

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); 
        setContentPane(new AfficheImage(s)); 
        getContentPane().setLayout(new BorderLayout()); 


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

        Image im;
        public AfficheImage(String s){

            im = getToolkit().getImage(s); 

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

             Graphics2D g2d = (Graphics2D)g;

        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));


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


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


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


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


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


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


        private void initComponents() {



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 :)