0

I have a picture that details what I want to achieve for all values of Game.height. Problem is my mathematical expression in the variable circle_dy seems to be incorrect when it comes to scale the image's location for all values of Game.height.

IMG

 // images of the game
        private Image circle;
    // ball's spawning location 
        private int circle_dx = 0;
        private int circle_dy = (Game.height/2) + 30 ;

    public class GameBoard{ 

    public GameBoard(){

            // construct an ImageIcon specified by the given string directory
        ImageIcon circle = new ImageIcon("src/pics/circle.png");
        // get image type of the ImageIcon and assign it into the image instance                                                  //variable
        this.circle = circle.getImage();


        setBackground(Color.black);


    }

    // appropriate method to paint is paintComponent
        public void paintComponent(Graphics g){
            // if you are overriding a method call the super class paintComponent method
            // because you are creating your own version of the paintComponent method
            super.paintComponent(g);

            // draw the image itself at a particular location on the JPanel


            g.drawImage(this.circle,circle_dx,circle_dy,this);
            }
   }






public class Game extends JFrame{

        public static final int width = 600;
        public static final int height = 200;

    public Game(){
    // add the JPanel to the jframe
    add(new GameBoard());
    setVisible(true);
    // set the size of the jframe
    setSize(width,height);
    }

}
4
  • Why? Why to rely on the height and width of viewable area? Jan 1, 2013 at 3:08
  • the reason being those are the dimension of my program. This program dimension may potentially change(as do all programs do). I do not want to just hard-code constants as that can mess up the location of the circle image at any time if the height changes.
    – Nicholas
    Jan 1, 2013 at 3:16
  • 1) For better help sooner, post an SSCCE. 2) See How do I create screenshots? (for tips on making great screenshots). E.G. Alt+PrintScreen for the GUI, save as PNG, don't include the IDE (post text base code, if relevant, & yes it is). Jan 1, 2013 at 4:00
  • thanks, I edit my post to look much better!
    – Nicholas
    Jan 1, 2013 at 18:27

1 Answer 1

1

As a suggestion:

Write a listener for windows resize that will update the size of your ball.

To calculate the required size changes, grab the x and y sizes of your window (would suggest saving them each time listener finishes). Work out the old and new values as a % then scale your ball by the same %.

For reference: http://docs.oracle.com/javase/tutorial/uiswing/events/componentlistener.html

2
  • when you mean % do you mean remainder? just making sure. I will try that.
    – Nicholas
    Jan 1, 2013 at 18:45
  • 1
    My bad I meant as a percentage of its original value, so if 100 becomes 24, then it would be 24%
    – Mark
    Jan 1, 2013 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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