如何从小程序中的另一个方法调用重绘?

发布于 2024-11-02 17:05:19 字数 225 浏览 3 评论 0原文

我正在用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗喜 2024-11-09 17:05:19

您需要对要调用其方法的对象的引用。一种方法是通过调用类中的构造函数参数将主小程序对象传递给调用类。请注意,这与 GUI 编程无关,而与基本 Java 有关。

例如:

import javax.swing.JApplet;

public class FooApplet extends JApplet {
   public void init() {
      try {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               createGUI();
            }
         });
      } catch (Exception e) {
         System.err.println("createGUI didn't successfully complete");
      }
   }

   private void createGUI() {
      OtherClass otherClass = new OtherClass(this);

   }
}

class OtherClass {
   private FooApplet fooApplet;

   public OtherClass(FooApplet fooApplet) {
      this.fooApplet = fooApplet;  // now I can call methods on the FooApplet object
   }

   public void myMethod() {
      // ... some code here...
      fooApplet.repaint(); // now that I have a valid ref I can call repaint
   }
}

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:

import javax.swing.JApplet;

public class FooApplet extends JApplet {
   public void init() {
      try {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               createGUI();
            }
         });
      } catch (Exception e) {
         System.err.println("createGUI didn't successfully complete");
      }
   }

   private void createGUI() {
      OtherClass otherClass = new OtherClass(this);

   }
}

class OtherClass {
   private FooApplet fooApplet;

   public OtherClass(FooApplet fooApplet) {
      this.fooApplet = fooApplet;  // now I can call methods on the FooApplet object
   }

   public void myMethod() {
      // ... some code here...
      fooApplet.repaint(); // now that I have a valid ref I can call repaint
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文