How do I move with a character in Jframe Java?

Asked

Viewed 110 times

-1

I wanted to know if how to make a camera for character when it reaches the end of the panel start going along with the character, because when setting a size this.setSize(500,500); the character when passing this mark it disappears and if you limit the drive it gets stuck in that square.

  • Hello. Please provide more details. What is the "end of the panel"? You are using a panel to simulate a horizontal scrolling, is that it? Edit the question to provide more information so someone can help you, such as adding images of the screen you have and want and the relevant code snippet.

1 answer

0

Since you have a large Jpanel, it is possible that you have already added it to a Jscrollpane, so you can "see" the entire Jpanel, according to the bar, if you have not done this, do a search in Jscrollpanes, but basically you will do the following:

Container cp=getContentPane();
JPanel pnJogo=new JPanel();//aqui vai ficar a interface do jogo
JScrollPane sc=new JScrollPane(pnJogo);//adicionando o painel ao ScrollPane

After this, you can set the value of your Scroll as you add, but from this point, the criterion of setValue is with you!

final JScrollBar bar1 = seuScroll.getHorizontalScrollBar();
bar1.setValue(10);//isso pode estar dentro de um for, ou um ActionListener também, por exemplo.

And, it would be a good idea too, you block the Scrollpane, so that the user can not "analyze" the phase before going forward, because then, take the "emotion" of the game, I apologize, but this is an idea, I do not know how to "block" the bar (if anyone knows, feel free to edit the answer)

an example of how you can do this, then comfort the character while walking, you will increase the value of the scrollbar

  • I think this will not solve since jpanel does not have scrollbar by itself.

  • from what I understand, the Jpanel where the game is, is a large Jpanel, so it is not possible to "put it on the screen", so the solution would be to add this Jpanel in a Jscrollpane and only then add the Jscrollpane in his Container, if you have done this, his life will facilitate, because the way to show the stage, he already has, just block the editing of the same, so he does not see "the front" of the stage and, from that go setting as the character advances

  • 1

    Yes, but that was not explained in the reply. As it is, will not help much, maybe editing and providing these details that you just comment, the response is more complete and useful to OP.

  • All right then, I’ll do it :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.