简体   繁体   English

在 java 的框架内移动相机

[英]Moving the camera inside a Frame in java

I would like to know how to move the camera inside the Frame in java.我想知道如何在 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.) (这只是示例代码,它不会真正起作用。在屏幕上有所有组件的ArrayList并单独移动每个组件。)

Or, if you just put them all in your frame (instead of overriding paintComponent in a custom JPanel) then use frame.getComponents() .或者,如果您只是将它们全部放在框架中(而不是在自定义 JPanel 中覆盖paintComponent ),则使用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.有关绝对定位的更多详细信息,请参阅http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

I think putting all your objects in a JPanel with a null layout and moving it as a camera is the best solution我认为将所有对象放在具有 null 布局的 JPanel 中并将其作为相机移动是最好的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM