How do i fix this code? (plz help drivin me crazy)

guys i am new at this, this is the newest code i did, maybe you guys can help, i was told that it is bad to have 2 keydown methods, and to have the second one in the update class, so maybe you guys can help me find where to put it.
import java.applet.*;
import java.awt.*;
public class Keyboard extends Applet implements Runnable
     // These lines initialize the variables
     // quantities are measured in pixels
     int x_pos = 30;               // x - Position of the ball
     int y_pos = 100;          // y - Position of the ball
     int x_speed = 10;          // initial horizontal speed
     int y_speed = 10;          // initial vertical speed
     int speed_factor = 2;          // overall speed
     int delay = 20;               // msec between animation steps
     int radius = 20;          // radius of the ball
     int appletsize_x = 700;         // horizontal dimension of applet
     int appletsize_y = 400;             // vertical dimension of applet
     static int SPACE_BAR = 32;      // key code for space bar
     int last_key=0;
     // Variables for double buffering
     // used to prevent the animated image from flickering
     private Image dbImage;
     private Graphics dbg;
     public void init()
          setBackground (Color.black);
     public void start ()
          // Make a thread
          // this is part of the program that runs animation
          Thread th = new Thread (this);
          th.start ();
     public void stop()
     public void destroy()
     // This medthod or, function, changes the direction of the ball
     // depending on keyboard input
     public boolean keyDown (Event e, int key)
          last_key = key;
          if (key == Event.LEFT) // the left key was pressed
               x_speed = -1;  // so go left
          else if (key == Event.RIGHT)
               x_speed = 1;   // go right
          else if (key == SPACE_BAR)
               x_speed = 0;   // stop
               y_speed = 0;
          else if (key == Event.UP) // up key pressed
               y_speed = -1;
          else if (key == Event.DOWN) //down key pressed
               y_speed = 1;
          return true;
     public void run ()
          Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
          while (true) // start an infinite loop
               // The following lines test to see if the ball
               // has reached the edge of the screen,
               // and if it has, reverse the direction
               if (x_pos > appletsize_x - radius)
                    x_speed = -1;
               else if (x_pos < radius)
                    x_speed = +1;
               if (y_pos > appletsize_y - radius)
                    y_speed = -1;
               else if (y_pos < radius)
                    y_speed = +1;
               // this line moves the ball
               x_pos += x_speed*speed_factor;
               y_pos += y_speed*speed_factor;               
               repaint();
               try
                    Thread.sleep (delay);
               catch (InterruptedException ex)
                    // do nothing
               Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
     public void update (Graphics g)
          public boolean keyDown (Event e, int key)
               g.setColor (color.green);
               if (key == 98) {
                    g.setColor (Color.blue);
               else if (key == 114)
                    g.setColor (Color.red);
               else if (key == 103)
               g.setColor (Color.green);
               else if (key == 121)
                    g.setColor (Color.yellow);
               else if (key == 112)
                    g.setColor (Color.pink);
               return true;
          // initialize double-buffered image
          if (dbImage == null)
               dbImage = createImage (this.getSize().width, this.getSize().height);
               dbg = dbImage.getGraphics ();
          dbg.setColor (getBackground ());
          dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
          dbg.setColor (Color.red);
          //dbg.drawString("The number is: "+last_key, 20, 20);
          paint (dbg);
          g.drawImage (dbImage, 0, 0, this);
          // draw the ball
          g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}

