Help with moving an object in Java?
-
Hello all, Could someone give me some advice on how to tackle this problem please - thank you in advance. So basically I have a program that paints a simple object image. There are 5 of these objects altogether and different classes and methods have been set up to do this and this works correctly. The program currently also asks the user if they would like to change the colour of this object shape, and which shape they would like to change (user inputs a number 1-5) then the program will change the colour of this shape. But the new task is for the user to input a number (1-5) to which shape they would like to MOVE position of. Any ideas on how this would be done? Would there need to be different methods for changePositionX, changePositionY? The user needs to input the X and Y co ordinates and then this will move the selected shape to its NEW position in the current JFRAME. Thank you, if any code is needed I will paste it in.
-
Answer:
Obviously, you need a hierarchy so all your shapes have the same methods. If we have abstract class Sprite class Star extends Sprite class PolyBall extends Sprite class Ellipse extends Sprite // and so on, then from an array of Sprite[] sprites; for( int i = 0; i< sprites.length; i++ ) { Sprite selected = null; if( userInput = sprites[ i ].getID(); selected = sprites [ i ]; break; } moveMySprite( selected );
Oddsocks at Yahoo! Answers Visit the source
Related Q & A:
- Is Java fully object-oriented?Best solution by Stack Overflow
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- Object reference not set to an instance of an object; null?Best solution by Stack Overflow
- How to solve "Object reference not set to an instance of an object?Best solution by Stack Overflow
- I need help with writing up the Java Code.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.