我有一张图片详细说明了我想要为Game.height的所有值实现的目标。问题是我在变量circle_dy中的数学表达式似乎是不正确的,因为它缩放了Game.height所有值的图像位置。
// 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);
}
}
答案 0 :(得分:1)
作为建议:
为windows resize编写一个监听器,用于更新球的大小。
要计算所需的大小更改,请抓住窗口的x和y大小(建议每次监听器完成时保存它们)。将%和新值计算为%,然后将球缩放相同的百分比。
供参考:http://docs.oracle.com/javase/tutorial/uiswing/events/componentlistener.html