programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Switch Statement

Ranch Hand
Posts: 97
How would I increment or decrement x or y by 30 in a switch statement? anyone have any tips for me? thanks in advance!

Holly Leery
Ranch Hand
Posts: 97
The code below is what i will use to randlomly pick a number 0 to 3...those numbers represent north, south, east or west inside the switch statement. for north i would subtract 30 from y and for east i would add 30 to x and so on. This is on a grid in an applet just so you know. i have a dot that moves north, south, east or west by 30 pixels which is the width of a square. how would i write the incrementing and decrementing in a switch statement? any ideas?

Ranch Hand
Posts: 1272
This is one piece - you can fill in the rest.

Holly Leery
Ranch Hand
Posts: 97
ok great...thank you. i have another quick question. if i have to use a code such as: g.fillOval( x-3, y-3, 6, 6 ); that way the switch statement works right, how do I start the dot in the middle of the grid at say 160, 160? I am using that formual above because at the end of this code I want to use this: distance = ( Math.abs( x - 160 ) + Math.abs( y - 160 ) ) / 30;
to determine how far the dot has moved. So with that said how do I start the dot in grid so that when the applet is first opened the dot is right in the middle? any ideas? thanks again!

Ranch Hand
Posts: 3061
The answer to your latest question depends on how you have organized your code. At the very least, I assume you have a class that extends Applet (or JApplet). If you make x and y member variables in this class, you can initialize them in the init() method. Usually when I write applets, I also have a class that extends Panel (or JPanel). In this case, the initialization goes in the panel's constructor.

I hope this helps. Please come back if you have any more questions.

Keep coding!

Layne

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.