[英]How to update JViewPort
我最近一直在搞怪Java,并且正在尝试开发一款小型游戏。 问题是,我正在使用的JViewport不会更新。 在视口上调用repaint方法时,视口在正确的位置闪烁,并且要在视口中显示的整个图像在背景中绘制,并且可以正常工作。 如何仅绘制视口而不是其余部分? 谢谢
public start() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension my = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Rectangle myrec = new Rectangle(0,0,my.width, my.height);
setBounds(myrec);
this.setUndecorated(true);
JPanel Background = new JPanel();
Background.setBounds(myrec);
Background.setLayout(null);
setContentPane(Background);
JScrollPane map = new JScrollPane();
map.setBounds(0, 0, 3000, 3000);
map.setLayout(null);
//setContentPane(map);
gameoberflaeche MainMap = new gameoberflaeche();
MainMap.setBounds(0,0, map.getWidth(), map.getHeight());
MainMap.setup();
map.add(MainMap);
Viewport currview = new Viewport(myrec, MainMap.player_1, map);
Background.add(currview);
//setContentPane(currview);
int delay = (1000/30);
ActionListener update = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
MainMap.Anfang.checkdead();
//currview.stats.update();
//currview.weaponpanel.update();
//currview.MagPanel.update();
currview.update(MainMap.player_1.field.x, MainMap.player_1.field.y);
}
};
new Timer(delay, update).start();
}
public class Viewport extends JViewport{
Player myplayer;
statpanel stats;
Magazin MagPanel;
Waffenpanel weaponpanel;
Rectangle myrec;
public Viewport(Rectangle myrec, Player myp, JScrollPane myview) {
setView(myview);
reshape(0,0,myrec.width,myrec.height);
}
public void update(int x, int y) {
// System.out.println(getView());
setViewPosition(new Point(x,y));
System.out.println(x + " " + y + " " + getViewRect());
}
}
JPanel Background = new JPanel();
变量名称不应以大写字母开头。
Background.setLayout(null);
不要使用空布局。 Swing旨在与布局管理器一起使用。
JScrollPane map = new JScrollPane();
map.setLayout(null);
切勿在JScrollPane上使用null布局。 JScrollPane使用内部布局管理器来管理水平/垂直滚动条,行/列标题和视口等。
Viewport currview = new Viewport(myrec, MainMap.player_1, map);
不要创建新的视口。 要更新滚动窗格中的组件,只需将组件添加到滚动窗格的视口中:
scrollPane.getViewport().setView(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.