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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:10:49+00:00 2026-06-14T19:10:49+00:00

I have an SWT Canvas with some image on it. I want my user

  • 0

I have an SWT Canvas with some image on it. I want my user to select parts of the image by clicking, holding the left mouse button and opening a selection box by moving the mouse. As a visual feedback I want to show the selected rectangle as a transparent overlay over my image.

The problem is I have not clue how to best achieve this? Can someone give me a hint?

  • 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-14T19:10:52+00:00Added an answer on June 14, 2026 at 7:10 pm

    My answer is somehow similar to the one I gave here:

    SWTChart mouse event handling

    The code below should give you an idea on where to start. It basically remembers the location of a MouseDown event. Each time the mouse is then dragged, a rectangle is drawn from the starting location to the current location. Once MouseUp happens, everything stops.

    public class DrawExample
    {
        private static boolean drag = false;
        private static int startX;
        private static int startY;
        private static int endX;
        private static int endY;
    
        public static void main(String[] args)
        {
            Display display = new Display();
            Shell shell = new Shell(display);
            shell.setText("Drawing Example");
    
            final Canvas canvas = new Canvas(shell, SWT.NONE);
            canvas.setSize(150, 150);
            canvas.setLocation(20, 20);
            shell.open();
            shell.setSize(200, 220);
    
            canvas.addListener(SWT.MouseDown, new Listener() {
    
                @Override
                public void handleEvent(Event event) {
                    startX = event.x;
                    startY = event.y;
    
                    drag = true;
                }
            });
    
            canvas.addListener(SWT.MouseUp, new Listener() {
    
                @Override
                public void handleEvent(Event event) {
                    endX = event.x;
                    endY = event.y;
    
                    drag = false;
    
                    canvas.redraw();
                }
            });
    
            canvas.addListener(SWT.MouseMove, new Listener() {
    
                @Override
                public void handleEvent(Event event) {
                    if(drag)
                    {
                        endX = event.x;
                        endY = event.y;
    
                        canvas.redraw();
                    }
                }
            });
    
            canvas.addListener(SWT.Paint, new Listener() {
    
                @Override
                public void handleEvent(Event event) {
                    if(drag)
                    {
                        GC gc = event.gc;
    
                        gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
                        gc.setAlpha(128);
    
                        int minX = Math.min(startX, endX);
                        int minY = Math.min(startY, endY);
    
                        int maxX = Math.max(startX, endX);
                        int maxY = Math.max(startY, endY);
    
                        int width = maxX - minX;
                        int height = maxY - minY;
    
                        gc.fillRectangle(minX, minY, width, height);
                    }
                }
            });
    
            while (!shell.isDisposed())
            {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            display.dispose();
        }
    }
    

    It looks like this:

    enter image description here

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

Sidebar

Related Questions

I have a strange case with SWT and Button after using setEnabled() - seems
I have a Java SWT GUI with a multiline Text control. I want to
Inside an SWT Table I have many TableItems. I want to move one of
I have an eclipse plugin project which uses some swt objects, eg - import
I doing some 2D drawing in a SWT GUI. For that I have a
I have populated swt Table and it displays it OK, but now I want
I have a Print Button in my SWT TitleAreaDialog. viewPDFButton.addSelectionListener(new SelectionAdapter() { public void
What is the proper way to dispose of SWT Shells? I have created some
I have a Tree Table in SWT. I want to show a view when
I want to have the same distribution of SWT-based application for Windows 32 and

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.