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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:40:53+00:00 2026-05-22T18:40:53+00:00

I was a problem with drawing rectangle using mouse. I have solve my problem

  • 0

I was a problem with drawing rectangle using mouse. I have solve my problem and I get results what I want. But I have a small problem. If I want to start draw rectangle when I click mouse button, text in my button change. This is small difference but I don’t want see this. Here is code:

    package draw;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;


public class Selection extends JPanel
    implements  ChangeListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final int WIDE = 640;
    private static final int HIGH = 640;  
    private List<Node> nodes = new ArrayList<Node>();
    private Point mousePt = new Point(WIDE / 2, HIGH / 2);
    private Rectangle mouseRect = new Rectangle();
    private boolean selecting = false;

    public static void main(String[] args) throws Exception {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                JFrame f = new JFrame("GraphPanel");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Selection gp = new Selection(); 
                gp.add(new JButton("Button"));
                f.add(new JScrollPane(gp), BorderLayout.CENTER);                
                f.pack();
                f.setVisible(true);
            }
        });
    }

    Selection() {

        this.setPreferredSize(new Dimension(WIDE, HIGH));      
        this.addMouseListener(new MouseHandler());
        this.addMouseMotionListener(new MouseMotionHandler());
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(new Color(0x00f0f0f0));
        g.fillRect(0, 0, getWidth(), getHeight());

        for (Node n : nodes) {
            n.draw(g);
        }
        if (selecting) {
            g.setColor(Color.BLACK
                    );
            ((Graphics2D) g).setComposite(AlphaComposite.getInstance(rule, alpha));
            g.fillRect(mouseRect.x, mouseRect.y,
                    mouseRect.width, mouseRect.height);

            g.drawRect(mouseRect.x, mouseRect.y,
                mouseRect.width, mouseRect.height);
        }
    }
    int rule = AlphaComposite.SRC_OVER;
    float alpha = 0.9F;


    private class MouseHandler extends MouseAdapter {

        @Override
        public void mouseReleased(MouseEvent e) {
            selecting = false;
            mouseRect.setBounds(0, 0, 0, 0);
            if (e.isPopupTrigger()) {

            }
            e.getComponent().repaint();
        }

        @Override
        public void mousePressed(MouseEvent e) {
            mousePt = e.getPoint();

                Node.selectNone(nodes);
                selecting = true;          
            e.getComponent().repaint();
        }      
    }

    private class MouseMotionHandler extends MouseMotionAdapter {

        Point delta = new Point();

        @Override
        public void mouseDragged(MouseEvent e) {
            if (selecting) {
                mouseRect.setBounds(
                    Math.min(mousePt.x, e.getX()),
                    Math.min(mousePt.y, e.getY()),
                    Math.abs(mousePt.x - e.getX()),
                    Math.abs(mousePt.y - e.getY()));

            } else {
                delta.setLocation(
                    e.getX() - mousePt.x,
                    e.getY() - mousePt.y);

                mousePt = e.getPoint();
            }
            e.getComponent().repaint();
        }
    }



    /** A Node represents a node in a graph. */
    private static class Node {

        private Color color;

        private boolean selected = false;
        private Rectangle b = new Rectangle();

        /** Draw this node. */
        public void draw(Graphics g) {
            g.setColor(this.color);

            if (selected) {
                g.setColor(Color.darkGray);
                g.drawRect(b.x, b.y, b.width, b.height);
            }
        }




       /** Mark this node as slected. */
         public void setSelected(boolean selected) {
            this.selected = selected;
        }

        /** Select no nodes. */
        public static void selectNone(List<Node> list) {
            for (Node n : list) {
                n.setSelected(false);
            }
        }
  }



    @Override
    public void stateChanged(ChangeEvent arg0) {
        // TODO Auto-generated method stub

    }

}

I think that can be a problem with place where I implement my button. I use eclipse SDK.
Can you 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-22T18:40:54+00:00Added an answer on May 22, 2026 at 6:40 pm

    Change your main to this. The reason is that you are setting alpha to < 1 for the entire panel and so the button is getting lighter shade. Simply move the button out of the panel and you are set.

    public static void main(String[] args) throws Exception {
        EventQueue.invokeLater(new Runnable() {
    
            public void run() {
                JFrame f = new JFrame("GraphPanel");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel(new BorderLayout());
                JButton button = new JButton("Button");
                panel.add(button, BorderLayout.PAGE_START);
                Selection gp = new Selection(); 
                panel.add(gp, BorderLayout.CENTER);
                f.add(new JScrollPane(panel), BorderLayout.CENTER);                
                f.pack();
                f.setVisible(true);
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have tried to draw an rubberband rectangle on a form using the
I have a problem with drawing. I have a frame with one button. Using
Using System.Drawing.Printing, I want to print a set of lines on print document. But
I have a problem drawing something quickly in .NET. I don't think that any
Problem: I have two spreadsheets that each serve different purposes but contain one particular
I have winform with a TextBox and I want to draw some GDI graphics
I am doing some image scaling using GDI+ (C#), and have noticed a problem
I have this problem while drawing a square in canvas, made out of two
I have a canvas onto which I am drawing a JavaScript game. The problem
i'm drawing rectangle on picturebox with mouse events: private void StreamingWindow_MouseDown(object sender, MouseEventArgs e)

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.