Ich bin gerade dabei die Seitenklasse umzuschreiben, dazu eine kurze Zwischenfrage:
Gibt es irgendeine Möglichkeit, an die Länge eines Wortes zu kommen, also an ein metrics-Objekt, bevor die Seite erzeugt wurde (Also ohne deren Graphics g zu kennen)?
Ich habe jetzt folgende Klasse “Chordstruct” geschrieben, die den String vorher schon so aufteilt, dass er eigentlich sehr komfortabel zu zeichnen wäre, wenn die metrics irgendwie schon bestimmbar wären:
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.font.TextLayout;
import java.util.Vector;
import org.w3c.dom.Node;
public class Liedstruct {
protected Node txt;
protected String modus;
protected Vector<zeile> zeilen;
int Chordsize, Headsize, Fontsize, chordHeight, headHeight, textHeight,
rand, hindex;
Font Chordfont, Headfont, Textfont;
FontMetrics chordmetrics, headmetrics, textmetrics;
String Schriftart;
Graphics2D gd;
TextLayout Tl;
public Liedstruct(Node t, String m, Graphics2D g) {
txt = t;
modus = m;
gd = g;
updateFonts();
Tl.getOutline(null).getBounds();
zeilen = new Vector<zeile>();
setZeilen();
}
protected void updateFonts() {
Schriftart = MainProgramm.Schriftart;
Chordsize = Integer.parseInt(MainProgramm.Chordsize);
Headsize = Integer.parseInt(MainProgramm.HeadSize);
Fontsize = Integer.parseInt(MainProgramm.Fontsize);
Chordfont = new Font(Schriftart, Font.PLAIN, Chordsize);
Headfont = new Font(Schriftart, Font.ITALIC, Headsize);
Textfont = new Font(Schriftart, Font.PLAIN, Fontsize);
gd.setFont(Chordfont);
chordmetrics = gd.getFontMetrics();
chordHeight = chordmetrics.getMaxAscent();
gd.setFont(Headfont);
headmetrics = gd.getFontMetrics();
headHeight = headmetrics.getMaxAscent();
gd.setFont(Textfont);
textmetrics = gd.getFontMetrics();
textHeight = textmetrics.getMaxAscent();
rand = 5;
}
private void setZeilen() {
for (int i = 0; i < txt.getChildNodes().getLength(); i++) {
if (txt.getChildNodes().item(i).getNodeName().compareTo("struct") == 0) {
// Ueberschrift der Struktur, z.b. Strophe, Chorus, ...
zeilen.add(new zeile(txt.getChildNodes().item(i)
.getAttributes().getNamedItem("name").getNodeValue(),
"Ueberschrift"));
for (int y = 0; y < txt.getChildNodes().item(i).getChildNodes()
.getLength(); y++) {
if (txt.getChildNodes().item(i).getChildNodes().item(y)
.getNodeName().compareTo("ln") == 0) {
// Strukturinhalt
zeilen.add(new zeile(txt.getChildNodes().item(i)
.getChildNodes().item(y).getNodeValue(),
"TextChord"));
}
}
}
}
}
class zeile {
private Vector<wort> woerter;
private String typ, text;
private int gesamtlaenge, hoehe;
protected zeile(String tx, String ty) {
typ = ty;
text = tx;
gesamtlaenge = 0;
hoehe = 0;
if (typ.compareTo("Ueberschrift") != 0) {
hoehe = chordHeight + textHeight + (chordHeight / 2);
buildWords();
}else{
hoehe = headHeight + (int)(0.5 * headHeight);
}
}
private void buildWords() {
boolean tagon = false;
boolean textwrite = false;
String worttext = "";
for (int x = 0; x < text.length(); x++) {
if (text.charAt(x) == '{') {
tagon = true;
} else if (text.charAt(x) == '}') {
tagon = false;
} else if (!tagon && text.charAt(x) == ' ' && textwrite) {
//Absoluten Index feststellen:
woerter.add(new wort(worttext, gesamtlaenge));
gesamtlaenge = gesamtlaenge + woerter.lastElement().getLaenge();
textwrite = false;
worttext = "" + text.charAt(x);
} else if(!textwrite){
worttext = worttext + text.charAt(x);
} else {
worttext = worttext + text.charAt(x);
textwrite = true;
}
if(x == text.length()-1 && !textwrite){
woerter.add(new wort(worttext, gesamtlaenge));
worttext = "" + text.charAt(x);
}
}
}
protected int getLaenge() {
return gesamtlaenge;
}
protected int getHoehe() {
return hoehe;
}
protected Vector<wort> getZeile() {
return woerter;
}
protected String getPlainText() {
return text;
}
protected String getTyp() {
return typ;
}
}
/**
* Ein Wort besteht aus dem Worttext, dessen Position und den Akkorden mit
* ihren Positionen, relativ zum Wortanfang.
*
* @author Sebi
*
*/
class wort {
private String text;
private String worttext;
private Vector<chord> chords;
private int index, laenge;
protected wort(String t, int i) {
text = t;
chords = new Vector<chord>();
worttext = "";
index = i;
laenge = 0;
buildWort();
}
private void buildWort() {
boolean chordon = false;
String chordtext = "";
for (int x = 0; x < text.length(); x++) {
if (text.charAt(x) == '{') {
chordon = true;
} else if (text.charAt(x) == '}') {
chordon = false;
//Relativen Index feststellen:
int chordindex = (textmetrics.stringWidth(worttext))-((int)(0.5*chordmetrics.stringWidth(chordtext)));
chords.add(new chord(chordtext, chordindex));
chordtext = "";
} else if (chordon) {
chordtext = chordtext + text.charAt(x);
} else {
worttext = worttext + text.charAt(x);
}
}
laenge = textmetrics.stringWidth(worttext);
if(laenge<(chords.lastElement().getIndex()+(int)(0.5*chordmetrics.stringWidth(chords.lastElement().getChord())))){
laenge = (chords.lastElement().getIndex()+(int)(0.5*chordmetrics.stringWidth(chords.lastElement().getChord())));
}
}
protected int getLaenge(){
return laenge;
}
protected Vector<chord> getChords() {
return chords;
}
protected int getIndex() {
return index;
}
protected String getWorttext() {
return worttext;
}
class chord {
private int index;
private String chord;
protected chord(String c, int i) {
chord = c;
index = i;
}
protected int getIndex() {
return index;
}
protected String getChord() {
return chord;
}
}
}
}