The timer clash wif the actionperformed of the clicked button, hw to solve

// This program demo on the followings:
// 1. To create a JFrame
// 2. To create JLabel and JButton.
// 3. The use of TTimer to automatically update the position of a JLabel
// every 2 seconds
// To learn more about Swing, go to this website:
// http://java.sun.com/docs/books/tutorial/uiswing/index.html
// There's a lot of demo program in this tutorial:
// Down the JFC/Swing Trail tutorial from:
// http://java.sun.com/docs/books/tutorial/information/download.html
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class TimerDemo3 extends JPanel implements ActionListener
     private static final int origX = 1700; //starting position of the shark
private int posX; //position of the small picture,
private int posX3;
private int posX4;
private int posX5;
private int posY; //to be updated every second
     private int posY2;
     private int posY3;
     private int posY4;
     private int posY5;
JLabel label1; //will be loaded with the hunter picture
JLabel label2; //will be loaded with the sharks picture
JLabel label3;
JLabel label4;
JLabel label5;
JButton buttonQuit;
JLabel label10;
     JLabel label11;
     JTextField name;
     JLabel ans;
     JButton enterButton;
     JLabel label;
ImageIcon icon1;
ImageIcon icon2;
ImageIcon icon3;
ImageIcon icon4;
ImageIcon icon5;
ImageIcon sharkImage;
Timer timer;
int delay = 100; // 1000 milliseconds = 1 seconds.
Timer timer2;
int delay2 = 300; // 3000 milliseconds = 3 seconds.
int noKilled = 0;
int i = 1;
public TimerDemo3() {
          // Create the 3 icons
icon1 = new ImageIcon("pic/hunter.jpg");
          icon2 = new ImageIcon("pic/shark2.jpg");
          icon3 = new ImageIcon("pic/shark3.jpg");
          icon4 = new ImageIcon("pic/shark4.jpg");
          icon5 = new ImageIcon("pic/shark2.jpg");
setLayout(null);
setBackground(Color.blue);
//Create and add the 2nd, 3rd .... label.
label2 = new JLabel(icon2);
          label2.setBounds(origX, 100, 250, 90);
          posX = origX;
          posY = 100;
          add(label2, null);
label3 = new JLabel(icon3);
          label3.setBounds(origX, 100, 250, 90);
          posX = origX;
          posY = 100;
          add(label3, null);
          label4 = new JLabel(icon4);
          label4.setBounds(origX, 100, 250, 90);
          posX = origX;
          posY = 100;
          add(label4, null);
          label5 = new JLabel(icon5);
          label5.setBounds(origX, 100, 250, 90);
          posX = origX;
          posY = 100;
          add(label5, null);
//Create and add the first label.
label1 = new JLabel(icon1);
          label1.setBounds(20,-390, 80, 390);
          posY2 = -390;
          add(label1, null);
          // create and place 1 buttons on the frame's content pane
          enterButton = new JButton ("ENTER");
enterButton.setBounds (250,675, 80, 30);
add (enterButton);
          // register this frame as an action listener of the 1 buttons
          enterButton.addActionListener (this);
          // Add label, text field and action listener
          ans = new JLabel ();
          ans.setText ("Please type the word");
          ans.setBounds (85,650,150,25);
          add (ans);
          name = new JTextField();
          name.setBounds (90,680,130,25);
          add (name);
          label10 = new JLabel("Score");
          label10.setBounds(700,650, 100, 30);
          add(label10, null);
          label11 = new JLabel("Mega Harpoon:");
          label11.setBounds(750,650, 100, 30);
          add(label11, null);
//Create and add the OK button.
          buttonQuit = new JButton("Quit");
          buttonQuit.setBounds(900, 675, 60, 30);
          buttonQuit.setActionCommand("Quit");
buttonQuit.addActionListener(this);
//Add button on the panel
          add(buttonQuit, null);
          //Set up a timer that calls this object's action handler.
          //For more info: http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
timer = new Timer(delay, this);
timer.start();
          //Set up a timer that calls this object's action handler.
          //For more info: http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
timer2 = new Timer (delay2, this);
timer2.start();
public void actionPerformed(ActionEvent e)
          // If this action is not activated by the button, but by the timer.
          // It will update the position of the picture
          posX -= 5;//for every 0.1 sec, the shark wil move 5 pixel.
          if (posX < 100)//the boundary that the shark meets
          posX = origX;// the shark return to origX position.
          posY = 100;
          label2.setBounds(posX, posY, 250, 90);
          posX3 -= 5;
          if (posX3 < 100)
          posX3 = 2300;
          posY3 = 500;
          label3.setBounds(posX3, posY3, 250, 90);
          posX4 -= 5;
          if (posX4 < 100)
          posX4 = 2500;
          posY4 = 300;
          label4.setBounds(posX4, posY4, 250, 90);
          posX5 -= 7;//every 0.1s, shark wil move 7 pixel forward
          if (posX5 < 100)// if the shark reach the boundary of 100, it will return
          posX5 = 2100;// return to the position
          posY5 = 400;
          label5.setBounds(posX5, posY5, 250, 90);
          // If this action is not activated by the button, but by the timer.
          // It will update the position of the picture
          if (posY2 < 0)
               posY2 += 3;//for every 0.1 sec, the hunter wil move 3 pixel.
               label1.setBounds(20, posY2, 80, 390);
          noKilled ++;
          if (noKilled > 10){
          timer2.setDelay(200);
          else if (noKilled > 20){
          timer2.setDelay(100);
          /*if (e.getSource== buttonQuit){
               Toolkit.getDefaultToolkit().beep();
               JButton clickedbutton = (JButton)e.getSource();
                    if (clickedbutton == enterButton)
                              String nameString = name.getText(); // get textbox value
                              String ansString = ans.getText(); // get label value
                              if ((nameString.equals(ansString)) == true)
                                        sharkImage = new ImageIcon("pic/shark2.jpg");
                                        i = i++;
                                        String k = "" + i;
                                        label11.setText(k);
                              else
                                        label3.setLocation(getX() + 5 , getY());
                                        label.setText("error, please try again ");
//     EnterButton.addActionListener(new ActionListener(
//          public void actionPerformed(ActionEvent f)
//      EnterButton_actionPerformed(f);
     public void EnterButton_actionPerformed(ActionEvent f)
     String name = inputLine.getText(); // get textbox value
     String ans = labelinput.getText(); // get label value
     if (name.equalsTo(ans)) = true
          sharkImage= "deadshark";
          i = i +1;
          harpoon.setText(i);
     } else{
          shark.setLocation(getX() + 5 , getY());
          label.setText("error, please try again ");
     public static void main(String[] args)
               //Create and set up the window.
          JFrame frame = new JFrame("MAD HUNTER");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setContentPane(new TimerDemo3());
               frame.setSize(1024, 768);
               frame.setLocation(0, 0);
          //Display the window.
          frame.setVisible(true);      

I'm not a big fan of subclassing components so that you can implement interfaces.
Here are two reasons why it's a bad idea:
1. Generally, the types of an object are its surface area, its contract with the world, to mix
metaphors. If your panel subclass implements ActionListener that means that I'm allowed
to add it as a listener of some of my buttons, too? I don't think that's your design. That fact that
you need to implement ActionListeners is purely an implementation detail.
2. If an object is an ActionListener for different, unrelated reasons, you're forced to do a
case analysis in the actionPerformed method. I thought Lincoln freed all the slaves and that
OOP freed us from that kind of case analysis.
What should you do instead? Write separate classes that implement ActionListener. If
those classes are reusable or only loosely coupled to the panel, make them top level classes.
If they are inseparable from your panel, they can be inner classes.

Similar Messages

  • I have rented a movie on my ipad. It did not download due to not being on wifi at the time.  I know I completed the rental because my gift card balance went down $3.99 plus tax.  How do I find where to download the movie?

    I have rented a movie on my ipad. It did not download due to not being on wifi at the time.  I know I completed the rental because my gift card balance went down $3.99 plus tax.  How do I find where to download the movie?

    But now I can't find the actual movie...I go to iTunes to purchases and view movies , to downloads and searched the title of the movie...I can see an icon of it and it is in a separate place from other purchases but it says downloaded in faded lettering. Where is it?
    If you can't find the movie you downloaded on your iPad, iPhone or iPod Touch, check inside your Videos App

  • I have Elements 11. Sometimes, if I edit a photo in Elements Editor, the date gets changed to the current date. Why does that happen? It doesn't happen all the time, so I can't isolate the cause. Any ideas?

    I have Elements 11. Sometimes, if I edit a photo in Elements Editor, the date gets changed to the current date. Why does that happen? It doesn't happen all the time, so I can't isolate the cause. Any ideas?

    I have Elements 11. Sometimes, if I edit a photo in Elements Editor, the date gets changed to the current date. Why does that happen? It doesn't happen all the time, so I can't isolate the cause. Any ideas?

  • WHEN I GO TO PRIVACY SETTINGS AND CLICK ON EXCEPTIONS AND TYPE IN A WEB ADDRESS TO ALLOW ALL THE TIME IT DOES NOT SAVE IT THE NEXT TIME I SIGN ON TO FIRE FOX IT IS LOST. IT WORKS FINE ON MY DESKTOP BUT NOT ON MY LAPTOP THAT I JUST BOUGHT in English.

    Question
    WHEN I GO TO PRIVACY SETTINGS AND CLICK ON EXCEPTIONS AND TYPE IN A WEB ADDRESS TO ALLOW ALL THE TIME IT DOES NOT SAVE IT THE NEXT TIME I SIGN ON TO FIRE FOX IT IS LOST. IT WORKS FINE ON MY DESKTOP BUT NOT ON MY LAPTOP THAT I JUST BOUGHT in English.

    I just updated my whatsapp...clicked on whatsapp in appstore and now can access my whatsapp

  • Alert message is needed at the time of creation ,if it reaches ,the maximu

    DEAR SA EXPERTS,
    Can you please me that alert message is needed at the time of creation ,if it reaches ,the maximum stock level (Stock+level)
    Thanks
    Mohit

    Hi,
    Please refer the below links.
    Alert Message
    Re: Alert Message
    Hope it helps you.
    Thanks.

  • Discount Condition with effect at the time of GR but not at the time of PO

    Dear All
    My customer requires a discount condition in a particular pricing procedure. He want the discount condition in such a way that it should be shown in PO conditions but it should not affect the net price while making purchase order. Rather it should be deducted at the time of GR.
    For example: i am making a PO for a material with net price 305Rs and discount given by the vendor is 5Rs per unit. In the PO discount condition should appear with 5Rs per KG/Litre/etc. But the PO net price should remain 305 instead of 300.
    Now when i am making a Goods Receipt at that time the discount should be calculated on the net price 305 and material should be loaded with price 300.
    Kindly help me to resolve the issue by suggesting some feasible options.
    Thank you.

    Dear Sandesh
    i want a discount condition type (Quantity wise i.e. 5 Rs/Liter) which will not affect the PO net price i.e. net rpice remains same irrespective of the discount condition value. Discount should be applicable at the time of GR i.e in material should be loaded with price after discount.
    For Ex: if the discount entered in PO is 5 Rs/Liter
    PO Amount: Quantity (100) * Net price (105) = 10500 (should not include the discount at the time of PO)
    GR amount: Quantity (100) * Net price (105-5=100) = 10000 (should include the discount at the time of GR)
    Hope you understand the issue.

  • I have had my iphoto crash twice whilst nearing the end of making a photo book. Are these books lost and do I have to start again? ):  I cant find the folder in iphoto on the time capsule. Folder from which the book was being made is now empty? help pleas

    I have had my iphoto crash twice whilst nearing the end of making a photo book. Are these books lost and do I have to start again? ):  I cant find the folder in iphoto on the time capsule. Folder from which the book was being made is now empty? help please Using IMAC OSx

    Hi shldr2thewheel,
         it has been a while since we have last spoke, I would like to let you know, I am still working on getting used to the switch from windows to a Mac/Apple system. I do have a new question for you, I did purchase In Design CS5.5 through journeyed.com through Cuyahoga Community College of which I attend as a student, is there a way to purchase an online book through iTunes to learn that as well? Also, you know me, the struggling student, I would also, when and if the book can be purchased through the iTunes, would need to know if you do know of a much easier book for struggling students like myself and at a reasonable price as well for the In Design CS5.5 program. Our campus bookstore had closed early, and, so did the colleges library and our local library here where I do live, so, I cannot go to either place to purchase a book or to take out a book, plus cash funds are low at this moment as well but, I do have money left on the iTunes account to use, if it can be used. So, can it be used, the iTunes money, towards finding a low priced online book? I am in great need of assistance as I have a project due for my one course for this Tuesday, September 4, 2012.
    Sincerely in need of help once again,
    Kim

  • TDS DEDUCTING TWICE AT THE TIME OF ADVANCE POSTING AND AT THE TIME OF INVOg

    HI ALL
    I HAVE AN ISSUE TDS DEDUCTING TWICE AT THE TIME OF ADVANCE POSTING AND AT THE TIME OF INVOICE POSTING
    AT THE TIME OF ADVANCE POSTING F-48
    ENTRY IS
    VENDOR     DR     30000
    BANK          CR     2900
    TDS          CR     300
    AT THE TIME OF INVOICE POSTING-FB60
    THE ENTRY IS
    EXPENSE     DR     30000
    VENDOR     CR     29700
    TDS          CR     300
    SO TDS HAS BEEN DEDUCTED TWO TIMES
    AT THE TIME OF DOWN PAYMENT CLEARING u2013F-54
    THE INVOICE ENTRY SHOULD BE REVERSED AS
    VENDOR     DR     29700
    TDS          DR     300
    VENDOR SPL CR      30000
    BUT IT IS NOT REVERSING WHILE CLEARING DOWN PAYMENT AT F-54
    FOR THIS I MADE THE CONFIGURATION FOR DOWN PAYMENT IN WITH HOLDING TAX TYPE FOR PAYMENT.HERE I ACTIVATED u201CCENTRAL INVOICE PROu201D
    THOUGH IT IS NOT REVERSING THE ENTRY OF INVOICE POSTING AT THE TIME OF DOWN PAYMENT CLEARING AT F-54.
    SO WHAT COULD BE THE PROBLE . IF YOU KNOW PLEASE TELL ME THE SOLUTION
    I WILL BE GREATE THANKS TO YOU
    REGARDS
    RAM

    Hi All
    I Have an issue with TDS.
    At the time of down payment clearing th invoice amount is not reversing..what are the settings I need to check
    Actually
    At the time of Advance Posting the entry is----
    VENDOR     DR     30000
    BANK          CR     2900
    TDS          CR     300
    At the time of Invoice Posting  theentry is -
    EXPENSE     DR     30000
    VENDOR     CR     29700
    TDS          CR     300
    At the time of Down Payment Clearing  in t code F-54 the invoice entry should be reversed as -
    VENDOR     DR     29700
    TDS          DR     300
    VENDOR SPL CR      30000
    But system not reversing while clearing down payment in t code F-54 and the system posting entry is as----
    VENDOR DR 30000
    VENDOR SPL   30000
    So what is the mistaken has been done by me .If you any idea please let me know. I will be thankful to you.
    Thanks&Regards
    Ram

  • I plugged my ipod into my computer to load up new tunes and it froaze on the time I dad connected it with the message 'do not disconnect' on the screen.Despite disconnecting and reconnecting it and ensuring it was charged up it will not operate. The 'lock

    I plugged my ipod into my computer to load up new tunes and it froaze on the time I dad connected it with the message 'do not disconnect' on the screen. Despite disconnecting and reconnecting it and ensuring it was charged up it will not operate. The 'lock' symbol is showing at the top of the screen even when I have moved the lock switch. I have been using my ipod for several years with no similar fault. What is the problem/solution?

    I would first try resetting the iPod:
    Reset iPod touch:  Press and hold the On/Off Sleep/Wake button and the Home
    button at the same time for at least ten seconds, until the Apple logo appears.
    Next would be to restore the iPod from backup

  • How can I fix the long delay (15 min  ) between the time I select a song to the time it begins playing?

    How can I fix the lond delay (15 min.) between the time I select a song to the time it begins playing?

    Hi Ben,
    This is a common issue that NI LabVIEW does not address.
    The good news is that OpenG provides a function to do just what you need.
    See this article: Resize Front Panel to largest decoration
    The way to get the OpenG functions is to use the JKI VI Package Manager (VIPM).
    In addition to the OpenG functions there are several other community provided tools avaliable through VIPM.
    For a quick test, I'm attaching these files (LV version 2010 sp1):
    Fit VI window to Largest Dec__ogtk.vi and the required sub vi Current VIs Parents Ref__ogtk.vi.
    You should really use VIPM to get the full OpenG library - there are many gems in there.
    steve
    Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
    question. Give "Kudos" to replies that help.
    Attachments:
    Fit VI window to Largest Dec__ogtk.vi ‏29 KB
    Current VIs Parents Ref__ogtk.vi ‏9 KB

  • When I start my MacBook Air, I get a problem report on the Time Capsule 802.11n. When I click Continue, it asks for a password. I don't know what the password is because I didn't set this up. How can I find out what the password is?

    When I start my MacBook Air, I get a problem report on the Time Capsule 802.11n. When I click Continue, it asks for a password. I don't know what the password is because I didn't set this up. How can I find out what the password is?

    Who set it up?

  • When I enter time machine (on Time Capsule) i see the stack of screen shots and the time line. However, when I roll over the mouse pointer, the time line does not activate. The cancel button does not get me out of the app: have to alt cmd esc. Ideas?

    When I enter time machine (on Time Capsule) i see the stack of screen shots and the time line. However, when I roll over the mouse pointer, the time line does not activate. The cancel button does not get me out of the app: have to alt+cmd+esc. Ideas?

    I have never seen it but then I run SL which is much more reliable than Lion..
    See
    http://pondini.org/TM/E4.html
    Check the master guru of all TM problems.

  • HT1449 I moved my itunes library to the time capsule. Everytime I put the computer to sleep, I have to change the location back to the time capsule. I cannot get the iTunes library to stay located in the time capsule.

    My itunes library does not stay in its new location in the time capsule. Every time I put the computer to sleep or leave it alone for awhile, the itunes location goes back to the original location in the macbook. Then I have to change the location back to the time capsule and wait for it to reorganize all the files again. So the question is how to keep my itunes in its new location the time capsule?

    Welcome to the discussion area!
    +Can someone please provide a definite answer, or at least warn against using the TC as a shared media device on a network. The whole point of getting such a device is to be able to offload large multimedia libraries from the laptops, yet access a shared library when networked. N'est ce pas?+
    We're all users here...not pros...so you'll get few definitive answers on a forum with varying opinions about almost every subject.
    My opinion would be this: Apple designed the Time Capsule for Time Machine to automatically backup all the files on your Mac and other Macs in the household on a regular basis and provide a way to retrieve a file that had been deleted recently by mistake. It's a pretty good router as well.
    If you check the info about the Time Capsule on Apple's site, the main features mentioned are backup of your Macs and a dual band wireless network capability.
    The Time Capsule was never designed to be a media server housing audio, video and other multi-media files and has never been promoted as such. Nevertheless, some users seem to have adopted the stance that this should be one of the benefits of the device, if not a main feature.
    There may be a need for media server type device, but the Time Capsule, at least at this time, is not the answer. If you feel otherwise, you might want to speak directly to Apple and let them know via Time Capsule Feedback.

  • How to remove the time slider from my project and keep only buttons

    How to remove the time slider from my project and keep only buttons to interact with my project .

    If I understand you correctly, go to the skin editor (Project, Skin Editor in Captivate 4), select the Playback Control tab, uncheck the Progressbar check box.  Leave checked the buttons for play/pause, rewind, forward, back, etc.
    I hope this helps.
    Mister C.

  • Can the time machine be accessed remotely over the internet?

    Hi, I am thinking about purchasing the time machine. Can the hard drive on the time machine be accessed remotely over the internet?

    Do you realize that if you store some of your Mac's data (photos, music, etc.) ONLY  on the external Time Capsule, AND also  store your "Time Machine" app "system backups" on the Time Capsule,then if the Time Capsule hard drive ever fails you will have lost the " original" and the "backup" copies of all that data  (photos, music, etc.)?  They will be gone forever.  NOT recommended!

Maybe you are looking for

  • Can't print with adhoc network for HP 7780 all-in-one

    Operating system is Windows Vista 32 bit. Downloaded full driver software from HP site and installed it OK with wired connection to printer.  All works fine in this mode. Now I want to do wireless printing via an adhoc network called MYNetwork that I

  • Error when launching External WBT

    Hello experts, Frequently, when launching Ext WBT courses-- we receive the following error: Server Response External LMS    External Training Provider  9000 Service ID  01 External Course ID  98406 Status  FAILURE The issue seems to be sporadic as of

  • Sound stream played too fast

    hello, i'm trying to stream audio byte[] data over multicast network (UDP). on the receiving end, the sample is played way too fast. CMIIW, this has nothing to do with networking stuff, but an audio playback problem. can someone give a hint? here's t

  • G4 PowerMac Duel Monitors

    Need some information, if you please. My G4 tower came with two plug-ins to accomodate two monitors.  I still have the Apple connection for VGA that came with the Mac, but I need the second option (male).  The male end has 30 Pins and 2 tiny pins on

  • Hi Gurus, To Add New key figure in BW 3.5 flow ODS- Infoset

    Hi Gurus , I have rquerimennt to add a new  keyfigure in ODS and then into  Infoset .  The histrorical data for this keyfigure is not needed . Can you please let me know what the are changes needed to be done in Transfer structure -> cs->updaterules-