-1

我确定不是,但我在考虑替代方法时感到头疼。我有一个带有背景和字符线程的视频游戏面板。角色每 100 毫秒不断改变他的位置,我想在屏幕上显示他所做的每一步。我可以很容易地将 gamePanel 作为参数传递给角色,但是当我这样做时我感觉很脏,因为我知道这是错误的方式。有什么帮助吗?

4

2 回答 2

1

There's nothing wrong per se with passing the JPanel as a parameter to a method since it's an object like any other, but I do worry about how you're drawing on the JPanel with this. You're not calling getGraphics() are you? I hope not.


Edit
You state/I reply:

The fact is that I was trying different solutions and now I don't have the version of the code in which I was using JPanel as a reference.

So it's a non-issue then?

Anyway, in that version everything was fine. I was just worried since in an hypothetical situation with more threads, it wouldn't sound so safe to me to have a shared JPanel for all. Hence the main question.

Threading is almost never an issue so long as you take care to make any and all Swing calls on the Swing event thread, and call all long-running code on a background thread. I don't see how this relates to your original question though about passing the JPanel in as a parameter.

If you still have a question, you will want to clarify it further as you're still talking about murky mumbo-jumbo that we have no access to.

于 2013-10-30T18:32:13.457 回答
1

JPanel 只是一个类。您可以将任何类的对象作为参数传递给方法。如果要将其传递给另一个类中的方法,则必须小心访问另一个类。但是只是传递 JPanel 的对象是完全可以的

于 2013-10-30T18:41:15.040 回答