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

Thread: Multiple methods in the same inner class?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple methods in the same inner class?

    Hey guys,

    I have two programs. 1 which implements multiple mouse events:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class MousePanel extends JPanel
        implements MouseInputListener {
     
        private int startX, startY, endX, endY;
        private Color lineColor = Color.black;
        private JLabel mouseStatus;
     
        public MousePanel() {
            setLayout(new BorderLayout());
            setBackground(Color.white);
            mouseStatus = new JLabel();
            add(mouseStatus,BorderLayout.SOUTH);
     
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
     
            // Create a new window using the Swing class JFrame and add this panel
     
            makeFrame();
        }
     
        public void mouseEntered(MouseEvent me) {
            mouseStatus.setText("Object entered X: " +
                               me.getX() + "  Y: " + me.getY());
        }
     
        public void mouseExited(MouseEvent me) {
            mouseStatus.setText("Object exited X: " +
                               me.getX() + "  Y: " + me.getY());
        }
     
        public void mousePressed(MouseEvent me) {
            mouseStatus.setText("Button pressed X: " +
                               me.getX() + "  Y: " + me.getY());
            startX = me.getX();
            startY = me.getY();
        }
     
        public void mouseReleased(MouseEvent me) {
            mouseStatus.setText("Button released X: " +
                               me.getX() + "  Y: " + me.getY());
            lineColor = Color.black;
            endX = me.getX();
            endY = me.getY();
            repaint();
        }
     
        public void mouseClicked(MouseEvent me) {
            mouseStatus.setText("Button clicked X: " +
                               me.getX() + "  Y: " + me.getY());
        }
     
        public void mouseMoved(MouseEvent me) {
            mouseStatus.setText("Mouse moved X: " +
                               me.getX() + "  Y: " + me.getY());
        }
     
        public void mouseDragged(MouseEvent me) {
            mouseStatus.setText("Button dragged X: " +
                               me.getX() + "  Y: " + me.getY());
            lineColor = Color.lightGray;
            endX = me.getX();
            endY = me.getY();
            repaint();
        }
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(lineColor);
            g.drawLine(startX, startY, endX, endY);
        }
     
     
        // Create a window frame
     
        public void makeFrame() {
     
            // Instantiate a window frame using Swing class JFrame
            JFrame frame = new JFrame("MousePanel");
     
            // When the window is closed terminate the application
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
     
            // set initial size of window
    	    frame.setSize(800, 600);
     
            // add the current object to the centre of the frame and make visible
            frame.getContentPane().add(this, BorderLayout.CENTER);
            frame.setVisible(true);
        }
    }

    However, if I were to want all these methods as anonymous inner classes, say for a larger JPanel, I can't seem to get it to work for the graphics method. As far as I know (from what I've tried), I can't get the graphics method to implement. Maybe I don't know the right code, so I was hoping someone could help.

    Thanks.

    Here's what I've got so far:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class MousePanel3 extends JPanel {
        private JLabel mouseStatus;
        private int startX, startY, endX, endY;
        private Color lineColor = Color.black;
     
        public MousePanel3() {
            setLayout(new BorderLayout());
            setBackground(Color.white);
            mouseStatus = new JLabel();
            add(mouseStatus,BorderLayout.SOUTH);
     
            // Anonymous inner class that overides method mouseClicked
            this.addMouseListener(new MouseAdapter() {
               public void mouseEntered(MouseEvent me) {
                   mouseStatus.setText("Object entered X: " +
                      me.getX() + "  Y: " + me.getY());
               }
            });
            this.addMouseListener(new MouseAdapter() {
               public void mouseExited(MouseEvent me) {
                   mouseStatus.setText("Object exited X: " +
                      me.getX() + "  Y: " + me.getY());
               }
            });
            this.addMouseListener(new MouseAdapter() {
               public void mousePressed(MouseEvent me) {
                   mouseStatus.setText("Button pressed X: " +
                      me.getX() + "  Y: " + me.getY());
                   startX = me.getX();
                   startY = me.getY();
               }
            });
            this.addMouseListener(new MouseAdapter() {
               public void mouseReleased(MouseEvent me) {
                   mouseStatus.setText("Button released X: " +
                      me.getX() + "  Y: " + me.getY());
                   lineColor = Color.black;
                   endX = me.getX();
                   endY = me.getY();
                   repaint();
               }
            });
            this.addMouseListener(new MouseAdapter() {
               public void mouseClicked(MouseEvent me) {
                   mouseStatus.setText("Button clicked X: " +
                      me.getX() + "  Y: " + me.getY());
               }
            });
            this.addMouseListener(new MouseAdapter() {
               public void mouseMoved(MouseEvent me) {
                   mouseStatus.setText("Mouse moved X: " +
                      me.getX() + "  Y: " + me.getY());
               }
            });
            this.addMouseListener(new MouseAdapter() {
               public void mouseDragged(MouseEvent me) {
                   mouseStatus.setText("Button dragged X: " +
                      me.getX() + "  Y: " + me.getY());
                   lineColor = Color.lightGray;
                   endX = me.getX();
                   endY = me.getY();
                   repaint();
               }
            });
            this.addMouseListener(new MouseAdapter() {
               public void paintComponent(Graphics g) {
                   super.paintComponent(g);
                   g.setColor(lineColor);
                   g.drawLine(startX, startY, endX, endY);
               }
            });
     
            // Create a new window using the Swing class JFrame and add this panel
     
            makeFrame();
        }
     
     
        // Create a window frame
     
        public void makeFrame() {
     
            // Instantiate a window frame using Swing class JFrame
            JFrame frame = new JFrame("MousePanel3");
     
            // When the window is closed terminate the application
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
     
            // set initial size of window
    	    frame.setSize(800, 600);
     
            // add the current object to the centre of the frame and make visible
            frame.getContentPane().add(this, BorderLayout.CENTER);
            frame.setVisible(true);
        }
    }

    I'm fairly sure it's the this.addMouseListener for the graphics method line is the wrong one, but I don't know what it should be. If anyone could let me know, that'd be great.

    Thanks very much.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Multiple methods in the same inner class?

    I am really confused as to what you're trying to do here. Why is there a paintComponent() method inside a MouseListener? This makes no sense. And why are you splitting each method up into its own MouseListener?
    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
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple methods in the same inner class?

    This isn't a complete program, this is me just messing around. I just wanted to know if it was possible THIS way. The paintComponent() method's there for drawing a line when the mouse listener for drag is activated. I'd prefer to do it using paintComponent and graphics, so I don't really want an alternate, however better, program. Is this possible?

    Thanks.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Multiple methods in the same inner class?

    Still not really sure why the paintComponent() method is inside a MouseListener.

    You have the right idea with your first approach. Just don't move the paintComponent method into your anonymous inner class. Also, I still don't understand why each method has its own inner class instead of just one class that contains them all.
    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
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple methods in the same inner class?

    So you're saying I can have all the methods just in the same inner class? Oh okay. I didn't know that.
    Can I have all the inner classes like that, and then the paint component separate but not anonymous? Thanks!

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple methods in the same inner class?

    Okay. 1 more question. I have this:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class MousePanel3 extends JPanel {
        private JLabel mouseStatus;
        private int startX, startY, endX, endY;
        private Color lineColor = Color.black;
     
        public MousePanel3() {
            setLayout(new BorderLayout());
            setBackground(Color.white);
            mouseStatus = new JLabel();
            add(mouseStatus,BorderLayout.SOUTH);
     
            // Anonymouse inner class that overides method mouseClicked
            this.addMouseListener(new MouseAdapter() {
               public void mouseEntered(MouseEvent me) {
                   mouseStatus.setText("Object entered X: " +
                      me.getX() + "  Y: " + me.getY());
               }
               public void mouseExited(MouseEvent me) {
                   mouseStatus.setText("Object exited X: " +
                      me.getX() + "  Y: " + me.getY());
               }
               public void mousePressed(MouseEvent me) {
                   mouseStatus.setText("Button pressed X: " +
                      me.getX() + "  Y: " + me.getY());
                   startX = me.getX();
                   startY = me.getY();
               }
               public void mouseReleased(MouseEvent me) {
                   mouseStatus.setText("Button released X: " +
                      me.getX() + "  Y: " + me.getY());
                   lineColor = Color.black;
                   endX = me.getX();
                   endY = me.getY();
                   repaint();
               }
               public void mouseClicked(MouseEvent me) {
                   mouseStatus.setText("Button clicked X: " +
                      me.getX() + "  Y: " + me.getY());
               }
               public void mouseMoved(MouseEvent me) {
                   mouseStatus.setText("Mouse moved X: " +
                      me.getX() + "  Y: " + me.getY());
               }
               public void mouseDragged(MouseEvent me) {
                   mouseStatus.setText("Button dragged X: " +
                      me.getX() + "  Y: " + me.getY());
                   lineColor = Color.lightGray;
                   endX = me.getX();
                   endY = me.getY();
                   repaint();
               }
            });
     
            // Create a new window using the Swing class JFrame and add this panel
     
            makeFrame();
        }
     
     
        public void paintComponent(Graphics g) {
           super.paintComponent(g);
           g.setColor(lineColor);
           g.drawLine(startX, startY, endX, endY);
        }
     
        // Create a window frame
     
        public void makeFrame() {
     
            // Instantiate a window frame using Swing class JFrame
            JFrame frame = new JFrame("MousePanel3");
     
            // When the window is closed terminate the application
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
     
            // set initial size of window
    	    frame.setSize(800, 600);
     
            // add the current object to the centre of the frame and make visible
            frame.getContentPane().add(this, BorderLayout.CENTER);
            frame.setVisible(true);
        }
    }

    This works...-ish... When the mouse has been dragged there is a black line indicating the drag, but it doesn't say "mouse dragged" or show a gray line dragging. Any ideas?

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Multiple methods in the same inner class?

    tommyf, when I provided you with the paintComponent() example, I had expected you to read further into it.
    Now the downfall of that help is quite clear, as you haven't quite understood the theory behind it.

    Yes you can have have them all in one and the paintComponent() doesn't belong to any mouse listeners, but to the JPanel (down from JComponent).
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Multiple methods in the same inner class?

    In response to your subsequent post, this is because of the functionality of a MouseListener.
    To understand why it doesn't detect mouse dragged, read the API for MouseListener (Specifically the main description / overview)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple methods in the same inner class?

    Quote Originally Posted by newbie View Post
    tommyf, when I provided you with the paintComponent() example, I had expected you to read further into it.
    Now the downfall of that help is quite clear, as you haven't quite understood the theory behind it.

    Yes you can have have them all in one and the paintComponent() doesn't belong to any mouse listeners, but to the JPanel (down from JComponent).
    No, you're right. I don't understand it, which is why I'm turning to help. I have looked on the internet, and they do provide documentation, but with most of them I don't know where to begin, and I simply don't have time to read through all of them for 1 thing I need to do. Presumably you can see what I need to do to get the right result. Can you explain to me why I need to put paintComponent in the right place, and where that place is? Also one final question, the code doesn't report dragging. Presumably this should sort itself out when the paintComponent gets fixed?

    Thanks.

  10. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Multiple methods in the same inner class?

    The paintComponent() method is in its rightful place right now.
    I'm sorry, but the logic doesn't quite add up when you say you don't have time to read documentation, but do have the time to wait for replies on the forum.
    The solution to the dragging is found in the API document I linked you, or at least the reasons are noted.

    To clarify: the paintComponent method belongs to JComponent, which is a superclass of JPanel, so you shouldn't be saying it's a member of mouseAdapter() (as it was in the beginning of the post).
    Last edited by newbie; January 24th, 2012 at 11:54 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Multiple methods in the same inner class?

    The thing is, the fact that you don't understand basic inheritance and polymorphism tells us that you should definitely be reading the basic tutorials and the documentation. You're diving into the deep end before learning how to swim and then telling the lifeguard that you don't have time to wade around in the shallow end.

    We're not trying to be mean, we're trying to save you a slew of headaches in the near future.

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/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!

  12. #12
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple methods in the same inner class?

    "To track mouse moves and mouse drags, use the MouseMotionListener". *palm*

    Thanks newbie for your help.

    Of course it all works now.

    Bisous X

  13. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple methods in the same inner class?

    Quote Originally Posted by KevinWorkman View Post
    The thing is, the fact that you don't understand basic inheritance and polymorphism tells us that you should definitely be reading the basic tutorials and the documentation. You're diving into the deep end before learning how to swim and then telling the lifeguard that you don't have time to wade around in the shallow end.

    We're not trying to be mean, we're trying to save you a slew of headaches in the near future.

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    I appreciate that, and normally I would, but I have to do A LOT more work besides this program between now and its due date. Sorry for having to be spoon fed this stuff, but I was just in a hurry.

    Thanks again.

Similar Threads

  1. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  2. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 08:42 AM
  3. [SOLVED] Help with Class Methods(Probably simple)
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 9
    Last Post: May 27th, 2010, 09:53 AM
  4. Completely Lost in this problem(multiple methods)
    By wacarter in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 31st, 2010, 04:44 PM
  5. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 09:44 PM