• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch Statement

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one piece - you can fill in the rest.
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic