• Java - More Efficient Way?
    6 replies, posted
I just began learning java and I was wondering if there was a easier way to make a bullseye. [code] import java.applet.Applet; import java.awt.*; import java.util.Set; public class Eli extends Applet { public void paint (Graphics page) { page.setColor (Color.red); page.fillOval(5, 5, getWidth()-10, getHeight()-10); page.setColor (Color.blue); page.fillOval(10, 10, getWidth()-20, getHeight()-20); page.setColor (Color.red); page.fillOval(15, 15, getWidth()-30, getHeight()-30); page.setColor (Color.blue); page.fillOval(20, 20, getWidth()-40, getHeight()-40); page.setColor (Color.red); page.fillOval(25, 25, getWidth()-50, getHeight()-50); page.setColor (Color.blue); page.fillOval(30, 30, getWidth()-60, getHeight()-60); page.setColor (Color.red); page.fillOval(35, 35, getWidth()-70, getHeight()-70); page.setColor (Color.blue); page.fillOval(40, 40, getWidth()-80, getHeight()-80); page.setColor (Color.red); page.fillOval(45, 45, getWidth()-90, getHeight()-90); page.setColor (Color.blue); page.fillOval(50, 50, getWidth()-100, getHeight()-100); page.setColor (Color.red); page.fillOval(55, 55, getWidth()-110, getHeight()-110); page.setColor (Color.blue); page.fillOval(60, 60, getWidth()-120, getHeight()-120); page.setColor (Color.red); page.fillOval(65, 65, getWidth()-130, getHeight()-130); page.setColor (Color.blue); page.fillOval(70, 70, getWidth()-140, getHeight()-140); page.setColor (Color.red); page.fillOval(75, 75, getWidth()-150, getHeight()-150); page.setColor (Color.blue); page.fillOval(80, 80, getWidth()-160, getHeight()-160); page.setColor (Color.red); page.fillOval(85, 85, getWidth()-170, getHeight()-170); page.setColor (Color.blue); page.fillOval(90, 90, getWidth()-180, getHeight()-180); page.setColor (Color.red); page.fillOval(95, 95, getWidth()-190, getHeight()-190); page.setColor (Color.blue); page.fillOval(100, 100, getWidth()-200, getHeight()-200); } } [/code] Thanks
something like: [code] for(int i = 0; i < 20; i++) { page.setColor (i % 2 == 0 ? Color.red : Color.blue); int num = 5 * (i + 1); page.fillOval(num, num, getWidth() - num2*2, getHeight() - num2*2); } [/code] does the same, not tested or anything :P edit: not sure if java supports ?:
If you want efficiency as in rendering performance you should use a texture/sprite instead of drawing programmatically. Also, remember to use [noparse][csharp][/csharp], [cpp][/cpp] and [code][/code][/noparse] tags when posting code ([noparse][csharp][/csharp][/noparse] is probably the closest for Java).
Thanks guys it helped a lot :D
A few performance things you can do. Note that almost all of these will decrease readability and a lot of them will probably be done by the optimiser anyway. I'm posting these for information only and do not recommend doing them due to the loss of readability. 1) There are two cases of "num2*2" (Should actually be "num*2"), this can be "cached" in a variable to save 20 multiply operations 2) The for loop could be implemented as "for( int i = 20; i--; )", this will result in the iterations 20 -> 1 rather than 0 -> 19, so you would need to change the code to "( i - 1 ) % 2" and "5 * i" - This will save 20 comparisons 3) The results of the getWidth() and getHeight() calls can be "cached" in a pair of variables which will save 20 calls (The overhead of a function call + the amount of time required to calculate the value, if any) 4) Batching, you can "unroll" this into two for loops, one for each colour with each one stepping by 2 and starting from different numbers (0 and 1) - You would then have two page.setColor() calls, one before each loop - This would save 18 page.setColor() calls which could be a reasonable time saving
[QUOTE=yngndrw;28674170]A few performance things you can do. Note that almost all of these will decrease readability and a lot of them will probably be done by the optimiser anyway. I'm posting these for information only and do not recommend doing them due to the loss of readability. 1) There are two cases of "num2*2" (Should actually be "num*2"), this can be "cached" in a variable to save 20 multiply operations 2) The for loop could be implemented as "for( int i = 20; i--; )", this will result in the iterations 20 -> 1 rather than 0 -> 19, so you would need to change the code to "( i - 1 ) % 2" and "5 * i" - This will save 20 comparisons 3) The results of the getWidth() and getHeight() calls can be "cached" in a pair of variables which will save 20 calls (The overhead of a function call + the amount of time required to calculate the value, if any) 4) Batching, you can "unroll" this into two for loops, one for each colour with each one stepping by 2 and starting from different numbers (0 and 1) - You would then have two page.setColor() calls, one before each loop - This would save 18 page.setColor() calls which could be a reasonable time saving[/QUOTE] Wow thanks didn't know it could be even more fast :P [editline]18th March 2011[/editline] I made a small applet that makes about 20 alternating color rings, but I was wondering if there was a way to slow down the process so I can see each ring appear. Like having 1 second in between each ring spawns.
Snip. My bad, misread the code.
Sorry, you need to Log In to post a reply to this thread.