avatar
请教document# Java - 爪哇娇娃
g*j
1
想用textpane装载一段文字,只有ATCG四种字母,所有的A用绿色,T用红色,等等。
我用document实现了,可是当文字数目多的时候,刷新速度超慢,请问如何解决?
下面是代码
String content="AATGCAGCTAGCTAGCTAGCTA"; //may be over 10000 letters
SimpleAttributeSet attrs = new SimpleAttributeSet();
for(int i=0;i{
if(content.charAt(i)=='A')
StyleConstants.setBackground(attrs,Color.green);
else if(content.charAt(i)=='T')
StyleConstants.setBackground(attrs,Color.red);
else if(content.charAt(i)=='C')
StyleConstants.setBackground(attrs,Color.blue)
avatar
Z*e
2
does ur use case require a textpane? or a readonly component will do?
maybe you can try JLabel with HTML content

【在 g****j 的大作中提到】
: 想用textpane装载一段文字,只有ATCG四种字母,所有的A用绿色,T用红色,等等。
: 我用document实现了,可是当文字数目多的时候,刷新速度超慢,请问如何解决?
: 下面是代码
: String content="AATGCAGCTAGCTAGCTAGCTA"; //may be over 10000 letters
: SimpleAttributeSet attrs = new SimpleAttributeSet();
: for(int i=0;i: {
: if(content.charAt(i)=='A')
: StyleConstants.setBackground(attrs,Color.green);
: else if(content.charAt(i)=='T')

avatar
l*0
3

~~~~~~~~~~~~~~~~~~~~~~bioinformation?
每次刷新界面,这个for都要运行一遍吗?

【在 g****j 的大作中提到】
: 想用textpane装载一段文字,只有ATCG四种字母,所有的A用绿色,T用红色,等等。
: 我用document实现了,可是当文字数目多的时候,刷新速度超慢,请问如何解决?
: 下面是代码
: String content="AATGCAGCTAGCTAGCTAGCTA"; //may be over 10000 letters
: SimpleAttributeSet attrs = new SimpleAttributeSet();
: for(int i=0;i: {
: if(content.charAt(i)=='A')
: StyleConstants.setBackground(attrs,Color.green);
: else if(content.charAt(i)=='T')

avatar
c*t
4
Use custom paint routine is easier and better.

【在 g****j 的大作中提到】
: 想用textpane装载一段文字,只有ATCG四种字母,所有的A用绿色,T用红色,等等。
: 我用document实现了,可是当文字数目多的时候,刷新速度超慢,请问如何解决?
: 下面是代码
: String content="AATGCAGCTAGCTAGCTAGCTA"; //may be over 10000 letters
: SimpleAttributeSet attrs = new SimpleAttributeSet();
: for(int i=0;i: {
: if(content.charAt(i)=='A')
: StyleConstants.setBackground(attrs,Color.green);
: else if(content.charAt(i)=='T')

avatar
F*n
5
You gave too little information, however,
1. As someone mentioned before, try not to reset your document every time
you refresh the view;
2. If the data is large, JTextPane will be slow ANYWAY, because your texts
are very fragmented. If this is the case, you should use TextLayout to cache
styled texts and directly render them to a JComponent. This is the fastest
way in Java2D to render texts.

【在 g****j 的大作中提到】
: 想用textpane装载一段文字,只有ATCG四种字母,所有的A用绿色,T用红色,等等。
: 我用document实现了,可是当文字数目多的时候,刷新速度超慢,请问如何解决?
: 下面是代码
: String content="AATGCAGCTAGCTAGCTAGCTA"; //may be over 10000 letters
: SimpleAttributeSet attrs = new SimpleAttributeSet();
: for(int i=0;i: {
: if(content.charAt(i)=='A')
: StyleConstants.setBackground(attrs,Color.green);
: else if(content.charAt(i)=='T')

avatar
g*j
6
是的,这里的document是从texepane获得的,
Document doc=text_pane.getDocument();
我对html不熟啊

【在 Z****e 的大作中提到】
: does ur use case require a textpane? or a readonly component will do?
: maybe you can try JLabel with HTML content

avatar
g*j
7
每次刷新都要运行一次。而且可能是频繁刷新,因为内容更新了,取决于user的操作。

【在 l********0 的大作中提到】
:
: ~~~~~~~~~~~~~~~~~~~~~~bioinformation?
: 每次刷新界面,这个for都要运行一遍吗?

avatar
g*j
8
能进一步解释解释吗,多谢了。

【在 c*****t 的大作中提到】
: Use custom paint routine is easier and better.
avatar
g*j
9

看来我没法用document了,我不得不频繁更新document的内容,user每更新一次某个参
数,或者拖动scrollbar,document的文字就需要更新。
cache
fastest
Java2D 的TextLayout 看起来不错,我刚google了一下。问题是,这个比起直接在
jpanel
上用drawstring的优势在哪里呢?多谢。

【在 F****n 的大作中提到】
: You gave too little information, however,
: 1. As someone mentioned before, try not to reset your document every time
: you refresh the view;
: 2. If the data is large, JTextPane will be slow ANYWAY, because your texts
: are very fragmented. If this is the case, you should use TextLayout to cache
: styled texts and directly render them to a JComponent. This is the fastest
: way in Java2D to render texts.

avatar
g*j
10
正在看TextLayout,很强大,多谢!

cache
fastest

【在 F****n 的大作中提到】
: You gave too little information, however,
: 1. As someone mentioned before, try not to reset your document every time
: you refresh the view;
: 2. If the data is large, JTextPane will be slow ANYWAY, because your texts
: are very fragmented. If this is the case, you should use TextLayout to cache
: styled texts and directly render them to a JComponent. This is the fastest
: way in Java2D to render texts.

avatar
F*n
11
JTextPane stores text as string and attributes. Everytime it's painted, the
rendering engine must interpret and convert them into glyphs and paint
objects, which takes some time. On the contrary, a TextLayout stores glyphs
and paint objects directly.
In other words, attributed strings are like source code, TextLayout is like
compiled code, and thus faster.

【在 g****j 的大作中提到】
: 正在看TextLayout,很强大,多谢!
:
: cache
: fastest

avatar
g*j
12
万分感谢!!

the
glyphs
like

【在 F****n 的大作中提到】
: JTextPane stores text as string and attributes. Everytime it's painted, the
: rendering engine must interpret and convert them into glyphs and paint
: objects, which takes some time. On the contrary, a TextLayout stores glyphs
: and paint objects directly.
: In other words, attributed strings are like source code, TextLayout is like
: compiled code, and thus faster.

avatar
l*0
13
Good.

the
glyphs
like

【在 F****n 的大作中提到】
: JTextPane stores text as string and attributes. Everytime it's painted, the
: rendering engine must interpret and convert them into glyphs and paint
: objects, which takes some time. On the contrary, a TextLayout stores glyphs
: and paint objects directly.
: In other words, attributed strings are like source code, TextLayout is like
: compiled code, and thus faster.

avatar
g*j
14
textlayout还是很慢,或者我的代码有问题?才10万长的序列。
public void initValues(Graphics2D g)
{
HashMap dna2color=new HashMap();
dna2color.put('A',Color.green);
dna2color.put('T',Color.red);
dna2color.put('C',Color.blue);
dna2color.put('G',Color.orange);

StringBuffer s=new StringBuffer();
for(int i=0;i<10000;i++) s.append("ATCGATCGATAT");

AttributedString as=new AttributedString(s.toString());
System.out.println(3);
for(int i=0;i
【在 F****n 的大作中提到】
: JTextPane stores text as string and attributes. Everytime it's painted, the
: rendering engine must interpret and convert them into glyphs and paint
: objects, which takes some time. On the contrary, a TextLayout stores glyphs
: and paint objects directly.
: In other words, attributed strings are like source code, TextLayout is like
: compiled code, and thus faster.

avatar
F*n
15
你这个问题主要是每次都要重复create TextLayout, 10万个字母要10万次。其实就4个
TextLayout重复用就可以了.
public void initValues(Graphics2D g)
{
HashMap dna2tl =new HashMap();
TextLayout atl = ...//create a text layout for A
dna2tl.put('A',atl);
...
}
更快的方法可以直接用4个Image.
HashMap dna2img

【在 g****j 的大作中提到】
: textlayout还是很慢,或者我的代码有问题?才10万长的序列。
: public void initValues(Graphics2D g)
: {
: HashMap dna2color=new HashMap();
: dna2color.put('A',Color.green);
: dna2color.put('T',Color.red);
: dna2color.put('C',Color.blue);
: dna2color.put('G',Color.orange);
:
: StringBuffer s=new StringBuffer();

avatar
g*j
16
如果用HashMap,我在paint的时候只能按每个字母paint并给
每个
字母都需要计算位置,麻烦一些。而不是每个textlayout对应一行并按行paint,不过
解决
了速度问题,非常感谢。

【在 F****n 的大作中提到】
: 你这个问题主要是每次都要重复create TextLayout, 10万个字母要10万次。其实就4个
: TextLayout重复用就可以了.
: public void initValues(Graphics2D g)
: {
: HashMap dna2tl =new HashMap();
: TextLayout atl = ...//create a text layout for A
: dna2tl.put('A',atl);
: ...
: }
: 更快的方法可以直接用4个Image.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。