-3

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

frame.moveInsideFrame(pointX,pointY).
Dillon Burton
  • 363
  • 6
  • 16
  • 1
    What camera? What have you tried? We need some more information before we can really understand your question. – Jon Egeland Dec 01 '12 at 00:54
  • 1
    Are you referring to a java.awt Frame? – djjeck Dec 01 '12 at 00:55
  • 1
    ...what? I don't understand. – tckmn Dec 01 '12 at 00:56
  • If I have a Window, lets say 500 X 500, and i want your view port to also be 500 X 500, how would I move that view port inside of the window to go to a different location in space? I haven't attempted it, as I have no idea how to move the screen inside of a frame. I thought it would be something like frame.getRelativeLocation(). Basically, I want to create a moving camera inside of a frame. – Dillon Burton Dec 01 '12 at 00:57

3 Answers3

1

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().

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 2
    An `ArrayList` is unnecessary; `frame.getComponents()` can be iterated over to save memory. – FThompson Dec 01 '12 at 01:05
  • @Vulcan well, I was assuming he was overriding `paintComponent`. I guess I'll put that too. – tckmn Dec 01 '12 at 01:06
  • That makes sense. But it is at all possible to make some sort of view-port that moves TO the objects instead of moving the objects inside the frame? – Dillon Burton Dec 01 '12 at 01:08
  • @DillonBurton just move all of the objects into the frame. You can't move the frame view itself. – tckmn Dec 01 '12 at 01:08
  • @PicklishDoorknob Ok. I've just heard of it being done before and was wondering if it were at all possible. – Dillon Burton Dec 01 '12 at 01:16
  • How about adding only a wrapper Frame to the view Frame, and all the other elements to that? You would only move a single Frame, instead of iterating through potentially dozens of widgets. – djjeck Dec 03 '12 at 03:24
0

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 (i.e. no layouts). See http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html for more details on absolute positioning.

djjeck
  • 1,781
  • 17
  • 25
  • Absolute positioning is a VERY bad idea. (also I already said this in my answer.) – tckmn Dec 01 '12 at 01:09
  • Why is that bad? I'm talking about absolutely positioning only the viewport, not the elements inside it. Why do you say that you can't do that? – djjeck Dec 02 '12 at 05:33
  • Never use absolute positioning, it is very bad practice. **NEVER.** http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing – tckmn Dec 02 '12 at 12:00
  • Since I don't have time to try and test absolute positioning in Java, I'll just give up on this discussion. – djjeck Dec 03 '12 at 03:22
0

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

Iyad
  • 73
  • 5