Results 1 to 13 of 13
  1. #1
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Mouse Listener For two JPanels

    Hello,

    The program i am creating consists of two JPanels (currently) and one canvas. The canvas is in the center of the JFrame, while flanked by JPanels where both have 13 buttons each.

    To paint a simple picture, my program will draw letters on the middle canvas when a button is clicked.

    I created my own buttons that implement the ActionListener interface , so when a button is clicked it will display its character in the console. But my question is, How do you implement the listener to draw something on the Canvas when the button's are clicked?

    Thanks
    Last edited by SunDontShine; 01-06-2015 at 10:21 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Mouse Listener For two JPanels

    Quote Originally Posted by SunDontShine View Post
    But my question is, How do you implement the listener to draw something on the Canvas when the button's are clicked?
    Simply cause the button event to modify variables which are used to draw shapes, figures, or animation. Then when the values have been set, do a repaint(). Then the Canvas paint method will be called. BTW, Canvas is AWT. JPanels are Swing. I suggest you get away from AWT. Just do your painting in a JPanel. To do so, override paintComponent().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Re: Mouse Listener For two JPanels

    Hello,

    Well I typed up a quick sample of code of related areas I am having issues with. The 'painting' screws up the JButtons and throws them everywhere. The left and right again are JPanels, with the center a 'painttest' extending from JPanel.

    How do I stop the jbuttons from messing up, and how do I avoid that circle drawn by itself from showing up. My goal is to draw letters eventually, but how do I also just create the ball as if it were floating.



    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class painttest extends JPanel implements ActionListener
    {
        private int xpos, ypos;
        public painttest()
        {
            xpos = 0;
            ypos = 0;
    
    
        }
    
        public static void main(String [] args)
        {
             JFrame frame = new JFrame("Painting Test");
                frame.setSize(650,700);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);
    
            JPanel panel1 = new JPanel();
            JPanel panel2 = new JPanel();
    
            painttest center = new painttest();
                center.setBackground(Color.cyan);
    
            JButton button1 = new JButton("Button 1");
            JButton button2 = new JButton("Button 2");
    
            panel1.add(button1);
            panel2.add(button2);
    
            frame.add(panel1, BorderLayout.WEST);
            frame.add(center, BorderLayout.CENTER);
            frame.add(panel2, BorderLayout.EAST);
    
    
    
            frame.setVisible(true);
    
            Timer timer = new Timer(50, center);
            timer.start();
    
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            g.fillOval(xpos,ypos,30,30);
            repaint();
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            xpos = xpos +1;
            ypos = ypos +1;
        }
    
        @Override
        public void setBackground(Color bg) {
            super.setBackground(bg);
        }
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Mouse Listener For two JPanels

    Why on earth are you calling repaint() from the paintComponent() method?

    When do you add an ActionListener to those JButtons?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Re: Mouse Listener For two JPanels

    In this example I have not. I am callling repaint because I want to eventually simulate how to write letters.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Mouse Listener For two JPanels

    You should never call repaint() from the paintComponent() method. Instead, set up a Timer or a Thread that calls repaint() for you.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Re: Mouse Listener For two JPanels

    ok well thank you.

    Do you have any other input on my other questions by any chance?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Mouse Listener For two JPanels

    A couple other observations.

    1. Class names start with upper case (check out Java Naming Conventions in my signature).
    2. In paintComponent() call super.paintComponent(g) first. That way the background color will be set and the ball won't leave a trail as it moves. Essentially this causes the JPanel to be redrawn before your own paint code is executed.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Mouse Listener For two JPanels

    Quote Originally Posted by SunDontShine View Post
    Do you have any other input on my other questions by any chance?
    Make the changes we've suggested so far, then repost your updated code along with specifically what it's doing that you don't understand (pictures might help), and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  10. #10
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Re: Mouse Listener For two JPanels

    1. Class names start with upper case (check out Java Naming Conventions in my signature).
    Thanks for the link Jim. That is very helpful.
    2. In paintComponent() call super.paintComponent(g) first. That way the background color will be set and the ball won't leave a trail as it moves. Essentially this causes the JPanel to be redrawn before your own paint code is executed.
    Thanks Again, I watched some videos but they deleted that line so it threw me for a spin. Thanks.

    How would I go about drawing a line similar if you were actually writing a Line on the screen with your finger or on a piece of paper with a pencil.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Mouse Listener For two JPanels

    Quote Originally Posted by SunDontShine View Post
    How would I go about drawing a line similar if you were actually writing a Line on the screen with your finger or on a piece of paper with a pencil.
    Not certain. You can do it with a mouse by recording the coordinates as the mouse moves and connecting them with a line. But you are describing using a stylus or your finger on a touch screen. I am not certain how to catch those types of events with the current JDK. They may use the same event handlers as the mouse. I just have never tried it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    SunDontShine is offline Member
    Join Date
    Jul 2014
    Posts
    22
    Rep Power
    0

    Default Re: Mouse Listener For two JPanels

    sorry for the confusion,

    I meant I want to make a trail with the paint method.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Mouse Listener For two JPanels

    Quote Originally Posted by SunDontShine View Post
    sorry for the confusion,
    I meant I want to make a trail with the paint method.
    Within the paintComponent() method you use the various methods of the graphics context. You can cast the Graphics class to Graphics2D for more functionality. Both the Graphics and Graphics2D classes are documented in the JDK API documentation. To draw a line from one point to the other you can do something like the following:

    Java Code:
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(x1,y1,x2,y2); // draw a line from (x1,y1) to (x2,y2)
    }
    The coordinates are usually instance fields and can be settable from other methods such as action or mouse listeners.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 11-05-2014, 02:34 AM
  2. Keyboard Listener to Mouse Listener
    By cupOJava in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2013, 09:26 PM
  3. mouse listing over JPanels
    By willemjav in forum AWT / Swing
    Replies: 13
    Last Post: 03-20-2013, 05:34 PM
  4. Mouse Listener
    By Quizerno in forum New To Java
    Replies: 8
    Last Post: 03-17-2011, 05:25 AM
  5. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 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
  •