3 Replies - 378 Views - Last Post: 25 May 2012 - 09:33 AM Rate Topic: -----

#1 06ChargerRT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-May 12

How to add a mousepressed event

Posted 25 May 2012 - 09:05 AM

I am a student and I am continuing to learn Java. I have been running through my textbook doing the exercises to get better. I completed my 1st Java class and I just want to learn more about it. My working code is below but for the life of me I can not figure out how a mousepressed event works. I want the ball to start moving when i click on the screen but no matter where I put my mousepressed event I get errors. My first code works but the ball is moving at startup.

I have pretty much put this event all over the code but nothing seems to work. I know I am missing something but I just can't figure it out. Why is the mouse pressed event not working? Am I missing or misplacing the code. I may have added too much code when I made the run() method.

My working code:
package bouncingball;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class BouncingBall extends JPanel implements Runnable
{
    private static final int vertical = 640; // vertical size of window
    private static final int horizontal = 480; // horizontal size of window
    private float radius = 50; // ball radius
    private float x = radius + 50;
    private float y = radius + 20; // center coordinates of ball
    private float xMove = 6;
    private float yMove = 5;
    private static final int updateRate = 100; // refresh of screen per second

    public BouncingBall()
    {
        setPreferredSize( new Dimension ( vertical, horizontal )); //size of panel
    }

    @Override
    public void run()
    {
        while ( true )
        {
            x += xMove;
            y += yMove;
            if ( x - radius < 0 )
            {
                xMove = -xMove; // reverse directions
                x = radius;
            }
            else if ( x + radius > vertical)
            {
                xMove = -xMove;
                x = vertical - radius;
            }

            if ( y - radius < 0 )
            {
                yMove = -yMove;
                y = radius;
            }
            else if ( y + radius > horizontal)
            {
                yMove = -yMove;
                y = horizontal - radius;
            }

            repaint();
            try
            {
                Thread.sleep( 1000 / updateRate );
            }
            catch ( InterruptedException e )
            {
                JOptionPane.showMessageDialog( null, "I'm sleeping!", null, JOptionPane.ERROR_MESSAGE);
            }
        } // end while
    } // end run

    @Override
    public void paintComponent ( Graphics graphics )
    {
        super.paintComponents(graphics);
        graphics.setColor( Color.BLACK);
        graphics.fillRect( 0, 0, vertical, horizontal );
        graphics.setColor( Color.BLUE );
        graphics.fillOval(( int )( x - radius), ( int ) ( y - radius), ( int ) ( 2 * radius), ( int ) ( 2 * radius ));
    }

    public static void main(String[] args)
    {
        JFrame jFrame = new JFrame( "Blue Bouncing Ball" );
        BouncingBall bBall = new BouncingBall();
        jFrame.add( bBall );
                jFrame.setVisible( true );
        jFrame.setSize( 650, 400 );
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();  
        Thread thread = new Thread( bBall );
        thread.start();
    } // end main
} // end runnable



Non-working code with my mousepressed attempt:
package bouncingball;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class BouncingBall extends JPanel implements Runnable, MouseListener
{
    private static final int vertical = 640; // vertical size of window
    private static final int horizontal = 480; // horizontal size of window
    private float radius = 50; // ball radius
    private float x = radius + 50;
    private float y = radius + 20; // center coordinates of ball
    private float xMove = 6;
    private float yMove = 5;
    private static final int updateRate = 100; // refresh of screen per second

    public void init()
    {
        addMouseListener();
    }
    
    public BouncingBall()
    {
        setPreferredSize( new Dimension ( vertical, horizontal )); //size of panel
    }

    @Override
    public void paintComponent ( Graphics graphics )
    {
        super.paintComponents(graphics);
        graphics.setColor( Color.BLACK);
        graphics.fillRect( 0, 0, vertical, horizontal );
        graphics.setColor( Color.BLUE );
        graphics.fillOval(( int )( x - radius), ( int ) ( y - radius), ( int ) ( 2 * radius), ( int ) ( 2 * radius ));
    }

    public static void main(String[] args)
    {
        JFrame jFrame = new JFrame( "Blue Bouncing Ball" );
        BouncingBall bBall = new BouncingBall();
        jFrame.add( bBall );
        jFrame.setVisible( true );
        jFrame.setSize( 650, 400 );
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();  
        Thread thread = new Thread( bBall );
        thread.start();
    } // end main

    private void addMouseListener() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mousePressed(MouseEvent me) 
    {   
    run();   
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    private void run() 
    {
    while ( true )
        {
            x += xMove;
            y += yMove;
            if ( x - radius < 0 )
            {
                xMove = -xMove; // reverse directions
                x = radius;
            }
            else if ( x + radius > vertical)
            {
                xMove = -xMove;
                x = vertical - radius;
            }

            if ( y - radius < 0 )
            {
                yMove = -yMove;
                y = radius;
            }
            else if ( y + radius > horizontal)
            {
                yMove = -yMove;
                y = horizontal - radius;
            }

            repaint();
            try
            {
                Thread.sleep( 1000 / updateRate );
            }
            catch ( InterruptedException e )
            {
                JOptionPane.showMessageDialog( null, "I'm sleeping!", null, JOptionPane.ERROR_MESSAGE);
            }
        } // end while
    } // end run
    
} // end runnable



Is This A Good Question/Topic? 0
  • +

Replies To: How to add a mousepressed event

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,149
  • Joined: 06-March 08

Re: How to add a mousepressed event

Posted 25 May 2012 - 09:16 AM

you never

addMouseListener(this);

to your JPanel

you wrote an init() method, that you never called by the way, which has a
addMouseListener() but should be addMouseListener(this)
so for just one line, better to simply put it in your constructor
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5427
  • View blog
  • Posts: 8,730
  • Joined: 19-March 11

Re: How to add a mousepressed event

Posted 25 May 2012 - 09:20 AM

You have a method called "addMouseListener", and this is what I find in it:


Quote

throw new UnsupportedOperationException("Not yet implemented");


This will not do anything.

It's perfectly reasonable to have an object serve as its own mouseListener, but you'd want to do something like

addMouseListener(this); 


This means that when this object fires a MouseEvent, it will also hear it.

Have a look at this tutorial if you haven't already.
Was This Post Helpful? 0
  • +
  • -

#4 06ChargerRT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-May 12

Re: How to add a mousepressed event

Posted 25 May 2012 - 09:33 AM

Thanks for the info all, I haven't taken my second class in Java so the mouse events I have not learned them yet. I will look at the tutorial that Jon offered.

Jon I am using netbeans and some of the code within the mouse events is pre-generated by netbeans. I was not sure if it was needed or not so I just left it in there.

I'll look at the tutorial and let you guys know how I made out.

This is really cool stuff. My first Java class, all the code ran at the bottom of netBeans in the console. These exercises have a windows like screen and I am really digging it. You can tell that I am new to this. I can get around the math parts of the code pretty well but when it comes to figuring out where code needs to be placed, that's a different story.

I'll let you guys know how I make out after looking at the tutorials.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1