Results 1 to 2 of 2
Thread: MouseAdapter Class?
- 11-18-2012, 09:07 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
MouseAdapter Class?
okay well, im having trouble with this. im suppose to have the smiley face stop when i click it and resume when i click it again, can anybody help?
heres my code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ReboundPanel extends JPanel
{
private final int WIDTH = 300, HEIGHT = 100;
private final int DELAY = 20, IMAGE_SIZE = 35;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY;
//-----------------------------------------------------------------
// Sets up the panel, including the timer for the animation.
//-----------------------------------------------------------------
public ReboundPanel()
{
timer = new Timer(DELAY, new ReboundListener());
image = new ImageIcon ("happyFace.gif");
x = 0;
y = 40;
moveX = moveY = 3;
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setBackground (Color.black);
timer.start();
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
image.paintIcon (this, page, x, y);
}
//************************************************** ***************
// Represents the action listener for the timer.
//************************************************** ***************
private class ReboundListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
x += moveX;
y += moveY;
if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
moveX = moveX * -1;
if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
moveY = moveY * -1;
repaint();
}
}
private class ReboundListener1 extends MouseAdapter{
public void mouseClicked (MouseEvent event)
{
if(event.getButton()==MouseEvent.BUTTON1)
{
timer.stop();
}
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent evt){
if (evt.getButton()==MouseEvent.BUTTON1)
{
timer.start();
}
}
});
}
}
}
- 11-18-2012, 01:37 PM #2
Re: MouseAdapter Class?
Hello and welcome! Please use [code][/code] tags when posting code so we can easily read it!
Forum Rules
Guide For New Members
BB Code List - Java Programming Forum
Similar Threads
-
How to get a compatible class of a template class? Return type of method is AClass<E>
By SKuypers in forum Advanced JavaReplies: 0Last Post: 12-07-2011, 11:55 AM -
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
Query on using MouseAdapter
By halluc1nati0n in forum Java AppletsReplies: 8Last Post: 07-02-2009, 08:04 AM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks