473,765 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can anyone help me fix my program its urgent

1 New Member
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 ");

addMouseListene r(new ReboundListener ());

x = 0;
y = 40;
moveX = moveY = 3;

setPreferredSiz e (new Dimension(WIDTH , HEIGHT));
setBackground (Color.black);
timer.start();
}

//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComp onent (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.get ClickCount();
if(count%2==1)
timer.stop();
if(count%2==0)
{
timer.start();
repaint();
}
}
public void mouseReleased(M ouseEvent event) {}
public void mouseEntered(Mo useEvent event) {}
public void mouseExited(Mou seEvent event) {}
public void mouseDragged(Mo useEvent event){}
public void mousePressed(Mo useEvent event){}
public void mouseMoved(Mous eEvent event){}
}
}
Oct 1 '06 #1
1 1737
D_C
293 Contributor
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
3436
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
6475
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 the socket is missing the last character (line feed). When the same text is sent without the urgent flag set, all of the characters are read. I'm reading the data using the blocking read call of the network stream class. The .NET...
1
1790
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 . base ^ exponent - 1 for example , power (3,4) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than otr equal to 1 . Hint the recursion step would use the relationship base ^ exponent = base . base ^ exponent-1
4
10513
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 doesn't seem to write to the screen in the way I would expect. The output is:
14
2418
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 form.here is my code:(simple code just for testing i did this program) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title...
1
1391
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 tables; in which, in any particular day, when it reaches that time, the system will automatically do some calculation. Now, i need to debug the part of that calculation. But I don't know how the computer time is detected by my program (the program was...
4
1319
by: camarkco | last post by:
Why does C# go out of it's way to prevent this? Mark
3
1685
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 program throughout the week is still hangs. I can restart it immediatly after the hang and it runs fine until next Saturday. There is nothing different about Saturday that I can think of.
2
905
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
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9946
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7371
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6646
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.