如何从小程序中的另一个方法调用重绘?
我正在用java编写一个游戏(一个小程序),并且有两个线程正在运行。一个线程在主类中运行,另一个线程是一个单独的类,绘制到公共变量类中的图形变量,以供所有其他类读取。 (主类读取图形变量并可以将其绘制(作为图像)。)
我希望能够从其他类调用主小程序的 repaint() 方法,但我不知道该怎么做因为调用“Main_applet_class”.repaint() 方法会导致“你不能从静态上下文中调用此方法”错误。帮助!
I'm writing a game (an applet) in java, and I have two threads running. One thread is running in the main class, and the other is a separate class drawing to a graphics variable in a class of public variables for all of the other classes to read. (The main class reads the graphics variable and can paint it (as an image).)
I'd like to be able to call the repaint() method for the main applet from the other class, but I have no idea how to do so because calling the "Main_applet_class".repaint() method results in a "You-can't-call-this-method-from-a-static-context" error. Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要对要调用其方法的对象的引用。一种方法是通过调用类中的构造函数参数将主小程序对象传递给调用类。请注意,这与 GUI 编程无关,而与基本 Java 有关。
例如:
You need a reference to the object that you want to call a method on. One way is to pass the main applet object to the calling class via a constructor parameter in the calling class. Note that this has nothing to do with GUI programming and all to do with basic Java.
For example: