This is my first post so if any other information is needed please let me know.
I’m making a game in a Java GUI and just have two hopefully quick questions about which. I can currently paint a map to the screen and pan around the map with the following code:
private class MoveMap implements MouseMotionListener{
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
if(e.getX() > swidth-30){
if(xmod+(columns*30) > swidth){
xmod-=30;
repaint();
}
}
else if(e.getX() < 30){
if(xmod < 0){
xmod+=30;
repaint();
}
}
else if(e.getY() > sheight-30){
if(ymod+(rows*30) > sheight){
ymod-=30;
repaint();
}
}
else if(e.getY() < 30){
if(ymod < 0){
ymod+=30;
repaint();
}
}
else{
}
}
}
The only problem is the mouse must continually move at the edge of the screen to continually pan. My question is if there’s a way to have the mouse at the edge of the screen and continually pan while updating the graphics? My second question would be if this could be applied to characters moving as well? I thought maybe a thread would be a possible solution but am not familiar with using them. Thank you!
I can think of two ways to achieve this.
You could…
Start a
javax.swing.Timerthat would update the x/y position as required and repaint the screen when ever the mouse entered within a given distance of the edge of the screen. You would, obviously stop the timer as the mouse moved back out 😉You could…
Use a background
Threadto monitor the mouse position and when it enters the “trigger” zone, would update the x/y values and trigger a repaint, making sure you re-sync the calls to the EDT 😉