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

  • Home
  • SEARCH
  • 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 4273316
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:41:47+00:00 2026-05-21T07:41:47+00:00

I just began my adventure with java. I have written an application with a

  • 0

I just began my adventure with java. I have written an application with a panel and three buttons. But I have a problem. 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. And I have a question. Is there a specific interface in this or do I have it manualy, call the appropriate methods for event listeners and there draw transparent rectangle? Here is a picture:
enter image description here

I make a some progress, but i have another problem. Everything is ok but when I paint rectangle, button is repainting. I want to this button don’t disapear when i paint rectangle. This is code:

    import java.awt.AlphaComposite;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class zaznacz extends JPanel implements MouseListener, MouseMotionListener
{


     Rectangle newLine=null;
     public zaznacz() 
     {

            addMouseListener(this);
            addMouseMotionListener(this);

        }


        static class Rectangle {

            int x1,y1,x2,y2;

            Rectangle(int _x1, int _y1,int _x2, int _y2){

                x1=_x1;y1=_y1;x2=_x2;y2=_y2;
            }



             void paint(Graphics g)
             {



                 g.drawRect(x1, y1, x2, y2);


            }

        }
        public void mouseClicked(MouseEvent e) {

                }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @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 mouseReleased(MouseEvent e) 
        {
            repaint();

        }
        Point startPoint;


        @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);


        }
        @Override
        public void mouseMoved(MouseEvent e) {


        }
        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);


            }
        });
    }

}

Somebody can 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-21T07:41:48+00:00Added an answer on May 21, 2026 at 7:41 am

    Edit: If you are referring to the highlighted rectangle, such as the one that appears as you drag your mouse over the desktop, then your approach would certainly be valid and feasible; I do not believe that are any features in Swing which provide an implementation of this feature. I don’t know whether JDesktop supports this, but it may be worth a look.

    Edit 2: Also take a look at GlassPane. It would make it much more convenient to implement what you describe than doing everything on the same pane would.

    If I understand your question correctly, the effects which ensue when the button is being hovered over or pressed are controlled by the Look and Feel (L&F). You can select the Look and Feel you would like among those that are available on the host operating system by using the javax.swing.UIManager class. Writing your own Look and Feel is a nontrivial task and usually takes several weeks of time, even for a person who is experienced in using image-editing applications and knows how to program graphical user-interfaces.

    Here’s an example (from this link) which shows you how to change the L&F to Nimbus:

    try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
    } catch (Exception e) {
        // If Nimbus is not available, you can set the GUI to another look and feel.
    }
    

    There are many others available, but it bears noting that many developers (and users) prefer to use the system’s default look and feel rather than that which is default for Java Swing components.

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

Sidebar

Related Questions

I just began toying around with F# in Mono and the following problem arose
I am currently developing on an advertising system, which have been running just fine
Just began researching mvc, and am not sure I grasp it yet. From what
Just began to develop using LINQ, and still can't understand some simple things. So,
I just began using Visual Studio to work on a practice .NET project. I
I just began reading more about Markov Chain Generators today, and am really intrigued
I just began experimenting with FluentMigrator . I noticed that failed migrations are not
I just began to work with Objective-C and I'm managing pretty well. My last
I just began looking into source control.... And installed subversion from collabnet... I also
I just began working on a little twitter-app using tweepy. is there any kind

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.