This is the screen that im trying to repaint but it is not repainting properly.
public class arenaScreenBuild extends JPanel{
int pX=200, pY=150;
public void updateScreen(){
repaint();
}
public void paintComponent(Graphics g) {
g.drawString("x:"+pX, 535, 525);
g.drawString("y:"+pY, 535, 545);
}
public void refreshXY(int x, int y){
pX=x;
pY=y;
System.out.println("Refreshed X&Y");
updateScreen();
}
}
This is the screen displaying the graphics. When run, every time i move(press the right arrow key), it displays “Refreshed X&Y” but even though it calls the updateScreen() method, the displayed items are not redrawn. The code, if it had worked, should display x:XVALUE, y:YVALUE after the “refreshed X&Y”.
public class ArenaKeys extends KeyAdapter {
arenaScreenBuild arenaBG = new arenaScreenBuild();
int xPos = 0, playerFace = 4,xPPos = 200, yPPos = 150;
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_RIGHT) {
if (xPos <= 3250)
if (((xPos + xPPos) >= 825) && ((xPos + xPPos) <= 910)
&& (yPPos >= 170) && (yPPos <= 255)) {
} else if (((xPos + xPPos) >= 1325) && ((xPos + xPPos)<= 1410)
&& (yPPos >= 170) && (yPPos <= 255)) {
} else
xPos += 5;
}
arenaBG.refreshXY(xPPos+xPos,yPPos);
}
}
EDIT: *Turns out that it does work but what i was doing was adding a Drawpanel on top of another drawpanel and this code was for the one underneath so it wasn’t letting the bottom code update, i solved this by merging together the two codes for both drawpanels.*
This seems to work for me (it’s an SSCCE created out of your code). Since it works and there’s virtually no modifications to your code, the problem probably doesn’t lay in the code you posted, but in some other code. Also, generally you’d want to use Key Bindings instead of a KeyListener for these sorts of things.