gpt4 book ai didi

java - 如何更新 JViewPort

转载 作者:行者123 更新时间:2023-12-02 11:56:29 25 4
gpt4 key购买 nike

我最近一直在摆弄java,正在尝试开发一个小游戏。问题是,我正在使用的 JViewport 没有更新。当在视口(viewport)上调用重绘方法时,视口(viewport)在正确的位置闪烁,并且视口(viewport)显示的整个图像在背景中绘制并且功能正常。如何只绘制视口(viewport)而不绘制整个其余部分?感谢您的回复;

    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 上使用空布局。 JScrollPane 使用内部布局管理器来管理水平/垂直滚动条、行/列标题和视口(viewport)等。

Viewport currview = new Viewport(myrec, MainMap.player_1, map);

不要创建新的视口(viewport)。要更新滚动 Pane 中的组件,您只需将组件添加到滚动 Pane 的视口(viewport)即可:

scrollPane.getViewport().setView(...);

关于java - 如何更新 JViewPort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47563883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com