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 6385367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:53:26+00:00 2026-05-25T02:53:26+00:00

I want to Zoom specific area on image. which is selected by the user.

  • 0

I want to Zoom specific area on image. which is selected by the user. image display on canvas using Swing. i already done full image zoom on canvas but can’t implement specific area zoom.
please help

  • 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-05-25T02:53:26+00:00Added an answer on May 25, 2026 at 2:53 am

    Canvas is awt not swing. Try JPanel instead. Example.

    But if you already done full image zoom, try to make the same on a sub-image.

    BufferedImage.getSubimage(x, y, w, h);
    

    EDIT

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class Example {
    
        private Point startPoint = new Point(0, 0);
        private Point rectLocale = new Point();
        private Dimension rectSize = new Dimension();
        private int zoom = 80;
        private BufferedImage capture = null;
        private BufferedImage raw;
    
        public Example() throws Exception {
            raw = new Robot().createScreenCapture(new Rectangle(
                    Toolkit.getDefaultToolkit().getScreenSize()));
            MouseBehavior behavior = new MouseBehavior();
            JPanel b = new JPanel() {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Dimension getMinimumSize() {
                    return new Dimension(500, 500);
                }
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(500, 500);
                }
    
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    Graphics2D g2d = ((Graphics2D) g);
                    g2d.drawImage(raw, 0, 0, null);
                    if (capture != null) {
                        int width2 = (int) (rectSize.width + rectSize.width * (zoom / 500d));
                        int height2 = (int) (rectSize.height + rectSize.height * (zoom / 500d));
                        int x2 = rectLocale.x - ((width2 - rectSize.width) / 2);
                        int y2 = rectLocale.y - ((height2 - rectSize.height) / 2);
                        Image scaledInstance = capture.getScaledInstance(
                                width2, height2, Image.SCALE_AREA_AVERAGING);
                        g2d.drawImage(scaledInstance, x2, y2, null);
                        g2d.drawRect(x2, y2, width2, height2);
                    } else {
                        g2d.draw(new Rectangle(rectLocale, rectSize));
                    }
                }
            };
            b.addMouseMotionListener(behavior);
            b.addMouseListener(behavior);
            b.addMouseWheelListener(behavior);
            JFrame f = new JFrame();
            f.setLocation(10, 10);
            f.setDefaultCloseOperation(3);
            f.add(b);
            f.pack();
            f.setVisible(true);
        }
    
        private class MouseBehavior extends MouseAdapter {
    
            @Override
            public void mousePressed(MouseEvent e) {
                startPoint = e.getPoint();
                rectLocale = new Point();
                rectSize = new Dimension();
                capture = null;
                if (e.getSource() instanceof JComponent) {
                    ((JComponent) e.getSource()).repaint();
                }
            }
    
            @Override
            public void mouseDragged(MouseEvent e) {
                Point currentPoint = e.getPoint();
                rectSize.width = Math.abs(currentPoint.x - startPoint.x);
                rectSize.height = Math.abs(currentPoint.y - startPoint.y);
                if (e.isShiftDown()) {
                    rectSize.width = rectSize.height = Math.min(
                            rectSize.width, rectSize.height);
                    int dx = startPoint.x - rectSize.width;
                    int dy = startPoint.y - rectSize.height;
                    rectLocale.x = startPoint.x < currentPoint.x ? startPoint.x
                            : Math.max(dx, dy);
                    rectLocale.y = startPoint.y < currentPoint.y ? startPoint.y
                            : Math.min(dx, dy);
                } else {
                    rectLocale.x = Math.min(currentPoint.x, startPoint.x);
                    rectLocale.y = Math.min(currentPoint.y, startPoint.y);
                }
                if (e.getSource() instanceof JComponent) {
                    ((JComponent) e.getSource()).repaint();
                }
            }
    
            @Override
            public void mouseReleased(MouseEvent e) {
                if (rectSize.width <= 0 || rectSize.height <= 0) {
                    capture = null;
                } else {
                    capture = raw.getSubimage(Math.max(0, rectLocale.x),
                            Math.max(0, rectLocale.y), rectSize.width, rectSize.height);
                }
                if (e.getSource() instanceof JComponent) {
                    ((JComponent) e.getSource()).repaint();
                }
            }
    
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                zoom = Math.min(2000, Math.max(0, zoom + e.getUnitsToScroll() * 10));
                if (e.getSource() instanceof JComponent) {
                    ((JComponent) e.getSource()).repaint();
                }
            }
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        Example example = new Example();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to zoom that area (50x50 area around it) of image where user
I am using ZedGraph and I want to zoom to a selected area by
I want to add functionality for the zoom in/out image that display by the
I want to zoom image in blackberry torch.For this i am using ZoomScreen predefined
I am using RotateAnimation for image. But I also want zoom on image with
I have an ximage which I want to zoom in on, and display. I'm
I want to zoom in and zoom out an Image in WP7,C# without using
I want to zoom an image in a scroll view. I used the below
I want to give zoom effect to the iPhone camera image while capturing the
want to have a Hyperlink-Button in a gridView in which I can display a

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.