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 3 of 3

Thread: My MouseInputListener methods aren't being called.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Exclamation My MouseInputListener methods aren't being called.

    I have the following class which uses mousePressed, mouseDragged, and mouseReleased. I've thrown some breakpoints in these methods with jdb, but I find that they aren't triggered when I try and click on my canvas. I've been working on this for a few hours and can't figure out why. Could someone offer a second set of eyes?

    Thanks,
    import java.awt.BasicStroke;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.event.MouseInputListener;
     
    class Canvas extends JPanel implements MouseInputListener {
      private BufferedImage m_img;
      private Point m_refPoint;
      private JLabel m_container;
     
      public Canvas() {
        m_img = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
        m_refPoint = null;
        m_container = new JLabel(new ImageIcon());
        m_container.setIcon(new ImageIcon(m_img));
        m_container.addMouseListener(this);
        m_container.addMouseMotionListener(this);
      }
     
      protected void draw(Point end, BufferedImage img) {
        Graphics2D g2 = img.createGraphics();
        g2.setPaint( m_selector.getFg() );
        g2.setStroke( new BasicStroke(2.0f) );
     
        g2.draw(new Rectangle2D.Float((float)m_refPoint.x,
                                      (float)m_refPoint.y, 
                                      (float)Math.abs(end.x-m_refPoint.x),
                                      (float)Math.abs(end.y-m_refPoint.y)));
     
        m_container.setIcon(new ImageIcon(img));
        repaint();
      }
     
      public void mousePressed(MouseEvent e) {
        m_refPoint = e.getPoint();
      }
     
      public void mouseDragged(MouseEvent e) {
        if (m_refPoint == null) return;
     
        Point end             = e.getPoint();
        BufferedImage scratch = new BufferedImage(m_img.getColorModel(),
                                m_img.copyData(null), 
                                m_img.getColorModel().isAlphaPremultiplied(),
                                null);
        draw( end, scratch );
      }
     
      public void mouseReleased(MouseEvent e) {
        if (m_refPoint == null) return;
        draw( e.getPoint(), m_img);
      }
     
      public void mouseClicked(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited (MouseEvent e) {}
      public void mouseMoved  (MouseEvent e) {}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My MouseInputListener methods aren't being called.

    Where is the component with the listeners added to the GUI container?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: My MouseInputListener methods aren't being called.

    The component with listeners is "JLabel m_container" which is a member of Canvas and holds an ImageIcon().

    I use these so that when I use the mouse in the container, my methods are called:
    m_container.addMouseListener(this);
    m_container.addMouseMotionListener(this);



    I think my problem is the sizing of the JLabel. I've tried setting m_container.setBackground(Color.BLACK); and everything is still the default colour. When I've loaded BufferedImages from files, JLabel get's resized properly, so I think the problem is in how I've created the BufferedImage.

    I HATE layout managers, for 8 weeks now, these have been the most time consuming parts of my java course. I'm tempted to use a null layout manager and give up on scaling.

    --- Update ---

    Nevermind, I forgot:
    this.add(m_container);

Similar Threads

  1. What is this component called?
    By Cronus in forum AWT / Java Swing
    Replies: 0
    Last Post: July 19th, 2013, 02:22 PM
  2. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  3. Formulas within my code aren't cooperating
    By bohrstein7 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: September 2nd, 2011, 03:30 PM
  4. GUI: JButtons aren't visible until clicked
    By Staticity in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 7th, 2011, 02:49 PM