在小程序中绘制画布

发布于 2024-07-11 15:12:07 字数 566 浏览 4 评论 0原文

我目前有一个小型 Java 程序,我想在桌面上(即在 JFrame 中)和小程序中运行它。 目前,所有绘图和逻辑均由扩展 Canvas 的类处理。 这为我提供了一个非常好的桌面应用程序的主要方法:

public static void main(String[] args) {
    MyCanvas canvas = new MyCanvas();
    JFrame frame = MyCanvas.frameCanvas(canvas);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.loop();
}

我可以为小程序做类似的事情吗? 理想情况下,MyCanvas 在这两种情况下都保持相同。

不确定它是否重要,但我正在使用 BufferStrategy 和 setIgnoreRepaint(true) 进行绘制。

编辑:澄清一下,我的问题似乎是绘制画布 - 因为所有绘制都是通过 canvas.loop() 调用完成的。

I currently have a small Java program which I would like to run both on the desktop (ie in a JFrame) and in an applet. Currently all of the drawing and logic are handled by a class extending Canvas. This gives me a very nice main method for the Desktop application:

public static void main(String[] args) {
    MyCanvas canvas = new MyCanvas();
    JFrame frame = MyCanvas.frameCanvas(canvas);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.loop();
}

Can I do something similar for the applet? Ideally MyCanvas would remain the same for both cases.

Not sure if its important but I am drawing using BufferStrategy with setIgnoreRepaint(true).

Edit: To clarify, my issue seems to be painting the canvas -- since all the painting is being done from the canvas.loop() call.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

青朷 2024-07-18 15:12:07

Applet 是一个 容器,只是 添加您的画布那里。

Applet is a Container, just add your Canvas there.

清风挽心 2024-07-18 15:12:07

一般来说,你拥有一个同时也是小程序的应用程序的方法是让你的入口点类扩展小程序,并让它的设置将画布添加到自身,等等。

然后,在主方法版本中,你只需实例化你的Applet 类并将其添加到新的 Frame(或 JApplet/JFrame 等)中。

请参阅此处此处 查看该技术的示例,其本质上可归结为(从第一个示例开始):

  public static void main(String args[])
  {
    Applet applet = new AppletApplication();
    Frame frame = new Frame();
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });

    frame.add(applet);
    frame.setSize(IDEAL_WIDTH,IDEAL_HEIGHT);
    frame.show();
  }

Generally, the way you would have an application that is also an applet is to have your entry point class extend Applet, and have its setup add the Canvas to itself, etc. etc.

Then, in the main method version, you just instantiate your Applet class and add it to a new Frame (or JApplet / JFrame, etc.).

See here and here for examples of the technique, which essentially boils down to (from the first example):

  public static void main(String args[])
  {
    Applet applet = new AppletApplication();
    Frame frame = new Frame();
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });

    frame.add(applet);
    frame.setSize(IDEAL_WIDTH,IDEAL_HEIGHT);
    frame.show();
  }
指尖凝香 2024-07-18 15:12:07

Canvas 不适合添加到 Swing 组件中。 请改用 JComponent(以及 setOpaque(true))。

Swing 组件应始终在 AWT 事件调度线程 (EDT) 上进行操作。 使用java.awt.EventQueue.invokeLaterinvokeAndWait 对于小程序)。 您不应该从 EDT 执行任何阻塞操作,因此为此启动您自己的线程。 默认情况下,您在主线程(或小程序的小程序线程)中运行,这与 EDT 完全分开。

我建议删除 MyCanvasJFrame 的任何依赖。 我还建议将使用框架的应用程序代码与使用小程序的应用程序代码分开。 将组件添加到 JAppletJFrame 相同(在这两种情况下都存在恶作剧,实际上发生的是 add 实际上调用 < code>getContentPane().add 这可能会导致一些不必要的混乱)。 主要区别在于您无法打包小程序。

Canvas is inappropriate for adding to Swing components. Use JComponent instead (and setOpaque(true)).

Swing components should always be manipulated on the AWT Event Dispatch Thread (EDT). Use java.awt.EventQueue.invokeLater (invokeAndWait for applets). You shouldn't do any blocking operations from the EDT, so start your own threads for that. By default you are running in the main thread (or applet thread for applets), which is quite separate from the EDT.

I suggest removing any dependence from your MyCanvas to JFrame. I also suggest keeping code for applications using frame separate from that using applets. Adding a component to a JApplet is the same as for JFrame (in both cases there is shenanigans where actually what happens is that add actually calls getContentPane().add which can cause some unnecessary confusion). The main difference is that you can't pack an applet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文