确保 AWT Canvas 在执行其他操作之前已经绘制在屏幕上。

huangapple go评论71阅读模式
英文:

Ensure that AWT Canvas is drawn on-screen before doing something else

问题

在我的应用程序中,我需要确保在进行下一步操作之前Canvas实际上已经出现在屏幕上。原因是我需要检索一个AWT Drawable,即该Canvas的本机窗口表面。如果Canvas不可见,即使设置了"visible"标志为true,该Drawable也将无效。

我的问题是我无法确定Java何时决定显示它。

我甚至编写了一个延迟Canvas构造函数后的代码,但问题是它在延迟过期后才显示。

canvas3d = new Canvas3d();
System.err.println("Before sleep");
try
{
   Thread.sleep(3000);
}
catch (Exception e) {}
System.err.println("After sleep");
canvas3d.Init();

这很奇怪,因为元素的绘制和渲染应该由一个完全独立的线程处理,理论上,我的主线程睡眠应该为它处理这些内容提供足够的时间。但出于某种原因,它仍然等待。

添加Repaint()或revalidate()也没有帮助。

这个问题是有时Canvas在Init()需要它可见之前显示,但其他时候它在那之前没有显示。

我是否可以强制应用程序在执行其他操作之前显示元素?或者我是否可以获取Canvas当前是否可见?因为检查Canvas.visible是没有意义的 - 无论它是true还是false,都不意味着它在运行时的那一刻实际上显示或隐藏。它只是一个标志,显示它应该被显示,但哎呀,糟糕,Java无法保证它确实显示!

英文:

In my application I NEED to make sure that Canvas is actually appeared on the screen before going for the next step. The reason is that I need to retrieve an AWT Drawable - i.e. the native window surface of that canvas. And that drawable would be invalid if the canvas is not visible, even if it has the flag "visible" set to true.

The problem Im having is that I can't be sure when Java actually decides to display it.

I even made a code to actually delay after Canvas constructor, but what happens is that it is displayed AFTER the delay has expired.

canvas3d = new Canvas3d();
System.err.println("Before sleep");
try
{
   Thread.sleep(3000);
}
catch (Exception e) {}
System.err.println("After sleep");
canvas3d.Init();

This is odd, because the drawing and rendering of elements should be handled by a completely separate thread, and my main thread sleeping should theoretically give it enough time to process the stuff. But it still waits for some reason.
Adding Repaint() or revalidate() doesn't help.

The problem with this is that sometimes, the canvas gets displayed before the Init() gets to the point that it needs it to be visible. But other times - it doesn't get displayed before that.

Can I somehow FORCE the application to display the element before I'm doing anything else? Or can I somehow retrieve if the canvas is displayed at the moment or not? Because checking Canvas.visible is pointless - whether it's true or false, it doesn't mean that it's actually displayed or hidden in that precise moment during runtime. It's just a flag showing that it's supposed to be displayed, but oh, oopsie, Java just can't guarantee that it actually is!

答案1

得分: 0

看起来我可以使用canvas.isShowing()来测试它是否真正显示在屏幕上。至少通过检查这个参数,我得到了我想要的结果(到目前为止)。

我仍然不确定是否足够,是否还有更多低级别的问题,特别是如果我尝试在X11渲染管线期间访问表面,此时AWT已经完全“显示”了组件,但X11尚未完成在屏幕上绘制它。这是一个担忧。

但就目前而言,这似乎是我能想到的最好的答案。

英文:

It seems that I can use canvas.isShowing() to test if it's actually visible on the screen. At least by checking that parameter I got the result I wanted (so far).

I am still not certain if that's enough and if there's something more that can go wrong on the low level, particularly if I try to access the surface during X11 rendering pipeline where AWT has already completely "displayed" the component but X11 hasn't yet finished drawing it on the screen.
That's one concern.

But for now, it seems to be the best answer I can come up with.

huangapple
  • 本文由 发表于 2020年7月31日 13:37:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63186317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定