0

我有一张图片详细说明了我想要为 Game.height 的所有值实现的目标。问题是我在变量 circle_dy 中的数学表达式在针对 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

1 回答 1

1

作为建议:

为 windows resize 编写一个监听器,它将更新你的球的大小。

要计算所需的大小更改,请获取窗口的 x 和 y 大小(建议在每次侦听器完成时保存它们)。将旧值和新值计算为 %,然后按相同的 % 缩放您的球。

供参考:http ://docs.oracle.com/javase/tutorial/uiswing/events/componentlistener.html

于 2013-01-01T18:32:02.927 回答