g****j 发帖数: 24 | 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) |
Z****e 发帖数: 2999 | 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')
|
l********0 发帖数: 283 | 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')
|
c*****t 发帖数: 1879 | 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')
|
F****n 发帖数: 3271 | 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')
|
g****j 发帖数: 24 | 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
|
g****j 发帖数: 24 | 7 每次刷新都要运行一次。而且可能是频繁刷新,因为内容更新了,取决于user的操作。
【在 l********0 的大作中提到】 : : ~~~~~~~~~~~~~~~~~~~~~~bioinformation? : 每次刷新界面,这个for都要运行一遍吗?
|
g****j 发帖数: 24 | 8 能进一步解释解释吗,多谢了。
【在 c*****t 的大作中提到】 : Use custom paint routine is easier and better.
|
g****j 发帖数: 24 | 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.
|
g****j 发帖数: 24 | 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.
|
|
|
F****n 发帖数: 3271 | 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
|
g****j 发帖数: 24 | 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.
|
l********0 发帖数: 283 | 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.
|
g****j 发帖数: 24 | 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.
|
F****n 发帖数: 3271 | 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();
|
g****j 发帖数: 24 | 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.
|