Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Drawing with the mouse in the same frame as swing components

  1. #1
    StillCantPlay is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Drawing with the mouse in the same frame as swing components

    Hi - I can't find anything on the internet to help with this.

    Any help or suggestions will be greatly appreciated!

    I have a frame with swing components (Jbuttons) and two different classes with logical gate drawings..
    Drawing with the mouse in the same frame as swing components-logic-gate-display.png

    I want to be able to draw with the mouse on the same panel as these logical gates:

    The below works fine if I take super.paint(g) out, but then the lightweight components mess up, I can only have it one way or the other..
    Is there any way around this?

    Java Code:
            //Class to display gates to screen:
        class DrawCanvas extends JPanel implements MouseMotionListener
        {        
            
            public DrawCanvas()
            {
                addMouseMotionListener(this);
                //setOpaque(false);
                
                setVisible(true);
            }
            public void paint(Graphics g)
            {
                super.paint(g);   //<----
    
                setBackground(Color.gray);
                System.out.println("Painting the screen");
                
                //Display my AND/OR gate drawing:
                if (bAnd == true)
                {
                    System.out.println("And gate has been drawn!");
                    andDisplay.andGraf(g);
                }
    
                
                if (bOr == true)
                {
                    System.out.println("Or gate has been drawn!");
                    orDisplay.OrGraf(g);
                }
    
    
                //Draw Continuous line:
                g.setColor(drawingColour);
                g.fillRect(mx1,my1, 5, 5);
    
                
                
            }
    
            //Continuous drawing: (works without super.paint)
    
            public void mouseDragged(MouseEvent e)
            {
                  mouseMoved(e);
                  repaint();
            }
    
    
            public void mouseMoved(MouseEvent e)
            {
                mx1 = (int) e.getPoint().getX();
                my1 = (int) e.getPoint().getY();
                  System.out.println("x = " + mx1);
                  System.out.println("y = " + my1);
            }
        
        }

    Thanks.
    Last edited by StillCantPlay; 04-03-2012 at 10:30 PM. Reason: Important bit's in bold

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Drawing with the mouse in the same frame as swing components

    You will want to search this forum, as there are many examples of how to draw. For example:
    StillCantPlay likes this.

  3. #3
    StillCantPlay is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Drawing with the mouse in the same frame as swing components

    Sweet, just finished the program - thanks for the reply.. the links were very helpful!

Similar Threads

  1. Repositioning components based on mouse position
    By robbie.26 in forum AWT / Swing
    Replies: 3
    Last Post: 06-21-2011, 06:55 AM
  2. Help with drawing a string using mouse events
    By ptuckley in forum AWT / Swing
    Replies: 2
    Last Post: 12-14-2010, 11:09 AM
  3. adding components to frame
    By roaan in forum New To Java
    Replies: 4
    Last Post: 07-05-2009, 03:30 PM
  4. drawing with mouse
    By aveek in forum Java 2D
    Replies: 1
    Last Post: 06-27-2009, 01:38 PM
  5. drawing points with mouse
    By josephdcoleman in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 12:39 AM

Posting Permissions

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