简体   繁体   English

如果我组件的preferredSize比Integer.MAX_VALUE高怎么办?

[英]What if the preferredSize of my component is taller than Integer.MAX_VALUE?

I have written a hex viewer. 我写了一个十六进制查看器。

However, I found a limitation in Swing. 但是,我在Swing中发现了一个限制。 If you have a 3GB file and you're rendering at 20 pixels per row, the total height of the component is: 如果您有3GB的文件,并且以每行20像素的速度进行渲染,则组件的总高度为:

(3GB / 16 bytes per row) * 20 pixels per row = 3.75GB (3GB /每行16个字节)*每行20个像素= 3.75GB

At this point, Swing fails to render anything because the value overflows Integer.MAX_VALUE . 此时,Swing无法呈现任何内容,因为该值溢出了Integer.MAX_VALUE

This is my best idea so far: 到目前为止,这是我最好的主意:

  • Clone enough of Java2D and JComponent to make an alternative to JComponent which does all rendering in long coordinates. 克隆足够多的Java2D和JComponent,以替代JComponent,后者以长坐标进行所有渲染。
  • Clone JViewport so that you have a way to render the view of the component/ 克隆JViewport,以便您可以呈现组件视图/
  • Put that alternative JViewport into a JScrollPane like you would normally do. 像通常一样,将替代JViewport放入JScrollPane中。

This seems like a lot of work, but such a component might be useful for things like large diagrams or large text documents. 这似乎是一项艰巨的工作,但是这样的组件可能对大型图表或大型文本文档有用。

I considered just paging it, but it seems like it would be awkward when you're using the Find feature and it matches text crossing a page boundary. 我考虑过只是对它进行分页,但是当您使用“查找”功能并且它与跨越页面边界的文本匹配时,看起来会很尴尬。

The (non-Java) hex viewer I normally use (Hex Fiend) doesn't use paging either. 我通常使用的(非Java)十六进制查看器(Hex Fiend)也不使用分页。 It just scrolls the whole height of the document as if it's not a problem. 它只是滚动文档的整个高度,就好像这不是问题一样。

Does anyone know a good way to get around this sort of thing? 有谁知道解决这类问题的好方法?

You don't need to draw your hex viewer in long coordinates because you only need to draw the part of the file that you want to be visible at the moment. 您不需要在长坐标中绘制十六进制查看器,因为您只需要绘制文件中当前希望显示的部分。 JScrollPane is a very useful tool for scrolling around large components, but you don't need it. JScrollPane是用于滚动大型组件的非常有用的工具,但是您不需要它。 If you want to scroll through so much stuff that it's troublesome to have a component big enough to hold it all, then you can simply do the scrolling by making a JScrollBar and painting your component according to the position of the scroll bar. 如果您要滚动的内容太多,以至于没有足够大的组件来容纳所有组件的麻烦,那么只需制作一个JScrollBar并根据滚动条的位置绘制组件即可进行滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM