Guys, I’m making a RTS game that needs to move the screen vision around the map.
To do it, I’m thinking about using an invisible component positioned on each side of the screen in a border layout.
When the mouse is over this component, it fires an action which moves the screen.
We know a game uses lots of memmory an proccessing. How a borderLayout whoud interfer in this process? Whould it take lots of processing? Is there a better way to do it?
Thanks
The idea of an invisible component with a MouseListener on the edges seems pretty cool to this Noob. But placing the components on your display panel, handling things like the overlapping of the game area, and getting the various component classes to communicate to one another seems like a big headache to me. Maybe it is doable, and a more experienced person will describe how.
But as a simpler alternative, building off of your basic idea, how about using a MouseMotionListener instead? You probably already have a MouseMotionListener in your main playing area, yes? If so, maybe this: define two X values, one for the left and one for the right, and test the mouse’s X position against these.
I definitely wouldn’t do a component. Instead use a timer and the last known mouse position.
public class MyClass implements MouseMotionListener, ActionListener
{
int mouseX, mouseY;
Timer timer;
public void mouseMoved(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
}
public MyClass()
{
timer = new Timer(10, this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
if (mouseX >= getWidth() - 50)
{
scrollRight();
}
//etc.
}
}
I don’t understand what the use of the timer is in the above example. Why not just add a mouselistener with your code to the JFrame the gameboard is in?
The timer is so that it will continue to scroll the playfield while the mouse remains on the edge.
Exactly. A MouseMotionListener will only fire events as they happen, and there is no “mouseIsDoingNothing” event. Because of this, you need to add a timer of some kind.
Oh, right, I usually have startDoingSomething() and a stopDoingSomething() methods, so that’s why i was confused.

Oh, right, I usually have startDoingSomething() and a stopDoingSomething() methods, so that’s why i was confused.
Even so, doingSomething() will need to be executed by some sort of loop or timer.