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());
}
}
Variable names should NOT start with an upper case character.
Don't use a null layout. Swing was designed to be used with layout managers.
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.
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: