JAVA arithmetic problem with double ?

If I try to work double precision numbers I get a unintelligible result, but what do I do wrong ?
Are there any inaccuracy in the JAVA double precision numbers arithmetic or what's the problem with this very simple example code ? How can I get correct result ?
Simple example code :
================
for (double i = 0; i < 10; i = i + 0.01)
System.out.println(i);
On the output :
===========
0.0
0.01
0.02
0.03
0.04
0.05
0.060000000000000005
0.07
0.08
0.09
0.09999999999999999
0.10999999999999999
0.11999999999999998
0.12999999999999998
0.13999999999999999
0.15
0.16
0.17
0.18000000000000002
0.19000000000000003
0.20000000000000004
0.21000000000000005
0.22000000000000006
0.23000000000000007
0.24000000000000007
0.25000000000000006
0.26000000000000006
0.2700000000000001
0.2800000000000001
0.2900000000000001
0.3000000000000001
0.3100000000000001
0.3200000000000001
0.3300000000000001
0.34000000000000014
0.35000000000000014
0.36000000000000015
0.37000000000000016
0.38000000000000017
0.3900000000000002
0.4000000000000002
0.4100000000000002
0.4200000000000002
0.4300000000000002
0.4400000000000002
0.45000000000000023
0.46000000000000024
0.47000000000000025
0.48000000000000026
0.49000000000000027
0.5000000000000002
0.5100000000000002
0.5200000000000002
0.5300000000000002
0.5400000000000003
0.5500000000000003
0.5600000000000003
0.5700000000000003
0.5800000000000003
0.5900000000000003
0.6000000000000003
0.6100000000000003
0.6200000000000003
0.6300000000000003
0.6400000000000003
0.6500000000000004
0.6600000000000004
0.6700000000000004
0.6800000000000004
0.6900000000000004
0.7000000000000004
0.7100000000000004
0.7200000000000004
0.7300000000000004
0.7400000000000004
0.7500000000000004
0.7600000000000005
0.7700000000000005
0.7800000000000005
0.7900000000000005
0.8000000000000005
0.8100000000000005
0.8200000000000005
0.8300000000000005
0.8400000000000005
0.8500000000000005
0.8600000000000005
0.8700000000000006
0.8800000000000006
0.8900000000000006
0.9000000000000006
0.9100000000000006
0.9200000000000006
0.9300000000000006
0.9400000000000006
0.9500000000000006
0.9600000000000006
0.9700000000000006
0.9800000000000006
0.9900000000000007
1.0000000000000007
1.0100000000000007
1.0200000000000007
1.0300000000000007
1.0400000000000007
1.0500000000000007
1.0600000000000007
1.0700000000000007
1.0800000000000007
1.0900000000000007
1.1000000000000008
1.1100000000000008
1.1200000000000008
1.1300000000000008
1.1400000000000008
1.1500000000000008
1.1600000000000008
1.1700000000000008
1.1800000000000008
1.1900000000000008
1.2000000000000008
1.2100000000000009
1.2200000000000009
1.2300000000000009
1.2400000000000009
1.2500000000000009
1.260000000000001
1.270000000000001
1.280000000000001
1.290000000000001
1.300000000000001
1.310000000000001
1.320000000000001
1.330000000000001
1.340000000000001
1.350000000000001
1.360000000000001
1.370000000000001
1.380000000000001
1.390000000000001
1.400000000000001
1.410000000000001
1.420000000000001
1.430000000000001
1.440000000000001
1.450000000000001
1.460000000000001
1.470000000000001
1.480000000000001
1.490000000000001
1.500000000000001
1.5100000000000011
1.5200000000000011
1.5300000000000011
1.5400000000000011
1.5500000000000012
1.5600000000000012
1.5700000000000012
1.5800000000000012
1.5900000000000012
1.6000000000000012
1.6100000000000012
1.6200000000000012
1.6300000000000012
1.6400000000000012
1.6500000000000012
1.6600000000000013
1.6700000000000013
1.6800000000000013
1.6900000000000013
1.7000000000000013
1.7100000000000013
1.7200000000000013
1.7300000000000013
1.7400000000000013
1.7500000000000013
1.7600000000000013
1.7700000000000014
1.7800000000000014
1.7900000000000014
1.8000000000000014
1.8100000000000014
1.8200000000000014
1.8300000000000014
1.8400000000000014
1.8500000000000014
1.8600000000000014
1.8700000000000014
1.8800000000000014
1.8900000000000015
1.9000000000000015
1.9100000000000015
1.9200000000000015
1.9300000000000015
1.9400000000000015
1.9500000000000015
1.9600000000000015
1.9700000000000015
1.9800000000000015
1.9900000000000015
2.0000000000000013
2.010000000000001
2.020000000000001
2.0300000000000007
2.0400000000000005
2.0500000000000003
Thanks for answerers !
Joseph

