wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Switch Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Switch Statement" Watch "Switch Statement" New topic
Author

Switch Statement

Holly Leery
Ranch Hand

Joined: Sep 08, 2004
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

Joined: Sep 08, 2004
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?

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
This is one piece - you can fill in the rest.


Mike Gershman
SCJP 1.4, SCWCD in process
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
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!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch Statement