View Full Version : JEditorPane.....il testo si può estrarre solo da URL?
qwerty_giggi
13-12-2006, 12:20
devo usare un JEditorPane perchè devo inserire una stringa con diversi font in una stessa area di testo.....ma il testo da inserire deve essere estratto solamente da un URL oppure posso inserlo manualmente?
ho visto che esiste il metodo setText(String)......ma questo imposta tutto il testo del JEditorPane.......
grazie
Puoi inserire la stringa text con:
Document doc = textComponent.getDocument();
int pos = doc.getEndPosition().getOffset();
try {
t.insertString(pos, text, null);
} catch(BadLocationException ex) {
throw new RuntimeException("This bad location ex shouldn't happen");
}
Il null in fondo sono gli attributi di stile applicati al testo inserito. Nel caso in cui sia diverso da null, se sono attributi di carattere valgono solo per il testo inserito, se sono attributi di paragrafo (tipo allineamento) valgono per tutto il paragrafo.
qwerty_giggi
13-12-2006, 15:06
ok....ma come faccio a costruire "AttributeSet" da passare al posto di null a "insertString"........anche il nome del font che voglio usare viene impostato nell ' AttributeSet?
Puoi usare un SimpleAttributeSet e StyleConstants. Ad esempio, se vuoi usare un Arial 20:
SimpleAttributeSet charAttributes = new SimpleAttributeSet();
StyleConstants.setFontFamily(charAttributes, "Arial");
StyleConstants.setFontSize(charAttributes, 20);
e poi passi quel charAttributes. Butta l'occhio a StyleConstants, ha un mezzo quintale di metodi.
qwerty_giggi
13-12-2006, 15:57
Vector VectorTextPane;
CICLO FOR SULLE AREE DI TESTO
lista=new CreaListaTextArea();
VectorTextPane=lista.CreaLista(VectorTextPane,proprieta,anchor); [^^]
public class CreaListaTextArea {
public Vector<JTextPane> CreaLista(Vector<JTextPane> VectorTextPane,RichTextRun[] propr,java.awt.Rectangle anchor){
JTextPane p=createTextPane(propr,anchor); [**]
VectorTextPane.add(p);
return VectorTextPane;
}
private JTextPane createTextPane(RichTextRun[] propr,java.awt.Rectangle anchor) {
JTextPane textComponent = new JTextPane();
textComponent.setSize(anchor.width,anchor.height);
textComponent.setLocation((int) anchor.getX(), (int) anchor.getY());
Document doc;
int size;
for (int i=0;i<propr.length;i++){
SimpleAttributeSet charAttributes = new SimpleAttributeSet();
StyleConstants.setFontFamily(charAttributes, propr[i].getFontName());
if(propr[i].getFontSize()==-1) size=18;
else size=propr[i].getFontSize();
StyleConstants.setFontSize(charAttributes, size);
if (propr[i].isBold()) StyleConstants.setBold(charAttributes,true);
if (propr[i].isItalic()) StyleConstants.setItalic(charAttributes,true);
if (propr[i].isUnderlined()) StyleConstants.setUnderline(charAttributes,true);
doc = textComponent.getDocument();
int pos = doc.getEndPosition().getOffset();
try {((Document) textComponent).insertString(pos, propr[i].getText(), charAttributes); [°]
} catch(BadLocationException ex) {throw new RuntimeException("This bad location ex shouldn't happen");}
}
return textComponent;
}
}
se eseguo l'errore è questo.....
Exception in thread "main" java.lang.ClassCastException: javax.swing.JTextPane
at CreaListaTextArea.createTextPane(CreaListaTextArea.java:48)----->riga [°]
at CreaListaTextArea.CreaLista(CreaListaTextArea.java:23) -->riga [**]
at Convert.main(Convert.java:81) ----->riga [^^]
inserendo le solite stampe si vede che il vettore RichTextRun che passo a lista.CreaLista è inizializzato correttamente.....la posizione della prima area di testo è 1........le altre non lo so visto che si blocca alla prima textArea ............
Nel codice che ho incollato c'era una t al posto di doc. Spero che NON sia stato quello a farti cadere in trappola :D
Male:
try {((Document) textComponent).insertString...
Bene:
try {doc.insertString...
qwerty_giggi
13-12-2006, 16:23
ops.....ma il setLineWrap va usato anche per le JTextPane?
se l'area di testo è di due righe mi visualizza solo la prima e metà della seconda.....se è di una sola riga me ne visualizza metà...ilsalvataggio nella Graphics2D l'ho lasciato uguale a quando usavo le JTextArea....
int capacita=VectorTextPane.size();
myImage = new BufferedImage(640,480,BufferedImage.TYPE_INT_BGR);
g2 = myImage.createGraphics();
g2.fillRect(0, 0, 640, 480);
for(int i=0;i<capacita;i++)
{g2.translate(VectorTextPane.elementAt(i).getX(), VectorTextPane.elementAt(i).getY());
VectorTextPane.elementAt(i).print(g2);
g2.translate(-VectorTextPane.elementAt(i).getX(),-VectorTextPane.elementAt(i).getY());
}
try{OutputStream out = new FileOutputStream("immagine1.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
}catch(Exception e){System.out.println(e);}
setLineWrap non c'è. Un JTextPane "wrappa" da solo, basta che gli imposti sia la dimensione corrente (setSize) sia la dimensione preferita (setPreferredSize). Usa lo stesso valore per entrambi.
qwerty_giggi
13-12-2006, 17:06
ho inserito queste due linee subito dopo .setSize(anchor.width,anchor.height)
Dimension dim=new Dimension(anchor.width,anchor.height);
textComponent.setPreferredSize(dim);
il risultato è sempre lo stesso.....
Strano, qui funziona. Io ho impostato la dimensione dopo aver inserito il testo. Forse è questo? O anche tu applichi la dimensione dopo?
qwerty_giggi
13-12-2006, 17:38
le ho inserite dopo il ciclo for che setta la JTextPane......quindi dopo che tutto il testo è stato inserito.....
qwerty_giggi
13-12-2006, 18:08
questo è ilcodice per la creazione delle JTextPane......
public class CreaListaTextArea {
public Vector<JTextPane> CreaLista(Vector<JTextPane> VectorTextPane,RichTextRun[] propr,java.awt.Rectangle anchor){
JTextPane p=createTextPane(propr,anchor); [**]
VectorTextPane.add(p);
return VectorTextPane;
}
private JTextPane createTextPane(RichTextRun[] propr,java.awt.Rectangle anchor) {
Dimension dim=new Dimension(anchor.width,anchor.height);
JTextPane textComponent = new JTextPane();
textComponent.setLocation((int) anchor.getX(), (int) anchor.getY());
Document doc;
int size;
String fontName;
for (int i=0;i<propr.length;i++){
if(propr[i].getFontSize()==-1) size=18;
else size=propr[i].getFontSize();
StyleConstants.setFontSize(charAttributes, size);
if(propr[i].getFontName()=="null") fontName="Arial";
else fontName=propr[i].getFontName();
StyleConstants.setFontFamily(charAttributes, fontName);
if (propr[i].isBold()) StyleConstants.setBold(charAttributes,true);
if (propr[i].isItalic()) StyleConstants.setItalic(charAttributes,true);
if (propr[i].isUnderlined()) StyleConstants.setUnderline(charAttributes,true);
doc = textComponent.getDocument();
int pos = doc.getEndPosition().getOffset();
try {doc.insertString(pos, propr[i].getText(), charAttributes);
} catch(BadLocationException ex) {throw new RuntimeException("This bad location ex shouldn't happen");}
}
textComponent.setSize(anchor.width,anchor.height);
textComponent.setPreferredSize(dim);
return textComponent;
}
}
il resto è uguale (salvataggio sulla Graphics2D)......ho provato anche a impostare di nuovo la dimensione dopo [**]....ma è uguale.....
Niente, non riesco a ottenere lo stesso comportamento che riscontri. Ho fatto due esperimenti rapidi e va. Non conta neanche l'ordine di inserimento testo-dimensioni. Va sempre, 'sto disgraziato.
qwerty_giggi
14-12-2006, 12:25
ho risolto modificando la dimensione della JTextPane....
textComponent.setSize(anchor.width+25,anchor.height+30);
va bene così.......
però non riesco a capire perchè se il font (anche di una sola parola) è Arial mi restuisce questo errore......
Exception in thread "main" java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at javax.swing.text.SimpleAttributeSet.addAttribute(Unknown Source)
at javax.swing.text.StyleConstants.setFontFamily(Unknown Source)
at CreaListaTextArea.createTextPane(CreaListaTextArea.java:49)
at CreaListaTextArea.CreaLista(CreaListaTextArea.java:14)
at Convert.main(Convert.java:82)
riga 49---->StyleConstants.setFontFamily(charAttributes, fontName);
ho anche inserito questo controllo.....
if(propr[i].getFontName()=="null") fontName="Arial";
else fontName=propr[i].getFontName();
StyleConstants.setFontFamily(charAttributes, fontName);
niente....?
qwerty_giggi
15-12-2006, 11:55
ok...sembra che ora se scelgo "arial" è ok.....al posto di "null" era null.........
ma perchè se il FontColor è:
FontColor-->100663296 (celeste) [System.out.println("FontColor-->"+propr[i].getFontColor());]
se poi stampo il colore che creo con l'istruzione:
Color col=new Color(propr[i].getFontColor());
StyleConstants.setForeground(charAttributes, col);
il risultato è questo.....
java.awt.Color[r=0,g=0,b=0]
infatti poi la scritta nella JTextPane è nera.......coma mai?qualche colore forse non viene riconosciuto?o comunque non c'è una corrispondenza per tutti i colori usati in PowerPoint con quelli poi delle Java2D?
100663296. Mi fido del Celeste, ma in che formato? Non è un ARGB 32 che è quello usato per la costruzione di un java.awt.Color da un intero.
qwerty_giggi
15-12-2006, 12:45
ARGB 32???? come faccio a vederlo??
Guarda la documentazione del metodo getFontColor delle librerie che usi. Dovrebbe esserci scritto qualcosa (spero).
qwerty_giggi
19-12-2006, 12:13
Questa è la documentazione del metodo........
getFontColor
public int getFontColor()
Returns:
font color as RGB value
See Also:
Color
l'indirizzo è......
http://jakarta.apache.org/poi/apidocs/org/apache/poi/hslf/usermodel/RichTextRun.html#getFontColor()
Che dire, una documentazione veramente esauriente.
Per quanto ne so 100663296 non è celeste neanche a piangere in cinese.
Sarebbe celeste un BGR 24 bit prodotto dalla trasformazione (C = 100663296)
int BGR24 = ((C - (int)1e8) * - 1) >>> 8;
Dal che otterresti un RGB32 utile alla creazione di un Color con:
int ARGB32 =
(((val >> 0) & 0xff) << 16) +
(((val >> 8) & 0xff) << 8) +
(val & 0xff);
e dall'ARGB32 ottieni sì un color celeste passando per
Color color = new Color(ARGB32);
Ma è una relazione che non ho mai sentito e potrebbe anche essere del tutto casuale.
Sei sicuro che 100663296 sia celeste?
qwerty_giggi
19-12-2006, 16:45
in allegato c'è la presentazione salvata come .jpg.....
la scritta "soluzione3" me la salva gialla.......e l'intero che restituisce con getFontColor() è:
NomeFont--->null
FontColor-->-16777472
FontSize--->40
java.awt.Color[r=255,g=255,b=0]
ho provato ad inserire questo:
col=new Color(propr[i].getFontColor());
float red=col.getRed();
float green=col.getGreen();
float blue=col.getBlue();
Color col1=new Color(red,green,blue ); [**]
e l'errore è questo.....
Exception in thread "main" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Green
at java.awt.Color.testColorValueRange(Unknown Source)
at java.awt.Color.<init>(Unknown Source)
at java.awt.Color.<init>(Unknown Source)
at java.awt.Color.<init>(Unknown Source)
at CreaListaTextArea.createTextPane(CreaListaTextArea.java:67) [**]
at CreaListaTextArea.CreaLista(CreaListaTextArea.java:21)
at Convert.main(Convert.java:74)
quindi,non lo so,ma devo cercare una conversione dall'RGB Standard a qualche altro tipo di RGB???
Non se che razza di RGB avessero in mente quelli che hanno fatto il metodo getFontColor. So solo che non è lo stesso RGB che va bene per creare un java.awt.Color. Prova a vedere nel codice sorgente delle librerie, magari c'è scritto qualcosa di più di quel "RGB" nella documentazione, che non vuol dir nulla.
qwerty_giggi
20-12-2006, 10:24
Questo è il sorgente dei metodi per prendere\impostare il colore del font.....
/** @return font color as RGB value
@see java.awt.Color
*/
public int getFontColor() {
return getCharTextPropVal("font.color");
}
/**
* Sets color of the text, as a RGB value
* @see java.awt.Color
*/
public void setFontColor(int rgb) {
setCharTextPropVal("font.color", rgb);
}
qui parla di RGB.....ci rinuncio.....
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.