473,413 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

can anyone help me fix my program its urgent

1
the following is the programming code,..i need to stop the rebound animation on a click of the mouse and continue its animation on another click
so far I could stop the animation by using timer.stop(), but its not starting back again!!..i cant understand why???
can anyone fix it for me
thanks

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");

addMouseListener(new ReboundListener());

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, MouseListener
{
//--------------------------------------------------------------
// 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();
}
public void mouseClicked (MouseEvent event)
{
int count=0;
count=event.getClickCount();
if(count%2==1)
timer.stop();
if(count%2==0)
{
timer.start();
repaint();
}
}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseDragged(MouseEvent event){}
public void mousePressed(MouseEvent event){}
public void mouseMoved(MouseEvent event){}
}
}
Oct 1 '06 #1
1 1722
D_C
293 100+
You should be able to get by using only boolean click instead of int count.
Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed (ActionEvent event)
  2. {
  3.    if(!click)
  4.    {
  5.       x += moveX;
  6.       y += moveY;
  7.    }
Expand|Select|Wrap|Line Numbers
  1. public void mouseClicked (MouseEvent event)
  2. {
  3.    int count;
  4.    count=event.getClickCount();
  5.    if(count%2==1)
  6.       click = true;
  7.    if(count%2==0)
  8.       click = false;
  9. }
Oct 2 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: dembla | last post by:
Hey Frnds can anyone help me in this i need a program in 'c' PROGRAM to print NxN Matrix 9 1 8 1 2 3 2 7 3 as 4 5 6 6 4 5 7 8 9 in sorted form
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
1
by: ayman723 | last post by:
Hi; I have two problems and I need your help with the code. below is the question. 1- write a recursive function powe ( base, exponent ) that, when invoked , returns base ^ exponent = base ....
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
14
by: ramadeviirrigireddy | last post by:
Hi All, I have the following code for form and servlet. when the form is submitted the servlet will print the values passed by the form. i'm not getting the servlet o/p when i submit the...
1
by: juliacjy | last post by:
Dear all, I'm needing an urgent help. I'm now maintaining a system which has a function of "Automatic Calculation". User is allowed to set of a time which then will be stored in one of my sql...
4
by: camarkco | last post by:
Why does C# go out of it's way to prevent this? Mark
3
by: Microsoft | last post by:
Hi I have a c# program that continually runs 24/7 and performs a variety of tasks based on a timer. There is one routine that hangs every Saturday morning without fail. If I restart the...
2
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I just start to learn C#. Can I run this program from VS.net instead of from DOS. Thanks. Betty using System;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.