我必须说我尝试了一切,但我无法理解我的代码有什么问题。在我的SIView
班级中,我创建了以指定分辨率MainFrame
扩展的(比如说 X,Y)。JFrame
然后我创建与 具有相同分辨率的gamePanel
扩展,并将其添加到. 问题是面板的有效分辨率是两倍大(x*2,y*2)。这就像面板被拉伸到两倍大。JPanel
MainFrame
MainFrame
框架将仅显示面板的四分之一(左上四分之一),pack()
或者手动设置大小,除非我将其设置为分辨率加倍,在这种情况下可以,但这不是正确的方法(在计算位置时游戏我必须将所有内容加倍或除以 2 以保持适当的比例)。我什至尝试了不同的布局管理器,但都没有成功。
这是主视图类的代码:
public class SIView implements Runnable {
private final MainFrame mainFrame;
private final GamePanel gamePanel;
public SIView(BlockingQueue<SIEvent> eventQueue) {
this.eventsQueue = eventQueue;
mainFrame = new MainFrame();
gamePanel = new GamePanel();
gamePanel.setVisible(true);
mainFrame.getContentPane().add(gamePanel);
// mainFrame.pack();
@Override
public void run() {
mainFrame.setVisible(true);
}
public void init() {
SwingUtilities.invokeLater(this);
}
//some code not related
}
框架类:
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 6513420589842315661L;
public MainFrame() {
setTitle("Space Intruders");
setSize(new Dimension(SIParams.RES_X, SIParams.RES_Y));
setResizable(false);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
面板类:
public class GamePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 8112087715257989968L;
private final PlayerShipView playerShip;
private final ArrayList<SmallEnemyShipView> smallEnemyShip;
private final ArrayList<LightMissleView> lightMissle;
public GamePanel() {
setPreferredSize(new Dimension(SIParams.RES_X, SIParams.RES_Y));
setMaximumSize(new Dimension(SIParams.RES_X, SIParams.RES_Y));
setBounds(0, 0, SIParams.RES_X, SIParams.RES_Y);
setBackground(new Color(0, 0, 0));
setLayout(new OverlayLayout(this));
setDoubleBuffered(true);
// TODO
playerShip = new PlayerShipView();
smallEnemyShip = new ArrayList<SmallEnemyShipView>();
lightMissle = new ArrayList<LightMissleView>();
this.add(playerShip);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
//some code not related
}