Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Good day, in other for me to get better and create more desired code. i would like to find out if the following situation is bad or acceptable.

I am creating an animation that gets triggered by two buttons. one on the left of a jpanel and the other on the right. depending on which button is pressed, the animation starts from the button position. Now it can be quite some work working out the different calculations with a lot of internal if and else statements(to know which button is clicked)on the Method that handles the animation(i have done so).

so am asking, if i clone the method and modify it to be suitable for one button (so each button calls a different method) which saves a lot of hassle, is that good or bad practise?

like this:

     private void moveImage(){

    if(xpos < imagewidth && ypos < imageheight) {

           if(buttonA){

              image.setX(image.getX() - xmove);

           } else {

              image.setX(image.getX() + xmove);
           }

           image.setY(image.getY() + ymove);
    }

     //other codes here 
}

or i could do this for button A and the same for button B:

 private void moveImageA(){

        if(xpos < imagewidth && ypos < imageheight) {

               image.setX(image.getX() - xmove);

               image.setY(image.getY() + ymove);
        }

          //other codes here 
    }
share|improve this question

1 Answer

Depends on application design. What is "more important" - image, or button?

It could be something like move(Image image) method in the buttons (decalred in interface of the buttons), or it may be reactOn(Button button) in Image.

It is hard to recommend something with the presented information. Generally, code reuse is always a good move.

share|improve this answer
well from what your asking, i should say the image. I use one action performed method which subsequently means one paintComponent Method which draws an image. so if i press button A and later Button B. the animation might be messed up for Button B if you know what i mean since the position of the image might have changed. – sparrow Apr 11 '12 at 11:43
Anyway, hard to recommend something certain. May be, en.wikipedia.org/wiki/Visitor_pattern may give you an idea. – Michael Apr 11 '12 at 13:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.