简体   繁体   English

如何更新JViewPort

[英]How to update JViewPort

i've been messing around with java recently and I'm trying to develop a small game. 我最近一直在搞怪Java,并且正在尝试开发一款小型游戏。 The problem is, that the JViewport i am using does not update. 问题是,我正在使用的JViewport不会更新。 When calling the repaint method on the Viewport the Viewport flickers at the right position and the whole image to be shown by Viewport is drawn in Background and functioning alright. 在视口上调用repaint方法时,视口在正确的位置闪烁,并且要在视口中显示的整个图像在背景中绘制,并且可以正常工作。 How do I just paint the Viewport, not the whole rest? 如何仅绘制视口而不是其余部分? Thx for replies; 谢谢

    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();

Variable names should NOT start with an upper case character. 变量名称不应以大写字母开头。

Background.setLayout(null);

Don't use a null layout. 不要使用空布局。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。

JScrollPane map = new JScrollPane();
map.setLayout(null);

Never use a null layout on a JScrollPane. 切勿在JScrollPane上使用null布局。 A JScrollPane uses an internal layout manager to manange the horizontal/vertical scrollbars, the row/column headers and the viewport etc. JScrollPane使用内部布局管理器来管理水平/垂直滚动条,行/列标题和视口等。

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

Don't create a new Viewport. 不要创建新的视口。 To update the component in the scrollpane all you do is add the component to the viewport of the scrollpane: 要更新滚动窗格中的组件,只需将组件添加到滚动窗格的视口中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM