Results 1 to 7 of 7
  1. #1
    edi233 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Drawing a selection box using swing

    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:


    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:
    Java 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);
    
    
                }
            });
        }
    
    }
    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.


    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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    :groan:

    Never use getGraphics() of a Component. Never call paintComponent(...) in client code.

    Learn how to correctly perform custom painting.
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

  4. #4
    edi233 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    I'm sorry, but I don't igored your advise. I need help and some advise and ideas how I can solve my problem and you give me only link with base of painting in java. I read and I still don;t know how I solve my problem. Now I change my code. I want add in mousepressed and mouseDragged creating new glass pane and painting on glass pane. What do You think about that? Sorry about this all posts but I am desperate. I don't want to get only links but I want some ideas how You start solve this problem when You must do aplication like in my example. And if you want to help me, help me and don't give only links. I can help too give people links for example to google.com, but this is not help.
    Last edited by edi233; 05-09-2011 at 02:09 PM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    Sorry, I'm not here to spoonfeed. You need to learn, and that link is one of the best learning resources for performing custom painting.

    db

  6. #6
    edi233 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    I don't want to argue with you, but your posts not help me. I read materials at this page I know that I must change my code. But I still don't know what I need change except methods getGraphics and paint in client mode and your posts doesn't help me. Have you any idea how can I solve problem? If no so why you comment all my posts. I am looking for help and only what you can offer me that is it link and malicious comments my other posts

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

Similar Threads

  1. Selection Sort. please help!
    By cassato in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 10:26 PM
  2. Selection in JTable
    By tremon in forum JDBC
    Replies: 1
    Last Post: 06-11-2010, 08:57 PM
  3. Help in Project Selection?
    By Hardik in forum New To Java
    Replies: 2
    Last Post: 09-03-2009, 05:27 PM
  4. Key selection for SSL/TLS
    By OrangeDog in forum Advanced Java
    Replies: 3
    Last Post: 04-27-2009, 10:38 PM
  5. Dir File Selection
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 07:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •