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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:48:12+00:00 2026-05-21T12:48:12+00:00

I have written an application with a panel and three buttons. I want to

  • 0

I have written an application with a panel and three buttons. I want to add selection this buttons using the mouse. I mean like we have in Windows on the Desktop. I press the left mouse button and with the movement of the mouse the area selection is growing.

Is there a specific interface in this or do I have it manually call the appropriate methods for event listeners and there draw transparent rectangle? Here is a picture:

Example Screenshot

So I have a problem when I paint rectangle using event mouse-dragged, button is repainting so user see blinking button. I want to this button don’t disapear when I paint rectangle. I think that I need to use glassPane. This is my conception. I have a frame. In frame I add panel with button and I need another panel where I will paint transparent rectangle. I am thinking then my button will not be still repainting. What do you think about this conception. Or maybe someone have another idea. This is code:

        @Override
        public void mousePressed(MouseEvent e) {
            startPoint=e.getPoint();
            setOpaque(true);

            Graphics2D g2 = (Graphics2D)getGraphics();

            Rectangle2D prostokat = new Rectangle2D.Double();
            prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.draw(prostokat);
            g2.setColor(Color.BLUE);
            g2.fill(prostokat);


        }



        @Override
        public void mouseDragged(MouseEvent e) {
            setOpaque(true);

            Graphics2D g2 = (Graphics2D)getGraphics();
            Rectangle2D prostokat = new Rectangle2D.Double();

            prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.draw(prostokat);
            g2.setColor(Color.BLUE);
            g2.fill(prostokat);
            paintComponent(g2);


        }

        int rule = AlphaComposite.SRC_OVER;
        float alpha = 0.85F;




    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable()
        {
public void run()
            {
                zaznacz rys = new zaznacz();
                JFrame frame = new JFrame();
                JButton Button = new JButton("1");
                JPanel panel = new JPanel();



                panel.add(Button);
                rys.add(panel);
                frame.setSize(400,300);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                panel.setOpaque(false);

                frame.add(rys);


            }
        });
    }

}

I know that code is no perfect but almost work. I have a little problem. When I press the mousebutton and dragging my button disapear.
enter image description here
enter image description here

I don’t need advise like “your code is wrong”. I know that and I want to somebody help me what I must correct. I know that I shouldn’t use paintComponent() in mouseEvents but only that way I can paint transparent rectangle. Or maybe you can othet idea how I can draw transparent rectangle. I try and try and I think that i must change mouseDragged method. because when I delete code from this method and only draw rectangle over a button all is ok. But problem is when I need draw rectangle by dragging mouse. I should change paint but I don’t have idea how. Anyone can help me or try help me?

  • 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-21T12:48:13+00:00Added an answer on May 21, 2026 at 12:48 pm

    I think that that code doesn’t works in this forms(main), maybe someone will debug that for you, please follows with Joey’s advices

    hmmm very offensive words, anyway follows (Oracle Java tutorial) http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html

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

Sidebar

Related Questions

I have a large application written using .Net remoting for file transfer. This was
I have written a application which runs in the background. I want to write
I have written an application in which I want to convert a .jpg image
I have a Windows Mobile application written in Java that uses AWT for the
I have written an application which simply executes java -jar . I want the
We have written an application that manages OpenVPN from the tray as an add-on
I have written an application (using Delphi 2009) that allows a user to select
I have written an application which is using NHibernate to provide the underlying object
I have written this event photography application that features a slideshow among other things.
I have written an application: parser and web portal. They are both using SQL

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.