Author Topic: [java] drawOval problems  (Read 11276 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
[java] drawOval problems
« on: May 12, 2011, 08:00:04 pm »
Gah sorry for creating so many java threads.. but I get so many buggs  :thumbsup:

Ok this time, I have a weird problem dealing with drawOval.

Code: [Select]
import java.awt.*;
import java.applet.*;
import java.util.Random;
 
public class sphere extends Applet {
 
        public void paint(Graphics g)  {
 
                Random r = new Random();
                int wh = r.nextInt(300);
                while(true) {
                        Color  c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
                        g.setColor(c);
                        for (wh = 0;wh>=r.nextInt(300);wh++) {
                                g.fillOval(r.nextInt(1001),r.nextInt(701),wh,wh);
                        }
 
//                      wh = r.nextInt(300);
                        try {
 
                Thread.sleep( 20 );
 
            } catch ( InterruptedException e ) {
 
                // do nothing
            }
                }
        }
}

In this applet, There is supposed to be a random circle drawn at a random location. It's width/height starts at 0, and is supposed to increase up to the randomly generated number thus making the circle bigger in an animation style. However it stays at the same size that's defined in the for loop (0) so you can't really see it. How come it's not increasing?
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: [java] drawOval problems
« Reply #1 on: May 14, 2011, 02:33:29 pm »
Look closely at this code:
Code: [Select]
for (wh = 0;wh>=r.nextInt(300);wh++) {It calculates a random number in every cycle of the for loop. This can be very irreliable.

further:
Code: [Select]
int wh = r.nextInt(300);
...
int wh = r.nextInt(300);
These two lines are a waste of size and CPU. I know that it's only a few bites and under a microsecound, but in a large applet, this kind of optimisation can tell the diference between fitting on a CD, and not fitting on a CD.

back to your question: You draw an oval with increasing the size, but always at a random location withouth deleting the previous one. I think your problem is that the screen is filled with hundreds of ovals so that you can't see a single oval. Instaed the screen will be just a solid color.

The first lines weren't an answer to your question, but I just wanted to help you to optimise your code.
« Last Edit: May 14, 2011, 02:34:34 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java] drawOval problems
« Reply #2 on: May 22, 2011, 03:09:11 pm »
oh ok. Well thanks for helping anyways, but I figured it out though :p

oh btw:
Quote from: ben_g
but in a large applet, this kind of optimisation can tell the diference between fitting on a CD, and not fitting on a CD.
theres always winrar :P
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Ashbad

  • Guest
Re: [java] drawOval problems
« Reply #3 on: May 22, 2011, 03:12:57 pm »
Code: [Select]
int wh = r.nextInt(300);
...
int wh = r.nextInt(300);

These two lines are a waste of size and CPU. I know that it's only a few bites and under a microsecound, but in a large applet, this kind of optimisation can tell the diference between fitting on a CD, and not fitting on a CD.

Since it could be placed in a function format, space could be conserved that way.  However, that could be only ~50 bytes (random guess), and follows the idea of 'inlining' functions -- which increases size but speeds many operations up.

I think it would be fine, since declaring it as a method would just add that much more size back, and would not merit any reward for optimization.  I say keep it ;)