nah i got past that too, updated this is my code now:
import java.applet.*;
import java.awt.*;
public class Keyboard extends Applet implements Runnable {
     // These lines initialize the variables
     // quantities are measured in pixels
     int x_pos = 30;               // x - Position of the ball
     int y_pos = 100;          // y - Position of the ball
     int x_speed = 10;          // initial horizontal speed
     int y_speed = 10;          // initial vertical speed
     int speed_factor = 2;          // overall speed
     int delay = 20;               // msec between animation steps
     int radius = 20;          // radius of the ball
     int appletsize_x = 700;         // horizontal dimension of applet
     int appletsize_y = 400;             // vertical dimension of applet
     static int SPACE_BAR = 32;      // key code for space bar
     int num=0;
     // Variables for double buffering
     // used to prevent the animated image from flickering
     private Image dbImage;
     private Graphics dbg;
     public void init()
          setBackground (Color.black);
     public void start ()
          // Make a thread
          // this is part of the program that runs animation
          Thread th = new Thread (this);
          th.start ();
     public void stop()
     public void destroy()
     // This medthod or, function, changes the direction of the ball
     // depending on keyboard input
     public boolean keyDown (Event e, int key)
          num = key;
          if (key == Event.LEFT) // the left key was pressed
               x_speed = -1;  // so go left
          else if (key == Event.RIGHT)
               x_speed = 1;   // go right
          else if (key == SPACE_BAR)
               x_speed = 0;   // stop
               y_speed = 0;
          else if (key == Event.UP) // up key pressed
               y_speed = -1;
          else if (key == Event.DOWN) //down key pressed
               y_speed = 1;
          return true;
     public void run ()
          Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
          while (true) // start an infinite loop
               // The following lines test to see if the ball
               // has reached the edge of the screen,
               // and if it has, reverse the direction
               if (x_pos > appletsize_x - radius)
                    x_speed = -1;
               else if (x_pos < radius)
                    x_speed = +1;
               if (y_pos > appletsize_y - radius)
                    y_speed = -1;
               else if (y_pos < radius)
                    y_speed = +1;
               // this line moves the ball
               x_pos += x_speed*speed_factor;
               y_pos += y_speed*speed_factor;               
               repaint();
               try
                    Thread.sleep (delay);
               catch (InterruptedException ex)
                    // do nothing
               Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
     public void update (Graphics g)
          if (num == 98)
               g.setColor (Color.blue);
          else if(num == 114)
               g.setColor (Color.red);
          else if(num == 103)
               g.setColor (Color.green);
          else if(num == 121)
               g.setColor (Color.yellow);
          else if(num == 112)
               g.setColor (Color.pink);
          else
               g.setColor (Color.green);
          // initialize double-buffered image
          if (dbImage == null)
               dbImage = createImage (this.getSize().width, this.getSize().height);
               dbg = dbImage.getGraphics ();
          dbg.setColor (getBackground ());
          dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
          //dbg.setColor (Color.red);
          //dbg.drawString("The number is: "+last_key, 20, 20);
          paint (dbg);
          g.drawImage (dbImage, 0, 0, this);
          // draw the ball
          g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}

