绘制 JComponent 而不将其添加到容器中

发布于 2024-11-15 16:44:13 字数 436 浏览 3 评论 0原文

我实现了一个自定义 JPanel,我已扩展其绘制方法以在全屏模式下进行大量手动渲染。现在我想将另一个 JComponent 集成到此(在我的例子中是一个 JPanel ,其中包含一个 JScrollpane 和一个 JTextPane > 作为其视口)应该出现在我的第一个面板的顶部,但由于我的自定义渲染管道很复杂,因此将 JComponent 添加到我的面板并通过 AWT 系统以传统方式绘制它是不可行的一个选项(我尝试过,它是最好的情况是古怪,最坏的情况是不起作用),所以我的问题是:是否可以通过调用其常规绘制方法来手动命令 JComponent 在程序中的某一点进行绘制,而不将其绑定到JContainer 如果是,我该怎么做?

预先感谢您的回答。

I've implemented a custom JPanel, whose paint method I've extended to do a lot of manual rendering in full screen mode. Now I would like to integrate another JComponent to this (in my case a JPanel that contains a JScrollpane with a JTextPane as its viewport) that should appear on top of my first panel, but because my custom rendering pipeline is complex, adding the JComponent to my panel and having it painted the traditional way through the AWT system is not an option (I tried and it's quirky at best, not functional at worst), so my question is: is it possible to manually order the JComponent to be painted at one point in my program by calling its regular paint method without tying it to a JContainer and if yes, how do I do this?

Thanks in advance for your answers.

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

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

发布评论

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

评论(5

趁年轻赶紧闹 2024-11-22 16:44:13

请参阅此线程上的 LabelRenderTest.java 源代码。标签最终会绘制到屏幕上,但在显示之前会绘制到 BufferedImage 上。

源代码的重要行是..

textLabel.setSize(textLabel.getPreferredSize());

See the LabelRenderTest.java source on this thread. The label is eventually drawn to screen, but it is painted to BufferedImage before ever being displayed.

The important line of the source is..

textLabel.setSize(textLabel.getPreferredSize());
对你再特殊 2024-11-22 16:44:13

您可以查看 CellRendererPane 并了解 BasicTableUI 如何用它绘制组件图像。

You can take a look at CellRendererPane and see how for example BasicTableUI paints component images with it.

森林迷了鹿 2024-11-22 16:44:13

是的,只需调用对象上的普通绘制方法并传递您想要其绘制的图形即可。然而,这只是要绘制它,听起来您希望它可以滚动,这意味着您需要将其添加到自定义 JPanel 中。在这种情况下,只需添加面板和布局管理器即可将组件放置在您需要的位置。

Yes, just call the normal paint method on the object and pass the Graphics you want it to paint on. However, this is just going to paint it and it sounds like you want it to possibly scroll which means you will need to add it to your custom JPanel. In that case just add the panel and you a layout manager that will place the component where you need it.

痕至 2024-11-22 16:44:13

您应该设置组件的大小。然后,要定位它,请使用图形的平移(x,y)将组件定位在所需的点处。

You should set size for the component. Then to position it use your Graphics' translate(x,y) to position the component in desired Point.

小耗子 2024-11-22 16:44:13

如果层次结构中有任何更高级别的容器,您可以使用

validate(); repaint(); 

pair 来做到这一点。

如果没有,您可以在最后更改它的大小或边界(如 +1 、 -1 )以使其重新绘制自身。

if there is any container higher level in the hierarchy you can use

validate(); repaint(); 

pair to do that.

if not you can change it's size or bounds ( like +1 , -1 ) at the end to make it repaint itself.

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