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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:12:15+00:00 2026-05-31T05:12:15+00:00

I would like to create a selection area tool. This tool should allow to

  • 0

I would like to create a “selection area” tool.
This tool should allow to draw a rectangle area on the screen with the mouse.

I am using a fullscreen, translucent, darken swt Shell as my background on which I draw a white rectangle to represent the selected area.

My problem is that I did not find an efficient way to refresh the rectangle area.
Until now I’ve used the redraw method but the visual effect is quite ugly, even thought I tried to redraw only he needed area :

    public ManualScreenAreaSelector(final Display display) {
        shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
        shell.setBounds(display.getClientArea());
        // shell.setFullScreen(true);
        shell.setAlpha(180);
        shell.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
    }

    @Override
    public void mouseMove(final MouseEvent e) {
        if (editionMode) {
            // retrieve the rectangular area corresponding to mouse selection
            final Rectangle r = makeRectangleFromSelection(clickCoordinates, new Point(e.x, e.y));
            // make the ugly 'tint' effect
            shell.redraw();

            GC gc = new GC(shell);
            gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
            gc.setAlpha(150);

            gc.fillRectangle(r.x, r.y, r.width, r.height);

            gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
            gc.fillRectangle(0, 0, r.x, r.y);
            gc.fillRectangle(0, 1080 - r.y, r.x, 1080 - r.y);
            gc.dispose();

            lastX = e.x;
            lastY = e.y;
        }
    }

    @Override
    public void mouseDown(final MouseEvent e) {
        // Right click = reset selection
        if (e.button == 3) {
            shell.redraw();
            selectedArea = null;
            if (editionMode) {
                editionMode = false;
                shell.removeMouseMoveListener(ManualScreenAreaSelector.this);
            }
        } else if (e.button == 1) {
            // left-click enter edition mode
            // Reset previous selection
            selectedArea = null;
            editionMode = true;
            clickCoordinates = new Point(e.x, e.y);
            lastX = e.x;
            lastY = e.y;
            shell.addMouseMoveListener(ManualScreenAreaSelector.this);
        }
    }

    @Override
    public void mouseUp(final MouseEvent e) {
        // left click, only if edition was set
        if ((e.button == 1) && editionMode) {
            editionMode = false;
            shell.removeMouseMoveListener(ManualScreenAreaSelector.this);
            selectedArea = makeRectangleFromSelection(clickCoordinates, new Point(e.x, e.y));
            shell.dispose();
        }
    }

So I would like to know if a more efficient solution, exist in SWT, without having to use the redraw method.

actual result


EDIT
I used 3 images to make the selection work :

  • The first is the image of the screen (a screenshot)
  • The second is the image of the screen + alpha-blended dark rectangle
  • The third is a buffer on which I draw the alpha-blended image + the rectangle copied from the screenshot image.

Performance is acceptable since there is only one alpha blend operation (for the second image).

Only one problem remains, when I used the graphics control of the shell to draw the shell the first time the alpha-blended image is not used as shell-background, everything else is working when the mouse events are sent :

public ManualScreenAreaSelector(final Display display) {

    screenWidth = display.getClientArea().width;
    screenHeight = display.getClientArea().height;
    // create a new Image of the screen
    backGround = new Image(display, display.getBounds());
    GC gc = new GC(display);
    gc.copyArea(backGround, 0, 0);
    gc.dispose();

    // Copy background image and add alpha blended effect
    aplhaBackGround = new Image(backGround.getDevice(), backGround.getImageData());
    GC alphaGC = new GC(aplhaBackGround);
    alphaGC.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
    alphaGC.setAlpha(200);
    alphaGC.fillRectangle(0, 0, screenWidth, screenHeight);
    alphaGC.dispose();

    // create the shell
    shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP | SWT.NO_BACKGROUND);
    shell.setBounds(display.getClientArea());

    // get shell graphics control
    shellGraphics = new GC(shell);
    // set the shell image to screen image <-- does nothing
    shellGraphics.drawImage(aplhaBackGround, 0, 0);

    // Image for the shell
    bufferImage = new Image(shell.getDisplay(), shell.getBounds());

    shell.print(shellGraphics);
}
public void mouseMove(final MouseEvent e) {
    if (editionMode) {
        // Get selected area
        final Rectangle selectedArea = makeRectangleFromSelection(clickCoordinates, new Point(
                e.x, e.y));
        // Copy alpha blended background into the buffer
        GC gc1 = new GC(aplhaBackGround);
        gc1.copyArea(bufferImage, 0, 0);
        gc1.dispose();
        // Paint "normal" background over selected area
        GC gc2 = new GC(bufferImage);
        gc2.drawImage(backGround, selectedArea.x, selectedArea.y, selectedArea.width,
                selectedArea.height, selectedArea.x, selectedArea.y, selectedArea.width,
                selectedArea.height);

        // draw the painted image on the shell
        shellGraphics.drawImage(bufferImage, 0, 0);
        gc2.dispose();
    }
}
  • 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-31T05:12:15+00:00Added an answer on May 31, 2026 at 5:12 am

    Try this approach:

    • Instead of shell.redraw();, call shell.print(gc) once and attach the GC to an image buffer. That gives you an image of the shell.

    • Get the OpenGL extensions for SWT or jogl. Put the image in the background and create a 3D rectangle for the selection. Use OpenGL for the alpha operations and composition.

    The reasoning is that alpha operations are slow in SWT which often has to use the CPU to do it. Your graphics card can do the same operation hundreds of times per second without breaking a sweat.

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

Sidebar

Related Questions

I would like to create a installer (like BootStarper) to achieve following steps using
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to create a database backed interactive AJAX webapp which has a
I would like to create an SSL connection for generic TCP communication. I think
I would like to create a file format for my app like Quake, OO,
I would like to create a stored procedure in MySQL that took a list
I would like to create an application wide keyboard shortcut for a Java Swing
I would like to create events for certain resources that are used across various

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.