Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Drawing a selection box using swing

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:
       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. Maybe I should use glass pane and paint only on glass pane? I need some ideas. Pls help me.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Drawing a selection box using swing

    The reason you're seeing flickering is because you're doing your painting incorrectly. If you're calling getGraphics() on a Component, you're going to have issues.

    You have the general idea right- you're going to need a MouseListener with either an extension of JPanel that overrides paintComponent(), or a GlassPane is another way to go.

    Get rid of the getGraphics(), do the painting correctly, and you should see an improvement. Recommended reading: Painting in AWT and Swing
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Drawing a selection box using swing

    Ugh, but I guess I'm just wasting my time, since you've already received and ignored this advice elsewhere.

    This thread has been cross posted here:

    http://www.java-forums.org/awt-swing/43694-drawing-selection-box-using-swing.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing a selection box using swing

    Thx for advise. I do not ignore all my and others posts. I read more info and correct my code. Sorry for crossposts

Similar Threads

  1. [SOLVED] Selection Sorting a text array
    By ComputerSaysNo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 30th, 2011, 06:56 PM
  2. [SOLVED] selection end and start
    By nasi in forum What's Wrong With My Code?
    Replies: 13
    Last Post: May 10th, 2010, 05:05 AM
  3. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM
  4. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 02:04 AM

Tags for this Thread