在小程序中绘制画布
我目前有一个小型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Applet 是一个 容器,只是 添加您的画布那里。
Applet is a Container, just add your Canvas there.
一般来说,你拥有一个同时也是小程序的应用程序的方法是让你的入口点类扩展小程序,并让它的设置将画布添加到自身,等等。
然后,在主方法版本中,你只需实例化你的Applet 类并将其添加到新的 Frame(或 JApplet/JFrame 等)中。
请参阅此处和此处 查看该技术的示例,其本质上可归结为(从第一个示例开始):
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):
Canvas
不适合添加到 Swing 组件中。 请改用JComponent
(以及setOpaque(true)
)。Swing 组件应始终在 AWT 事件调度线程 (EDT) 上进行操作。 使用
java.awt.EventQueue.invokeLater
(invokeAndWait
对于小程序)。 您不应该从 EDT 执行任何阻塞操作,因此为此启动您自己的线程。 默认情况下,您在主线程(或小程序的小程序线程)中运行,这与 EDT 完全分开。我建议删除
MyCanvas
对JFrame
的任何依赖。 我还建议将使用框架的应用程序代码与使用小程序的应用程序代码分开。 将组件添加到JApplet
与JFrame
相同(在这两种情况下都存在恶作剧,实际上发生的是add
实际上调用 < code>getContentPane().add 这可能会导致一些不必要的混乱)。 主要区别在于您无法打包
小程序。Canvas
is inappropriate for adding to Swing components. UseJComponent
instead (andsetOpaque(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
toJFrame
. I also suggest keeping code for applications using frame separate from that using applets. Adding a component to aJApplet
is the same as forJFrame
(in both cases there is shenanigans where actually what happens is thatadd
actually callsgetContentPane().add
which can cause some unnecessary confusion). The main difference is that you can'tpack
an applet.