简体   繁体   中英

How to update JViewPort

i've been messing around with java recently and I'm trying to develop a small game. The problem is, that the JViewport i am using does not update. 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. 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.

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

Never use a null layout on a JScrollPane. A JScrollPane uses an internal layout manager to manange the horizontal/vertical scrollbars, the row/column headers and the viewport etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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