This result is entirely unsurprising, and the flaw lies in your program, not in Java.
Java represents numbers in 2's complement [SEM] form (read the Java Language Specification (JLS), according to agreed standards. When you specify a fractional number that appears to be nice and easy to work with, you are thinking in decimal notation, not binary, and you are giving it a number that cannot be precisely represented. It is a little like asking you to write down exactly what pi is (all its digits ;-)).
So the number you get is not precisely what you asked for (one very insignificant error). But then you compound it by adding an inaccurately represented number to an inaccurately represented number, and so on, until the bit errors combine to make a very inaccurate number.
The correct way to approach what you are trying to do would be to perform the addition using integers, and multiply each time you want to scale it to a double.
This has been asked many times - I would suggest you:
- Search the forums
- Check out the language spec
- or do a degree in CompSci :-)
A calculator is a little more accurate because it does not have to conform to agreed standards, and so can use special logic to minimise such errors. The average pocket calulator also works to a much lower level of accuracy (usu 8 digits), and therefore masks the errors to you. You'd find that you got the same effect if you played with it long enough.
Hope this helps

Similar Messages

  • Java/OpenJDK problem with OSS/osspd/ALSA/pulseaudio [SOLVED]

    I've got problems with sound output of java programs, which usually try to hog /dev/dsp, using pulseaudio and openjdk 7.
    Some rare java apps' sound methods surprisingly do work. Others (which the majority of java programs seem to use) do not. In Sun Java I could make those work by using 'padsp', however this method apparently fails on Archlinux with both latest Oracle Java (formerly worked on Ubuntu w/ Sun Java 6) and with OpenJDK too.
    So I tried 'osspd' (package is called 'ossp') and it shows the root process "/usr/sbin/osspd --dsp-slave=/usr/sbin/ossp-padsp" been started, but the java applications will still not provide sound which I find pretty odd. I tried adding 'soundcore.preclaim_oss=0' to the 'kernel..' line in my grub's menu.lst, but that didn't help either.
    Last edited by Jindur (2012-05-20 02:59:20)

    It's been quite a while, but I finally found a solution (read: big ugly hack).
    Someone literally created a biguglyhack and posted it here:
    http://lifein19x19.com/forum/viewtopic. … 243#p98243
    (downloadable file there: javadummymixer_biguglyhack.zip [12.97 KiB] -> rename it from .zip to .jar and move it into your java's lib/ext folder -> profit!)
    I applied it to OpenJDK 6 (should be same for 7 though) and all my java sound works flawlessly now, with pulseaudio, without any need for padsp stuff or osspd or whatever.
    My sound.properties file reads:
    javax.sound.sampled.Clip=com.sun.media.sound.DirectAudioDeviceProvider
    javax.sound.sampled.Port=com.sun.media.sound.PortMixerProvider
    javax.sound.sampled.SourceDataLine=com.sun.media.sound.DirectAudioDeviceProvider
    javax.sound.sampled.TargetDataLine=com.sun.media.sound.DirectAudioDeviceProvider
    but actually I'm not sure whether that even matters at all.
    (Side note: The linked thread also mentions a "-D.sun.." command-line parameter that supposedly forces java to avoid disfunctional audio methods. The above biguglyhack however worked for me without any need of applying that command-line parameter.)
    Last edited by Jindur (2012-05-20 02:58:05)

  • Java debugging - problems with standard input

    Hi
    I hope somebody can help me with this problem.
    I'm developing a Java app that reads from the standard input. I use System.in.read() to do that. When I debug it, I use the Standard I/O window instead of the Run Log window to pass in values, but nothing happens (i.e. the line following the read() is never reached). I also tried to run the app and then attach the debugger to use the Run Log window as standard input, with the same result. If I add a breakpoint before the call to read(), the debugger pauses the execution, so I suppose the breakpoints are OK.
    In the executable Info window, Debugging tab, I use the pseudo terminal with the Java debugger.
    So my question is: how can I enter values from the standard input in debugging mode?

    I believe that you are seeing these problems because we have found the
    current versions of RMI found in the JDK to be inherently unscalable -- we
    do not listen for them. As you noticed, we recommend that you use our
    packages for now, and then do the simple change that you found if you decide
    that you do not like WLS. (If you do decide that you do not like us, please
    definitely let us know why you chose the way you did!)
    Thanks,
    Michael
    Michael Girdley
    Product Manager, WebLogic Server & Express
    BEA Systems Inc
    "Tim Dawson" <[email protected]> wrote in message
    news:397b8cc2$[email protected]..
    We're developing a J2EE application on NT using WLS 5.1, SP3, but we don't
    want to be tied to any particular J2EE server, so we're making sure that
    none of our code is weblogic specific.
    In keeping with this philosophy, I created an RMI service that used the
    standard java.rmi.* imports instead of weblogic.rmi.* and used thestandard
    RMIC compiler rather than the weblogic.rmic compiler.
    Unfortunately, I couldn't get the system to work. I kept getting
    "java.rmi.MarshalException: Error marshaling transport header; nested
    exception is: java.io.EOFException" whenever I'd call Naming.lookup().
    Of course, when I did the search & replace with java.rmi.* with
    weblogic.rmi.*, and switched from the standard rmic to weblogic.rmic,
    everything started working just fine. This is the ONLY change I made!
    Has anyone else run into similar problems?
    Tim Dawson
    Sr. Software Architect
    is.com

  • Compiler Problem with double. Need Help

    Hello,
    I am having a problem with WorkShop Compilers 5.0 98/12/15 C++ 5.0. There is a problem with declaring double inside a nested class which is nested 3 times. Below is the code what shows the problem. If you have Workshop 5.0 please cut and paste the program below and do a simple cc <filename> compile. Run it and you should see the problem.
    ---------------------------cut----------------------------
    #include <stdio.h>
    class myclass3
    public:
    myclass3() {}
    ~myclass3() {}
    short aaa;
    int bbb;
    double ccc;
    char ddd;
    class myclass2
    public:
    myclass2() {}
    ~myclass2() {}
    short aaa;
    int bbb;
    double ccc;
    char ddd;
    myclass3 m_myclass3[50];
    class myclass
    public:
    myclass() {}
    ~myclass() {}
    int a;
    char b;
    float c;
    short aa;
    double bb;
    myclass2 m_myclass2;
    int main()
    myclass m_myclass;
    m_myclass.m_myclass2.m_myclass3[1].ccc = 11111.1111;
    printf("<%f>\n",m_myclass.m_myclass2.m_myclass3[1].ccc);
    return 0;
    ---------------------------cut----------------------------
    The result I get on my machine is
    <-3100025845628125216032986949222617505819484589120973946744108811398502585541238252995581839380933130321904775653231251529249013141831370896782391931299571941247444654355877793201472387703486841059940760873131775588361546584668766208.000000>
    This is definitly incorrect. Does anyone have this problem? Is this a compiler bug? If you figure it out please email me at [email protected]
    Thanks
    Salman Ahmad

    Salman - I tested your program on an up to date version of the C++
    5.0 compiler and received the following results:
    a.out<11111.111100>
    That is what I would expect...
    Have you installed all of the patches for the 5.0 compiler? It is
    possible that this problem was found and fixed.
    The current basic patchid's for CC 5.0 on sparc are: 107311-12, 107357-11, and the libC patch for your version of the OS.

  • Java installation problem with N95

    Hi!
    did anyone experience this?
    i can't install any java file (.jar or .jad) to my n95.
    i was able to install any jar app but suddenly i can't install any jar file
    any ideas?
    i tried to install both to memory card or to phone memory.
    while the installation bar is in progress suddenly an alert says "unable to install"...
    i have no problem with sys and sysx files...

    I had same problem. Here's where...
    http://java.sun.com/j2se/1.4.2/system-configurations.html
    Supported System Configurations
    JavaTM 2 Platform, Standard Edition
    Version 1.4.2
    These configurations apply to both the Java 2 SDK and Java 2 Runtime Environment (J2RE).
    IA32 Windows
    System Requirements
    32-bit: SDK J2RE
    Windows 98 (1st & 2nd Editions) Windows Active & Classic Desktop

  • Java API, problem with secondary keys using multi key creator

    Hi,
    I'm developing an application that inserts a few 100k or so records into a Queue DB, then access them using one of four Hash SecondaryDatabases. Three of these secondary dbs use a SecondaryMultiKeyCreator to generate the keys, and one uses a SecondaryKeyCreator. As a test, I'm trying to iterate through each secondary key. When trying to iterate through the keys of any of the secondary databases that use a SecondaryMultiKeyCreator, I have problems. I'm attempting to iterate through the keys by:
    1: creating a StoredMap of the SecondaryDatabase
    2: getting a StoredKeySet from said map
    3: getting a StoredIterator on said StoredKeySet
    4: iterate
    The first call to StoredIterator.next() fails at my key binding (TupleBinding) because it is only receiving 2 bytes of data for the key, when it should be getting more, so an Exception is thrown. I suspected a problem with my SecondaryMultiKeyCreator, so I added some debug code to check the size of the DatabaseEntries it creates immediately before adding them to the results key Set. It checks out right. I also checked my key binding like so:
    1: use binding to convert the key object to a DatabaseEntry
    2: use binding to convert the created DatabaseEntry back to a key object
    3: check to see if the old object contains the same data as the new one
    Everything checked out ok.
    What it boils down to is this: my key creator is adding DatabaseEntries of the correct size to the results set, but when the keys are being read back later on, my key binding is only receiving 2 bytes of data. For the one SecondaryDatabase that doesn't use a SecondaryMultiKeyCreator, but just a SecondaryKeyCreator, there are no issues and I am able to iterate through its secondary keys as expected.
    EDIT: New discovery: if I only add ONE DatabaseEntry to the results set in my SecondaryMultiKeyCreator, I am able to iterate through the keys as I would like to.
    Any ideas or suggestions?
    Thank you for your attention,
    -Justin
    Message was edited by:
    Steamroller

    Hi Justin,
    Sorry about the delayed response here. I have created a patch that resolves the problem.
    If you apply the patch to your 4.6.21 source tree, and then rebuild Berkeley DB, the improper behavior should be resolved.
    Regards,
    Alex
    diff -rc db/db_am.c db/db_am.c
    *** db/db_am.c     Thu Jun 14 04:21:30 2007
    --- db/db_am.c     Fri Jun 13 11:20:28 2008
    *** 331,338 ****
           F_SET(dbc, DBC_TRANSIENT);
    !      switch (flags) {
    !      case DB_APPEND:
                 * If there is an append callback, the value stored in
                 * data->data may be replaced and then freed.  To avoid
    --- 331,337 ----
           F_SET(dbc, DBC_TRANSIENT);
    !       if (flags == DB_APPEND && LIST_FIRST(&dbp->s_secondaries) == NULL) {
                 * If there is an append callback, the value stored in
                 * data->data may be replaced and then freed.  To avoid
    *** 367,388 ****
    -            * Secondary indices:  since we've returned zero from an append
    -            * function, we've just put a record, and done so outside
    -            * __dbc_put.  We know we're not a secondary-- the interface
    -            * prevents puts on them--but we may be a primary.  If so,
    -            * update our secondary indices appropriately.
    -            * If the application is managing this key's data, we need a
    -            * copy of it here.  It will be freed in __db_put_pp.
    -           DB_ASSERT(dbenv, !F_ISSET(dbp, DB_AM_SECONDARY));
    -           if (LIST_FIRST(&dbp->s_secondaries) != NULL &&
    -               (ret = __dbt_usercopy(dbenv, key)) == 0)
    -                ret = __db_append_primary(dbc, key, &tdata);
                 * The append callback, if one exists, may have allocated
                 * a new tdata.data buffer.  If so, free it.
    --- 366,371 ----
    *** 390,401 ****
                /* No need for a cursor put;  we're done. */
                goto done;
    !      default:
    !           /* Fall through to normal cursor put. */
    !           break;
    !      if (ret == 0)
                ret = __dbc_put(dbc,
                    key, data, flags == 0 ? DB_KEYLAST : flags);
    --- 373,379 ----
                /* No need for a cursor put;  we're done. */
                goto done;
    !      } else
                ret = __dbc_put(dbc,
                    key, data, flags == 0 ? DB_KEYLAST : flags);
    diff -rc db/db_cam.c db/db_cam.c
    *** db/db_cam.c     Tue Jun  5 21:46:24 2007
    --- db/db_cam.c     Thu Jun 12 16:41:29 2008
    *** 899,905 ****
           DB_ENV *dbenv;
           DB dbp, sdbp;
           DBC dbc_n, oldopd, opd, sdbc, *pdbc;
    !      DBT olddata, oldpkey, newdata, pkey, temppkey, tempskey;
           DBT all_skeys, skeyp, *tskeyp;
           db_pgno_t pgno;
           int cmp, have_oldrec, ispartial, nodel, re_pad, ret, s_count, t_ret;
    --- 899,905 ----
           DB_ENV *dbenv;
           DB dbp, sdbp;
           DBC dbc_n, oldopd, opd, sdbc, *pdbc;
    !      DBT olddata, oldpkey, newdata, pkey, temppkey, tempskey, tdata;
           DBT all_skeys, skeyp, *tskeyp;
           db_pgno_t pgno;
           int cmp, have_oldrec, ispartial, nodel, re_pad, ret, s_count, t_ret;
    *** 1019,1026 ****
            * should have been caught by the checking routine, but
            * add a sprinkling of paranoia.
    !      DB_ASSERT(dbenv, flags == DB_CURRENT || flags == DB_KEYFIRST ||
    !            flags == DB_KEYLAST || flags == DB_NOOVERWRITE);
            * We'll want to use DB_RMW in a few places, but it's only legal
    --- 1019,1027 ----
            * should have been caught by the checking routine, but
            * add a sprinkling of paranoia.
    !       DB_ASSERT(dbenv, flags == DB_APPEND || flags == DB_CURRENT ||
    !             flags == DB_KEYFIRST || flags == DB_KEYLAST ||
    !             flags == DB_NOOVERWRITE);
            * We'll want to use DB_RMW in a few places, but it's only legal
    *** 1048,1053 ****
    --- 1049,1107 ----
                     goto err;
                have_oldrec = 1; /* We've looked for the old record. */
    +      } else if (flags == DB_APPEND) {
    +           /*
    +            * With DB_APPEND, we need to do the insert to populate the
    +            * key value. So we swap the 'normal' order of updating
    +            * secondary / verifying foreign databases and inserting.
    +            *
    +            * If there is an append callback, the value stored in
    +            * data->data may be replaced and then freed.  To avoid
    +            * passing a freed pointer back to the user, just operate
    +            * on a copy of the data DBT.
    +            */
    +           tdata = *data;
    +           /*
    +            * If this cursor is going to be closed immediately, we don't
    +            * need to take precautions to clean it up on error.
    +            */
    +           if (F_ISSET(dbc_arg, DBC_TRANSIENT))
    +                dbc_n = dbc_arg;
    +           else if ((ret = __dbc_idup(dbc_arg, &dbc_n, 0)) != 0)
    +                goto err;
    +
    +           pgno = PGNO_INVALID;
    +
    +           /*
    +            * Append isn't a normal put operation;  call the appropriate
    +            * access method's append function.
    +            */
    +           switch (dbp->type) {
    +           case DB_QUEUE:
    +                if ((ret = __qam_append(dbc_n, key, &tdata)) != 0)
    +                     goto err;
    +                break;
    +           case DB_RECNO:
    +                if ((ret = __ram_append(dbc_n, key, &tdata)) != 0)
    +                     goto err;
    +                break;
    +           default:
    +                /* The interface should prevent this. */
    +                DB_ASSERT(dbenv,
    +                    dbp->type == DB_QUEUE || dbp->type == DB_RECNO);
    +
    +                ret = __db_ferr(dbenv, "DBC->put", 0);
    +                goto err;
    +           }
    +           /*
    +            * The append callback, if one exists, may have allocated
    +            * a new tdata.data buffer.  If so, free it.
    +            */
    +           FREE_IF_NEEDED(dbenv, &tdata);
    +           pkey.data = key->data;
    +           pkey.size = key->size;
    +           /* An append cannot be replacing an existing item. */
    +           nodel = 1;
           } else {
                /* Set pkey so we can use &pkey everywhere instead of key.  */
                pkey.data = key->data;
    *** 1400,1405 ****
    --- 1454,1465 ----
      skip_s_update:
    +       * If this is an append operation, the insert was done prior to the
    +       * secondary updates, so we are finished.
    +       */
    +      if (flags == DB_APPEND)
    +           goto done;
    +      /*
            * If we have an off-page duplicates cursor, and the operation applies
            * to it, perform the operation.  Duplicate the cursor and call the
            * underlying function.

  • HP Laserjet 1020 problem with double-side printing

    Hi, everyone!
    I have a problem with my printer HP laserjet 1020. From the community I know that there is no driver for 1020, but there is for 1022 (http://support.apple.com/kb/DL907). I installed it and everything was fine until I tried to print 2-sided page... It appears 2-sided option disappeared and I don't know how to fix this...
    Can anyone help, please?
    Thank you for looking.

    AMW122,
    The part in the photograph appears to be part of the fuser assembly.  If it is part of the fuser assembly, I would recommend you replace the fuser.  An internet search on "LaserJet 3300 fuser" found several links.  
    From what I can see, it appears that the fuser sleeve is damaged which will continue to worsen.
    If it is not the fuser, can you post another photograph further away so that we can see the location on the printer where it is located, etc...
    Kaz
    Kazman
    I am an HP Employee
    Say Thanks by clicking the Kudos Star in the post that helped you.
    Please mark the post that solves your problem as Accepted Solution

  • WAS 6.40 Java Edition - problems with j2sdk-1_4_2_10

    Just for info!
      Several installations without success
      VMWare 5.0.0
      Windows 2003 SP1
      j2sdk-1_4_2_10
      2GB RAM
        Steps 18 or 22 errrors.
      Put j2sdk-1_4_2_08 - OK
      cheers!

    Pl. check OSS Note 716604. SAP recommends using 1.4.2_09 SDK.
    Extract from the Note:
    Quote:
    Access to appropriate Sun J2SE Version:
    Please use Sun J2SE 1.4.2_09 SDK (or higher 1.4.2 versions after they will become available). It is not recommended to use versions lower than 1.4.2_09. Please also do not use J2SE 5.0
    Important note:
    In contrast to the recommendation above please do not use J2SE 1.4.2_10  as it has problems during installation. The problem is under investigation.
    Unquote:
    I have used 1.4.2_09 and have no problems with the installation.
    Regards
    Chandu
    P.S : Pl. reward points if it is helpful to you.

  • Problem with Double Buffering and Swing

    Hi
    I made a game and basically it works pretty well, my only problem is it flickers really badly right now. I read up on a whole lot of forums about double buffering and none of those methods seemed to work. Then I noticed that Swing has double buffering built in so I tried that but then I get compilation errors and I'm really not sure why. My original code was a console application and worked perfectly, then I ported it into a JApplet and it still works but it flickers and thats what I'm tryign to fix now.
    The code below is in my main class under the constructor.
    Heres the double buffering code I'm trying to use, I'm sure you all seen it before lol
    public void update(Graphics g)
              // initialize buffer
              if (dbImage == null)
                   dbImage = createImage(this.getSize().width, this.getSize().height);
                   dbg = dbImage.getGraphics();
              // clear screen in background
              dbg.setColor(getBackground());
              dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
              // draw elements in background
              dbg.setColor(getForeground());
              paint(dbg);
              // draw image on the screen
              g.drawImage(dbImage, 0, 0, this);
         }My paint is right under neath and heres how it looks
    This snipet of code works but when I change the method to
    public paintComponent(Graphics g){
    super.paintComponent(g)...
    everythign stops working and get a compilation error and says that it can't find paintComponent in javax.swing.JFrame.
    public void paint(Graphics g)
              super.paint(g);
              //if game starting display menue
              if (show_menue)
                   //to restart lives if player dies
                   lives = 3;
                   menue.draw_menue(g);
                   menue_ufo1.draw_shape(g);
                   menue_ufo2.shape_color = Color.DARK_GRAY;
                   menue_ufo2.draw_shape(g);
                   menue_ufo3.shape_color = Color.BLUE;
                   menue_ufo3.draw_shape(g);
                   menue_ufo4.shape_color = new Color(82, 157, 22);
                   menue_ufo4.draw_shape(g);
                   menue_ufo5.draw_shape(g);
                   menue_ufo6.shape_color = new Color(130, 3, 3); ;
                   menue_ufo6.draw_shape(g);
                   menue_turret.draw_ship(g);
                   menue_ammo.draw_ammo(g);
              else
                   //otherwise redraw game objects
                   gunner.draw_ship(g);
                   y_ammo.draw_ammo(g);
                   grass.draw_bar(g);
                   o_ufo.draw_shape(g);
                   b_ufo.draw_shape(g);
                   m_ufo.draw_shape(g);
                   s_ufo.draw_shape(g);
                   z_ufo.draw_shape(g);
                   xx_ufo.draw_shape(g);
                   info.draw_bar(g);
                   live_painter.draw_lives(g, lives);
                   score_painter.draw_score(g, score);
                   level_display.draw_level(g, level);
                   explosion.draw_boom(g);
         }I just want to get rid of the flickering for now so any help will be greatly appreciated. Depending which will be simpler I can either try to double buffer this program or port it all to swing but I'm not sure which elements are effected by AWT and which by Swing. Also I read some of the Java documentation but couldn't really understand how to implement it to fix my program.
    Thanks in advance
    Sebastian

    This is a simple animation example quickly thrown together. I have two classes, an animation panel which is a JPanel subclass that overrides paintComponent and draws the animation, and a JApplet subclass that simply holds the animation panel in the applet's contentpane:
    SimpleAnimationPanel.java
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    class SimpleAnimationPanel extends JPanel
        private static final int DELAY = 20;
        public static final int X_TRANSLATION = 2;
        public static final int Y_TRANSLATION = 2;
        private Point point = new Point(5, 32);
        private BufferedImage duke = null;
        private Timer timer = new Timer(DELAY, new TimerAction());
        public SimpleAnimationPanel()
            try
                // borrow an image from sun.com
                duke = ImageIO.read(new URL(
                        "http://java.sun.com/products/plugin/images/duke.wave.med.gif"));
            catch (MalformedURLException e)
                e.printStackTrace();
            catch (IOException e)
                e.printStackTrace();
            setPreferredSize(new Dimension(600, 400));
            timer.start();
        // do our drawing here in the paintComponent override
        @Override
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            if (duke != null)
                g.drawImage(duke, point.x, point.y, this);
        private class TimerAction implements ActionListener
            @Override
            public void actionPerformed(ActionEvent e)
                int x = point.x;
                int y = point.y;
                Dimension size = SimpleAnimationPanel.this.getSize();
                if (x > size.width)
                    x = 0;
                else
                    x += X_TRANSLATION;
                if (y > size.height)
                    y = 0;
                else
                    y += Y_TRANSLATION;
                point.setLocation(new Point(x, y)); // update the point
                SimpleAnimationPanel.this.repaint();
    }AnimationApplet.java
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    public class AnimationApplet extends JApplet
        public void init()
            try
                SwingUtilities.invokeAndWait(new Runnable()
                    public void run()
                        // construct the panel
                        JPanel simpleAnimation = new SimpleAnimationPanel();
                        // put it in the contentPane of the JApplet
                        getContentPane().add(simpleAnimation);
                        setSize(simpleAnimation.getPreferredSize());
            catch (InterruptedException e)
                e.printStackTrace();
            catch (InvocationTargetException e)
                e.printStackTrace();
    }Here's a 3rd bonus class that shows how to put the JPanel into a stand-alone program, a JFrame. It's very similar to doing it in the JApplet:
    AnimationFrame.java
    import javax.swing.JFrame;
    public class AnimationFrame
        private static void createAndShowUI()
            JFrame frame = new JFrame("SimpleAnimationPanel");
            frame.getContentPane().add(new SimpleAnimationPanel());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        public static void main(String[] args)
            java.awt.EventQueue.invokeLater(new Runnable()
                public void run()
                    createAndShowUI();
    }Edited by: Encephalopathic on Mar 15, 2008 11:01 PM

  • Problem with double click in alv interactive report

    Hello frnds,
            the problem is i am displaying basic list using grid display with pf-status and usercommand forms. My first column is checkbox, after selecting checkboxes if i click on the pf-status Button then in the internal table  corresponding field is not getting updated with value 'X'. But once if i double click on the basic list after selecting the checkboxes the values are reflecting as X in the internal table . But my requirement is i want to get the values to be populated once i click on push button only ?
    fcode i have used is &IC1( which is the fcode to trigger double event).Please help me out.
    Thanks in advance.
    subash.

    hi ,
    i had used like this .. it is working...
    *&      Form  USER_COMMAND
    FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM
                            R_SELFIELD TYPE SLIS_SELFIELD.
      clear : v_flag.
      CASE R_UCOMM.
        WHEN 'DATA'.
    LOOP AT IT_TEMP.
         if it_temp-checkbox =  'X'.
              v_flag = 'X'.
              v_pernr1 = IT_TEMP-PERNR.
    *--Get compensation data and populate final internal table
              refresh it_fin.
              CALL FUNCTION 'Z_HR_COMP_STATEMENT'
                EXPORTING
                  PERNR = v_pernr1
                  BEGDA = s_date-low
                  ENDDA = s_date-high
                TABLES
                  FINAL = it_fin.
              if not it_fin[] is initial.
                delete adjacent duplicates from  it_fin comparing pernr.
                read table it_fin index 1.
                move-corresponding it_fin to it_final.
                append it_final.
                clear it_final.
              endif.
            endif.
         enddo.
    ENDLOOP.
          if not v_flag is initial.
    *------ display final data
            PERFORM final_display.
          else.
            message s000 with 'Select atleast one pernr'.
          endif.
      endcase.
    ENDFORM.                    "USER_COMMAND
    regards,
    venkat.

  • Yosemite problems with double click dock commands and keyboard shorcuts

    I have a mid 2012 macbook pro 17", i recently installed Yosemite although it fixed my internet problem (connection dropping randomly) now i am having issues with opening apps and files. Double clicking on a file launches the corresponding app but does't open the file i just get and message stating the app is not open anymore if click the file again. Finder keyboard shorcuts such as command+Q don't work. Can't access some menu items such as "about this mac". Dragging a file into the dock icon launches the app but doesn't open the file either.
    I have no idea what's causing this. anybody got a clue or having similar problems
    Thank you

    Same problem here. (Yosemite 10.0.1)
    the mouse is a 'team scorpion g-reaver' http://teamscorpion.net/ts2014/g-reaver_1.html
    It's work perfectly on the pc of my brother (he uses windows).
    It's a clean install...
    And has a funny fact... The Left Button was with this problem... So what I did? I changed the primary mouse button to the Right... Worked ok for almost 1~2 days... After that, the two buttons showed the double-click problem....
    But, I repeat, the mouse works perfectly in the windows of my brother '-'

  • Java VM problems with dbassist

    I downloaded jdk116_v5 from blackdown as instructed in the
    installation manual. It's installed in /jdk116_v5 and I created
    the link to /usr/local/jre also as instructed.
    The installation went well until the end. It couldn't run the
    dbassist. It spewed the message:
    Unable to initialize threads: Cannot find class java/lang/thread
    Could not create Java VM
    The same message also pops up whenever I try running the
    dbassist program.
    The readme that came with jdk said that it does need a JAVA_HOME
    and CLASSPATH. I have experimented with setting the JAVA_HOME
    to /jdk116_v5 and CLASSPATH to /jdk116_v5 and also
    to /jdk116_v5/lib and /jdk116_v5/lib/classes.zip but nothing
    seems to solve the problem.
    Does anyone have any suggestions? Unfortunately, (dare I say it?)
    Java is not one of my strengths...
    Thanks and Regards
    Devin
    null

    I modified the dbassist script to add the
    $JRE_DIR/lib/classes.zip to CLASSPATH
    Got rid of the not finding the lang/thread class error. but now
    get an exception
    java.lang.NullPointerException
    at
    CheckerClass_base.CheckforNcharSets(CheckerClass_base.java:995)
    at CheckerClass.initializeLanguage(CheckerClass.java:241)
    at CheckerClass.DetectAll(CheckerClass.java:73)
    at DBConfigInfo_base.<init>(DBConfigInfo_base.java:901)
    at DBConfigInfo.<init>(DBConfigInfo.java:44)
    at DBCreateWizard.<init>(DBCreateWizard.java:421)
    at DBCreateWizard.main(DBCreateWizard.java:1082)
    Devin Smith (guest) wrote:
    : I downloaded jdk116_v5 from blackdown as instructed in the
    : installation manual. It's installed in /jdk116_v5 and I created
    : the link to /usr/local/jre also as instructed.
    : The installation went well until the end. It couldn't run the
    : dbassist. It spewed the message:
    : Unable to initialize threads: Cannot find class
    java/lang/thread
    : Could not create Java VM
    : The same message also pops up whenever I try running the
    : dbassist program.
    : The readme that came with jdk said that it does need a
    JAVA_HOME
    : and CLASSPATH. I have experimented with setting the JAVA_HOME
    : to /jdk116_v5 and CLASSPATH to /jdk116_v5 and also
    : to /jdk116_v5/lib and /jdk116_v5/lib/classes.zip but nothing
    : seems to solve the problem.
    : Does anyone have any suggestions? Unfortunately, (dare I say
    it?)
    : Java is not one of my strengths...
    : Thanks and Regards
    : Devin
    null

  • HttpServletRequesr.getParameter :  Problem with double quotes

    Hi,
    I am having a problem in accessing "POST" parameters which have "quotes" in it. For example,
    If the post parameter named param contains Hello "(hw)" World, the getParameter returns Hello instead of Hello "(hw)" World.
    I'm not sure if this is related to encoding. Encoding seem to be ISO-8859-1 (got from getCharacterEncoding).
    Any clue of the problem ?

    How do you send the post parameters. Do you send them usign a html from or are you sending them programaticaly. In that case make sure the keys and values are URL encoded with UTF-8

  • Problem With Double

    I'm executing the code abouve, but I found an error:
    double d4 = 19.64 + .51;
    System.out.println(">>d4:" + d4);
    [out] >>d4:20.150000000000002 // erro

    This tech tip article explains what you're seeing:
    http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2

  • Java installation problem with E60

    did anyone experience this?
    i can't install any java file (.jar or .jad) to my E60. including opera mini..
    any ideas?
    i tried to install both to memory card or to phone memory.
    while the installation bar is in progress suddenly an alert says "unable to install"...
    .sisx files works fine..
    please help.
    Heni

    I had same problem. Here's where...
    http://java.sun.com/j2se/1.4.2/system-configurations.html
    Supported System Configurations
    JavaTM 2 Platform, Standard Edition
    Version 1.4.2
    These configurations apply to both the Java 2 SDK and Java 2 Runtime Environment (J2RE).
    IA32 Windows
    System Requirements
    32-bit: SDK J2RE
    Windows 98 (1st & 2nd Editions) Windows Active & Classic Desktop

Maybe you are looking for

  • How do I sort my phone numbers inside a contact? (using custom labels)

    Alright, since I've converted to iCloud, I'm now adding new contacts in there. However... before I was adding phone numbers through Address Book on my Macbook (that ended up creating a conflict with iCloud so I'm now using iCloud). What I want to kno

  • STUDENT WITH VISUAL PROBLEMS

    Dear Adobe, currently I'm working in a school with young people of 18 years old. The students all study graphic design and learn indesign, photoshop and illustrator. All of our students have a disability (physically or autism). One of them has a seri

  • Timescale at the bottom of Gantt Chart

    Hi, In the old P3 application, we were able to put timescale at the bottom of Bars area, how can we do it with new P6.

  • Save in pdf or image

    http://www.marcospaulo.com/appweb/ After choose this colours ou textures of the owl I want save/send mail....format:pdf or image .......Some help me?? I think better is javascript, but found sources for html5....nothing about this in the edge...

  • Exporting Importing mail boxs

    We have two mail boxs operating in the one computer account - have now set up two accounts on our computer (one for each operator). How do i export their mail box contents (all emails) to the mail program in the other computer account.