Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:26:00+00:00 2026-06-05T22:26:00+00:00

I’m making a screenshot program so when you press the screenshot button, a JMessageDial

  • 0

I’m making a screenshot program so when you press the screenshot button, a JMessageDial pops up and prompts you with your image, it the tells you to drag your mouse and make a box around the area that you want to take a screenshot of. I can’t seem to find how to get the image inside the box when the user clicks okay.

So what I need help with is getting the image inside of the rectangle the user “draws” with his mouse, and store that in a variable.

Here is the specific code:

public void selectArea(final BufferedImage screen) throws Exception {
    final BufferedImage screenCopy = new BufferedImage(screen.getWidth(), screen.getHeight(), screen.getType());
    final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
    JScrollPane screenScroll = new JScrollPane(screenLabel);

    screenScroll.setPreferredSize(new Dimension((int)(screen.getWidth()/2), (int)(screen.getHeight()/2)));

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(screenScroll, BorderLayout.CENTER);

    final JLabel selectionLabel = new JLabel("Draw a rectangle");
    panel.add(selectionLabel, BorderLayout.SOUTH);
    repaint(screen, screenCopy);
    screenLabel.repaint();

    screenLabel.addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseMoved(MouseEvent me) {
            start = me.getPoint();
            repaint(screen, screenCopy);
            selectionLabel.setText("Start Point: " + start);
            screenLabel.repaint();
        }

        @Override
        public void mouseDragged(MouseEvent me) {
            end = me.getPoint();
            captureRect = new Rectangle(start, new Dimension(end.x-start.x, end.y-start.y));
            try {
                paintFinalImage(new BufferedImage((int)screenCopy.getGraphics().getClipBounds(captureRect).getWidth()+1,
                        (int)screenCopy.getGraphics().getClipBounds(captureRect).getHeight()+1, screen.getType()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            repaint(screen, screenCopy);
            screenLabel.repaint();
            selectionLabel.setText("Rectangle: " + captureRect);
        }
    });
    JOptionPane.showMessageDialog(null, panel, "Select your image", JOptionPane.OK_OPTION, new ImageIcon(""));
    uploadImage(screenShot);
    System.out.println("Final rectangle: " + screenCopy.getGraphics().getClipBounds(captureRect));
}

And this is the full class if you need it:

package com.screencapture;


import java.awt.*;

public class Main implements ActionListener, ItemListener, KeyListener, NativeKeyListener {

    private final Image icon = Toolkit.getDefaultToolkit().getImage("./data/icon.gif");
    private final TrayIcon trayIcon = new TrayIcon(icon, "Screen Snapper");
    private JFrame frame;
    private Rectangle captureRect;
    private JComboBox<String> imageType;
    private boolean hideWhenMinimized = false;
    private final String API_KEY = "b84e430b4a65d16a6955358141f21a61";
    private static Robot robot;
    private BufferedImage screenShot;
    private Point end;
    private Point start;

    public static void main(String[] args) throws Exception {
        robot = new Robot();
        new Main().start();
    }

    public void start() throws Exception {
        GlobalScreen.getInstance().registerNativeHook();
        GlobalScreen.getInstance().addNativeKeyListener(this);
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        frame = new JFrame("White power!");
        frame.setPreferredSize(new Dimension(250, 250));
        frame.getContentPane().setLayout(null);
        frame.setIconImage(icon);
        frame.addKeyListener(this);
        frame.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e) {}

            @Override
            public void windowClosing(WindowEvent e) {}

            @Override
            public void windowClosed(WindowEvent e) {}

            @Override
            public void windowIconified(WindowEvent e) {
                if (hideWhenMinimized)
                    frame.setVisible(false);
            }

            @Override
            public void windowDeiconified(WindowEvent e) {}

            @Override
            public void windowActivated(WindowEvent e) {}

            @Override
            public void windowDeactivated(WindowEvent e)  {}

        });

        JLabel lblImageType = new JLabel("Image Format:");
        lblImageType.setBounds(10, 11, 90, 14);
        frame.getContentPane().add(lblImageType);

        String[] imageTypes = {"PNG", "JPG"};
        imageType = new JComboBox<String>(imageTypes);
        imageType.setBounds(106, 8, 51, 20);
        frame.getContentPane().add(imageType);

        JButton btnTakeScreenShot = new JButton("ScreenShot");
        btnTakeScreenShot.setBounds(24, 69, 115, 23);
        frame.getContentPane().add(btnTakeScreenShot);
        btnTakeScreenShot.addActionListener(this);

        setTrayIcon();
        frame.pack();
        frame.setVisible(true);
    }

    public void setTrayIcon() throws AWTException {
        CheckboxMenuItem startup = new CheckboxMenuItem("Run on start-up");
        CheckboxMenuItem hide = new CheckboxMenuItem("Hide when minimized");
        String[] popupMenuOptions = {"Open", "Exit"};
        if (SystemTray.isSupported()) {
            SystemTray tray = SystemTray.getSystemTray();
            trayIcon.setImageAutoSize(true);
            PopupMenu popupMenu = new PopupMenu();
            for (String option : popupMenuOptions) {
                if (option == "Exit") {
                    popupMenu.add(startup);
                    popupMenu.add(hide);
                    popupMenu.add(option);
                } else {
                    popupMenu.add(option);
                }
            }
            trayIcon.setPopupMenu(popupMenu);
            popupMenu.addActionListener(this);
            startup.addItemListener(this);
            hide.addItemListener(this);
            trayIcon.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    frame.setVisible(true);
                }

            });
            tray.add(trayIcon);
        }
    }

    public void selectArea(final BufferedImage screen) throws Exception {
        final BufferedImage screenCopy = new BufferedImage(screen.getWidth(), screen.getHeight(), screen.getType());
        final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
        JScrollPane screenScroll = new JScrollPane(screenLabel);

        screenScroll.setPreferredSize(new Dimension((int)(screen.getWidth()/2), (int)(screen.getHeight()/2)));

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(screenScroll, BorderLayout.CENTER);

        final JLabel selectionLabel = new JLabel("Draw a rectangle");
        panel.add(selectionLabel, BorderLayout.SOUTH);
        repaint(screen, screenCopy);
        screenLabel.repaint();

        screenLabel.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseMoved(MouseEvent me) {
                start = me.getPoint();
                repaint(screen, screenCopy);
                selectionLabel.setText("Start Point: " + start);
                screenLabel.repaint();
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                end = me.getPoint();
                captureRect = new Rectangle(start, new Dimension(end.x-start.x, end.y-start.y));
                try {
                    paintFinalImage(new BufferedImage((int)screenCopy.getGraphics().getClipBounds(captureRect).getWidth()+1,
                            (int)screenCopy.getGraphics().getClipBounds(captureRect).getHeight()+1, screen.getType()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                repaint(screen, screenCopy);
                screenLabel.repaint();
                selectionLabel.setText("Rectangle: " + captureRect);
            }
        });
        JOptionPane.showMessageDialog(null, panel, "Select your image", JOptionPane.OK_OPTION, new ImageIcon(""));
        uploadImage(screenShot);
        System.out.println("Final rectangle: " + screenCopy.getGraphics().getClipBounds(captureRect));
    }

    public void paintFinalImage(BufferedImage image) throws Exception {
        screenShot = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
        Graphics2D g = screenShot.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
    }

    public void repaint(BufferedImage orig, BufferedImage copy) {
        Graphics2D g = copy.createGraphics();
        g.drawImage(orig, 0, 0, null);
        if (captureRect != null) {
            g.setColor(Color.BLACK);
            g.draw(captureRect);
        }
        g.dispose();
    }

    public void uploadImage(BufferedImage image) throws Exception {
        String IMGUR_POST_URI = "http://api.imgur.com/2/upload.xml";
        String IMGUR_API_KEY = API_KEY;
        String readLine = null;
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, imageType.getSelectedItem().toString(), outputStream);
            URL url = new URL(IMGUR_POST_URI);

            String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(outputStream.toByteArray()).toString(), "UTF-8");
            data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(IMGUR_API_KEY, "UTF-8");

            URLConnection urlConnection = url.openConnection();
            urlConnection.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
            wr.write(data);
            wr.flush();
            InputStream inputStream;
            if (((HttpURLConnection) urlConnection).getResponseCode() == 400) {
                inputStream = ((HttpURLConnection) urlConnection).getErrorStream();
            } else {
                inputStream = urlConnection.getInputStream();
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                readLine = line;
            }
            wr.close();
            reader.close();
        } catch(Exception e){
            e.printStackTrace();
        }
        String URL = readLine.substring(readLine.indexOf("<original>") + 10, readLine.indexOf("</original>"));
        System.out.println(URL);
        Toolkit.getDefaultToolkit().beep();
        StringSelection stringSelection = new StringSelection(URL);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        System.out.println(command);
        if (command.equalsIgnoreCase("open")) {
            frame.setVisible(true);
        } else if (command.equalsIgnoreCase("exit")) {
            System.exit(-1);
        } else if (command.equalsIgnoreCase("screenshot")) {
            try {
                final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                final BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
                selectArea(screen);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        System.out.println(e.getItem() + ", " +e.getStateChange());
        String itemChanged = e.getItem().toString();
        if (itemChanged.equalsIgnoreCase("run on start-up")) {
            //TODO
        } else if (itemChanged.equalsIgnoreCase("hide when minimized")) {
            if (e.getStateChange() == ItemEvent.DESELECTED) {
                hideWhenMinimized = false;
            } else if (e.getStateChange() == ItemEvent.SELECTED) {
                hideWhenMinimized = true;
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println("KeyTyped: "+e.getKeyCode());
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
        System.out.println("KeyReleased: "+e.getKeyCode());
        System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
        if (e.getKeyCode() == 154) {
            try {
                final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                final BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
                selectArea(screen);
                frame.setVisible(true);
            } catch(Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:26:02+00:00Added an answer on June 5, 2026 at 10:26 pm

    I recommend that you simplify your problem to solve it. Create a small compilable and runnable program that doesn’t have all the baggage of your current code, but whose only goal is to display an image, and let the user select a sub-portion of that image. Then if your attempt fails, you can post your attempt here, and we can actually both run it, and understand it, and hopefully then be able to fix it.

    For example, here’s a small bit of compilable and runnable code that uses a MouseListener to select a small section of an image. Perhaps you can get some ideas from it:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ImagePlay extends JPanel {
       public static final String IMAGE_PATH = "http://upload.wikimedia.org/wikipedia/" +
            "commons/thumb/3/39/European_Common_Frog_Rana_temporaria.jpg/" +
            "800px-European_Common_Frog_Rana_temporaria.jpg";
       private static final Color RECT_COLOR = new Color(180, 180, 255);
       private BufferedImage img = null;
       Point p1 = null;
       Point p2 = null;
    
       public ImagePlay() {
          URL imgUrl;
          try {
             imgUrl = new URL(IMAGE_PATH);
             img = ImageIO.read(imgUrl );
             ImageIcon icon = new ImageIcon(img);
             JLabel label = new JLabel(icon) {
                @Override
                protected void paintComponent(Graphics g) {
                   super.paintComponent(g);
                   myLabelPaint(g);
                }
             };
    
             setLayout(new BorderLayout());
             add(new JScrollPane(label));
    
             MouseAdapter mAdapter = new MyMouseAdapter();
             label.addMouseListener(mAdapter);
             label.addMouseMotionListener(mAdapter);
    
          } catch (MalformedURLException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    
       private void myLabelPaint(Graphics g) {
          if (p1 != null && p2 != null) {
             int x = Math.min(p1.x, p2.x);
             int y = Math.min(p1.y, p2.y);
             int width = Math.abs(p1.x - p2.x);
             int height = Math.abs(p1.y - p2.y);
             g.setXORMode(Color.DARK_GRAY);
             g.setColor(RECT_COLOR);
             g.drawRect(x, y, width, height);
          }
       }
    
       private class MyMouseAdapter extends MouseAdapter {
    
          @Override
          public void mousePressed(MouseEvent e) {
             p1 = e.getPoint();
             p2 = null;
             repaint();
          }
    
          @Override
          public void mouseDragged(MouseEvent e) {
             p2 = e.getPoint();
             repaint();
          }
    
          @Override
          public void mouseReleased(MouseEvent e) {
             p2 = e.getPoint();
             repaint();
    
             int x = Math.min(p1.x, p2.x);
             int y = Math.min(p1.y, p2.y);
             int width = Math.abs(p1.x - p2.x);
             int height = Math.abs(p1.y - p2.y);
             BufferedImage smlImg = img.getSubimage(x, y, width, height);
             ImageIcon icon = new ImageIcon(smlImg);
             JLabel label = new JLabel(icon);
             JOptionPane.showMessageDialog(ImagePlay.this, label, "Selected Image", 
                   JOptionPane.PLAIN_MESSAGE);         
          }
       }
    
    
       private static void createAndShowGui() {
          ImagePlay mainPanel = new ImagePlay();
    
          JFrame frame = new JFrame("ImagePlay");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    It uses a publicly available image obtained online, a MouseListener that’s added to a JLabel, and the JLabel has its paintComponent(...) method overridden so as to show the guide lines from the MouseListener/Adapter. It creates the sub image via BufferedImage’s getSubimage(...) method, and then it displays it in a JOptionPane, but it would be trivial to save the image if desired.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.