Similar Messages

  • I accidentally dropped macbook air that was in a book bag. The keyboard is working because I can see the light but the screen is black and it won't turn off. How should I fix this? Please Help ME!!

    I accidentally dropped my friend's macbook air that was in a book bag. The keyboard is working because I can see the light but the screen is black and it won't turn off. How should I fix this? Please Help ME!!
    I tried to turn it off and it didn't work... and I held on to the shift key too and it still doesn't work..
    Please help me..

    Accidental damage is not covered under Apple warranty.  And it seems there is much accidental damage.  Only a Genius Bar tech looking at it can tell how much it will cost to repair.
    Cost to repair will be high, I suspect (though Genius Bar will confirm/deny.
    There is no gentle way to say this sir/ma'am ... someone will need to pay for your friend's MBA repairs.

  • I downloaded Itunes to my computer today to update my ipod's ios (4.2.1 yes i know im way behind) but when i click check for updates it tells me my ios is the current one. How can i fix this? please help!

    I downloaded Itunes to my computer today to update my ipod's ios (4.2.1 yes i know im way behind) but when i click check for updates it tells me my ios is the current one. How can i fix this? please help!

    and at the same time....
    I guess you can be glad that you at least aren't "way behind"!
    Cheers,
    GB

  • I can't open Safari. It keeps giving an alert that says, "Suspicious Activity might have been detected." How do I fix this?Please help. thanks!

    Safari won't work. It flashes an alert. Alert says that a Suspicious Activity Might Have been Detected. Major Security Issue. How do i fix this? Please help. Thanks!

    You may have installed a variant of the "VSearch" ad-injection malware. Follow Apple Support's instructions to remove it.
    If you have trouble following those instructions, see below.
    Malware is always changing to get around the defenses against it. This procedure works as of now, as far as I know. It may not work in the future. Anyone finding this comment a few days or more after it was posted should look for a more recent discussion, or start a new one.
    The VSearch malware tries to hide itself by varying the names of the files it installs. To remove it, you must first identify the naming pattern.
    Triple-click the line below on this page to select it, then copy the text to the Clipboard by pressing the key combination  command-C:
    /Library/LaunchDaemons
    In the Finder, select
              Go ▹ Go to Folder...
    from the menu bar and paste into the box that opens by pressing command-V. You won't see what you pasted because a line break is included. Press return.
    A folder named "LaunchDaemons" may open. Look inside it for two files with names of the form
              com.something.daemon.plist
    and
               com.something.helper.plist
    Here something is a variable string of characters, which can be different in each case. So far it has always been a string of letters without punctuation, such as "cloud," "dot," "highway," "submarine," or "trusteddownloads." Sometimes it's a meaningless string such as "e8dec5ae7fc75c28" rather than a word. Sometimes the string is "apple," and then you must be especially careful not to delete the wrong files, because many built-in OS X files have similar names.
    If you find these files, leave the LaunchDaemons folder open, and open the following folder in the same way:
    /Library/LaunchAgents
    In this folder, there may be a file named
              com.something.agent.plist
    where the string something is the same as before.
    If you feel confident that you've identified the above files, back up all data, then drag just those three files—nothing else—to the Trash. You may be prompted for your administrator login password. Close the Finder windows and restart the computer.
    Don't delete the "LaunchAgents" or "LaunchDaemons" folder or anything else inside either one.
    The malware is now permanently inactivated, as long as you never reinstall it. You can stop here if you like, or you can remove two remaining components for the sake of completeness.
    Open this folder:
    /Library/Application Support
    If it has a subfolder named just
               something
    where something is the same string you saw before, drag that subfolder to the Trash and close the window.
    Don't delete the "Application Support" folder or anything else inside it.
    Finally, in this folder:
    /System/Library/Frameworks
    there may an item named exactly
                v.framework
    It's actually a folder, though it has a different icon than usual. This item always has the above name; it doesn't vary. Drag it to the Trash and close the window.
    Don't delete the "Frameworks" folder or anything else inside it.
    If you didn't find the files or you're not sure about the identification, post what you found.
    If in doubt, or if you have no backups, change nothing at all.
    The trouble may have started when you downloaded and ran an application called "MPlayerX." That's the name of a legitimate free movie player, but the name is also used fraudulently to distribute VSearch. If there is an item with that name in the Applications folder, delete it, and if you wish, replace it with the genuine article from mplayerx.org.
    This trojan is often found on illegal websites that traffic in pirated content such as movies. If you, or anyone else who uses the computer, visit such sites and follow prompts to install software, you can expect more of the same, and worse, to follow. Never install any software that you downloaded from a bittorrent, or that was downloaded by someone else from an unknown source.
    In the Security & Privacy pane of System Preferences, select the General tab. The radio button marked Anywhere  should not be selected. If it is, click the lock icon to unlock the settings, then select one of the other buttons. After that, don't ignore a warning that you are about to run or install an application from an unknown developer.
    Then, still in System Preferences, open the App Store or Software Update pane and check the box marked
              Install system data files and security updates (OS X 10.10 or later)
    or
              Download updates automatically (OS X 10.9 or earlier)
    if it's not already checked.

  • Almost everytime I go to a new website I will get a pop up that brings me to a new window that says, "Ad server by Dymanet". How do I fix this?! Help is much appreciated!

    Almost everytime I go to a new website I will get a pop up that brings me to a new window that says, "Ad server by Dymanet". How do I fix this?! Help is much appreciated!
    == This happened ==
    Every time Firefox opened
    == Can't remember, it's been a while.

    It sounds like adware. Try this cleaning your computer with this software:
    http://www.malwarebytes.org/

  • Every time I want to download itunes 11, I keep getting a network error. I have tried changing my location but every time, I get a network error. I have been able to install itunes 10.7 without a problem. How do I fix this?? Help!!

    I recently updated my ipod to iOS 7. Now that I have done this, my itunes needs to be itunes 11. I have been able to download itunes 10.7 but now I need an upgrade. Every time I go to update it, I get a "Failed-Network Error" message. I have tried changing my location and networks but I still get the same message. I really need to download itunes 11 inorder for my ipod to work with the itunes. How do I fix this problem?? Help!!!

    I had the same problem and tried restarting my computer. It configured windows updates for a while, then iTunes downloaded successfully. Hope this helps!

  • My ipod touch was interrupted during an update and now my itunes cant recognise the device, and ive tried to restore it but it just keeps coming up with the usb and itunes picture. how do i fix this? Please help

    my ipod touch was interrupted during an update and now my itunes cant recognise the device, and ive tried to restore it but it just keeps coming up with the usb and itunes picture. how do i fix this? Please help

    http://support.apple.com/kb/HT1808

  • Proximity sensor is not working in my iphone 4.how do i fix this problem?help me..

    proximity sensor is not working in my iphone4..whenever i gets call and resting my phone over to my cheeks,it doesn't gets locked..bcoz of this loud speaker gets turn on..how do i fix this problem?

    Same problem. iPhone 4 16 GB

  • HT3529 I have just updasted to the IOS6 and now my Imesasages and FaceTime won't work at all How can I fix this problem Please help I'm deployed and that's how i talk to my loved ones.

    I have just updated my Iphone and Ipad to the newest IOS that Apple has available. And now none of my Imessages work or facetime. How can i fix this?

    Try giving your iPad a reset. Hold down the sleep and home keys for about 20 seconds. When you see the silver apple, let go and let it reboot and try again.
    How are you charging it? its's best charged plugged into the wall  not via USB. So if you're trying via USB, try using the supplied power block plugged into the wall.

  • My Itunes would not update. The I was told to uninstall everything. Now I can not install Itunes, there is an error everytime. How do I fix this? Please help me!

    It started with my itunes not being able to update and it said errors. Then I uninstalled multiple programs the forums told me to do. Still had errors trying to uninstall. Then tried to reinstall, errors. Then tried to go back and restore my computer to a previous time when Itunes was working. It has disapeared. The programs I have tried to "repair" in ADD OR REMOVE PROGRAMS are:
    ITUNES
    APPLE APPLICATION SUPPORT
    APPLE SOFTWARE UPDATE
    BONJOUR
    QUICKTIME
    The error says: The feature you are trying to use is on a network resource that is unavailable.
    I thought I uninstalled all these programs, but they are still there, yet I can't find Itunes to open it. I tried connecting my Ipod to see if Itunes would open and it won't.
    The only thing I can think of is to wipe out my whole computer to the basics and start over?!?!
    I HAVE NO IDEA HOW TO FIX THIS AND I CAN'T FIND A SOLUTION ON THESE FORUMS. HELP ME PLEASE!!!!

    I fixed it! Here's how:
    I downloaded MICROSOFT FIX IT PROGRAM (IT'S FREE) and I REALLY RECOMMEND IT!
    http://support.microsoft.com/fixit/
    IT FIXED MY ERRORS FOR INSTALLING AND UNINSTALLING. I WAS NOT ABLE TO PROPERLY UNINSTALL ITUNES/BONJOUR ECT.
    THEN I REINSTALLED AND IT WORKED!
    I have trying to updat my itunes for almost a year I think. So I am excited!!!!

  • How could I fix this code

    Hii 2 all
    How could I change this statement for parameters list,, so the client_name can contain spaces ??
    add_parameter(p_id,'p_client_name',TEXT_PARAMETER, '''' || :cntrl.client_name || '''');
    cause it couldn't print and give syntax error when the :cntrl.client_name contains any spaces !!!!
    please help
    thanks:)

    First I must apologize, I asked you to test something that was not needed... for a moment I thought we were talking about web.show_document, that's why I asked you to test with double-quotes..
    Regarding your problem, Manu is right, there is not need to wrap around the parameter in quotes..
    add_parameter(p_id,'p_client_name',TEXT_PARAMETER, :cntrl.client_name);I have to ask you the same question that Manu asked you before, where are you using this parameter?
    In your Report you must be using in a piece of PL/SQL code, we need you to post that code...
    Edited by: Rodolfo Ferrari on Sep 10, 2009 4:44 PM

  • HT4623 I updated to ios7 but no longer can I print. How do I fix this problem? HELP,

    I updated to ios7, but now cannot use my printer. How can I solve this problem?

    Hey Miichaelb62,
    I found some basic troubleshooting steps for you to try first:
    Verify that the printer:
    Has power and is turned on. If it has power, turn the printer off and then on again to see if that resolves the issue.
    Is connected to the same Wi-Fi network as the iOS device.
    Has the latest firmware version installed.
    Has paper with enough ink or toner installed.
    Is free of any errors displayed on the printer's control panel.
    If necessary, consult the printer documentation or contact the printer vendor for help with the above steps.Notes:
    AirPrint printers connected to the USB port of the Apple AirPort Base Station or Time Capsules are not supported with AirPrint.
    Some Wi-Fi AirPrint printers can take several minutes to join a Wi-Fi network after being turned on. Ensure that the AirPrint printer is connected to the Wi-Fi network before attempting to print.
    via: AirPrint Basics
    http://support.apple.com/kb/HT4356
    Welcome to Apple Support Communities!
    Take care,
    Delgadoh

  • How do I fix this, Please someone help

    No matter what website I go to I get an content encoding error, and when I do go to a website and don't recieve that message the page will download but not all contents are there...This has never happened before. How do I solve this?

    Clear the cache and the cookies from sites that cause problems.
    * "Clear the Cache": Tools > Options > Advanced > Network > Offline Storage (Cache): "Clear Now"
    * "Remove the Cookies" from sites causing problems: Tools > Options > Privacy > Cookies: "Show Cookies"

  • How can I fix this code ?

    hello,
    I have a small problem with this code:
    #include  <fstream>
    #include  <iostream>
    #include  <algorithm>
    #include  <iterator>
    #include  <string>
    #include  <vector>
    using namespace std;
    int main()
         string from, to;
         cin >> from >> to;
         ifstream is(from.c_str());
         istream_iterator<string> ii(is);
         istream_iterator<string> eos;
         //  line 24: Error:
         vector<string> b(ii, eos);  //  line 24: Error:
         sort(b.begin(), b.end());
         ofstream os(to.c_str());
         ostream_iterator<string> oo(os, "\n");     
             unique_copy(b.begin(), b.end(), oo);     
             return !is.eof() && !os;          
             return 0;
    }When I compile this happens
    bash-2.05$ CC -V
    CC: Sun C++ 5.6 2004/06/02
    bash-2.05$ CC -o std_vector.out std_vector.cpp
    "std_vector.cpp", line 24: Error:
    Could not find a match for
    std::vector<std::string>::vector(std::istream_iterator<std::string, char, std::char_traits<char>, int>, std::istream_iterator<std::string, char, std::char_traits<char>, int>)needed in main().
    1 Error(s) detected.
    What can I do ?
    best regards
    Morten Gulbrandsen

    You have run into a documented limitation of the default libCstd implementation of the C++ standard library. The default library is not a complete implementation, and in particular lacks required template member functions that allow implicit type conversions. We still use this library for binary compatibility with earlier releases.
    The optional STLport version of the library is a full implementation of hte C++ Standard Library. If you don't need binary compatibility, meaning you don't need to link to code that uses libCstd, you can add the option -library=stlport4 to every CC command line, compiling and linking. Your sample code compiles with STLport.
    If you need to use libCstd, you might get this code to work by inserting explicit type conversions. I have not tried to see whether that actually can be done.

  • Trying to change my DNS to a US address to unlock Netflix however there is no save prompt? How do I fix this? Please help!

    How do I get the DNS to save when there is no save option?. The address will not take effect and keeps changing back to the original. Thanks so much for any help with this!

    I fixed it! Here's how:
    I downloaded MICROSOFT FIX IT PROGRAM (IT'S FREE) and I REALLY RECOMMEND IT!
    http://support.microsoft.com/fixit/
    IT FIXED MY ERRORS FOR INSTALLING AND UNINSTALLING. I WAS NOT ABLE TO PROPERLY UNINSTALL ITUNES/BONJOUR ECT.
    THEN I REINSTALLED AND IT WORKED!
    I have trying to updat my itunes for almost a year I think. So I am excited!!!!

Maybe you are looking for