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

New Topic/Question
Reply



MultiQuote



|