简体   繁体   中英

Auto Scroll with the JLabel containing an image

There's something I want to try but I've no idea how to go about this and not sure if it's at all possible.

I've got a JPanel of JLabels using a GridLayout. This JPanel is then added to a JScrollPane.

One if the JLabels has an image in it. That image is supposed to show me where the character in the program is. I just a JLabal so I can get it's position. So basically when I want to move the character, I remove the image from the current JLabel and add it onto the next JLabel. I understand this is possibly the worst approach ever but I am new, all I know is Java and I've got a deadline and didn't think to learn Game Development in Java.

Anyway, since I'm using a JScrollPane, I would like the program to automatically scroll when the JLabel with the image cannot be seen anymore. Therefore as the character is moving, the JSCrollPane is scrolling, so I don't have to scroll to it. It does it for me. So like a camera watching where it's going.

Is this possible?

Thank you very much!!

I understand this is possibly the worst approach ever

You will get really terrible movement since the character will jump many pixels at one time (depending on the size of your image).

Therefore as the character is moving, the JSCrollPane is scrolling, so I don't have to scroll to it.

After you add the Icon to a different label you can use the scrollRectToVisible(..) method of the label. You will need to make the Rectangle size equal to the size of the scrollpanes viewport to force the viewport to scroll every time. So you will need to calculate the x/y position to make sure the label stays centered in the viewport.

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