简体   繁体   中英

Moving the camera inside a Frame in java

I would like to know how to move the camera inside the Frame in java. Ex:

frame.moveInsideFrame(pointX,pointY).

You would have to move everything in the frame. For example:

private void moveInsideFrame(int moveX, int moveY) {
    for (int i = 0; i < listOfObjects.size(); i ++) {
        JComponent current = listOfObjects.get(i);
        current.x -= moveX;
        current.y -= moveY;
    }
}

(this is just example code, it won't really work. have an ArrayList of all of your components on screen and move each individually.)

Or, if you just put them all in your frame (instead of overriding paintComponent in a custom JPanel) then use frame.getComponents() .

You can consider your outer frame as the "camera" frame, and you can create a "world" frame inside of it. You can then move your world window using opposite coordinates as to emulate the movement of a camera.

To implement it, use an absolute positioning for the inner frame (ie no layouts). Seehttp://docs.oracle.com/javase/tutorial/uiswing/layout/none.html for more details on absolute positioning.

I think putting all your objects in a JPanel with a null layout and moving it as a camera is the best solution

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