1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| /*
* TestSVGDocument.java
*
* Created on 23 juillet 2007, 18:51
*/
package be.cercle.common.swing;
import java.awt.Font;
import java.awt.FontMetrics;
import javax.swing.JFrame;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.swing.JSVGCanvas;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;
/**
* Classe permettant la visualisation d'un canevas
*
* Zoom + : Ctrl + click droit maintenu pour sélectionner texte ou Shift + click gauche en descendant
* Zoom - : Shift + click gauche en montant
* Déplacer : Shift + click droit
* Rotation : Ctrl + click gauche en déplaçant la souris
* @author catha
*/
public class TestSVGDocument extends JFrame {
SVGDocument svgDoc = null;
JSVGCanvas jsvgCanvas = null;
Document doc = null;
StringBuffer sb = new StringBuffer("Descriptif du livre Nana (Emile Zola) " +
"Anna Coupeau, fille de Gervaise Macquart, appartient à la dernière génération dune lignée abâtardie " +
"par lalcool et le dérèglement psychique. Issue des faubourgs, elle devient, grâce à sa beauté troublante, " +
"lune des prostituées les plus convoitées de la haute société parisienne. Cruelle et émouvante, " +
"elle fascine autant quelle irrite. Les hommes en sont fous, mais elle les ruine. Marquée par ses origines " +
"et son ascendance, Nana venge par ses caprices de femme frivole le petit peuple de Paris écrasé par la société " +
"bien-pensante du Second Empire. À travers une peinture sans concession dun univers corrompu par le luxe et " +
"loisiveté mondaine, Zola a su faire de cette fille des rues trop vite confrontée à la médiocrité du monde " +
"lun des personnages les plus populaires de la littérature française. Après LAssommoir, Nana, autre roman " +
"« parisien » paru en 1880, confirma le succès de Zola et le conforta dans la voie du roman naturaliste.");
/**
* Creates new form TestSVGDocument
*/
public TestSVGDocument() {
initComponents();
this.setTitle("Visualisation acte");
traitementTxt();
this.setExtendedState(MAXIMIZED_BOTH);
jsvgCanvas = new JSVGCanvas();
jsvgCanvas.setSVGDocument((SVGDocument)doc);
jsvgCanvas.setSize(600, 1200);
jScrollPane1.setViewportView(jsvgCanvas);
pack();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
cePanelDocument = new be.cercle.common.swing.CePanel();
ceScrollPaneDoc = new be.cercle.common.swing.CeScrollPane();
jScrollPane1 = new javax.swing.JScrollPane();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
cePanelDocument.setLayout(new java.awt.GridBagLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.weighty = 0.1;
cePanelDocument.add(ceScrollPaneDoc, gridBagConstraints);
getContentPane().add(cePanelDocument, java.awt.BorderLayout.CENTER);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.NORTH);
pack();
}// </editor-fold>
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
this.dispose();
}
private void traitementTxt() throws IllegalArgumentException {
int posX = 100;
int posY = 20;
boolean firstLine = true;
Font fontUsed = null;
int hautY = 0;
int posDebut = 0;
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
doc = impl.createDocument(svgNS, "svg", null);
// Get the root element (the 'svg' element).
Element svgRoot = doc.getDocumentElement();
// Set the width and height attributes on the root 'svg' element.
svgRoot.setAttributeNS(null, "width", "21.00cm");
svgRoot.setAttributeNS(null, "height", "29.00cm");
SVGGraphics2D svgGraphics2D = new SVGGraphics2D(doc);
svgGraphics2D.getGraphicContext().setFont(Font.decode("Times New Roman" + "-" + Font.PLAIN + "-" + "12"));
FontMetrics fontMetrics = svgGraphics2D.getFontMetrics();
hautY=fontMetrics.getHeight();
int posFin = 80;
int nbrPx = fontMetrics.stringWidth(sb.substring(posDebut,posFin));
while (true) {
if (posDebut < 0)
posDebut = 0;
if (posX < 100)
posX = 100;
while (posFin > 0 && ((posX + nbrPx) > 570)) { // maximum 570 points par ligne
posFin --;
nbrPx = fontMetrics.stringWidth(sb.substring(posDebut,posFin));
}
if (posFin<sb.length()) {
while (posFin > 0 && !sb.substring(posFin,posFin+1).equals(" ")) // ne pas couper les mots
posFin --;
}
nbrPx = fontMetrics.stringWidth(sb.substring(posDebut,posFin));
svgGraphics2D.drawString(sb.substring(posDebut,posFin), posX, posY);
posX += nbrPx;
svgGraphics2D.drawLine(posX,posY-4,570,posY-4); // finir la ligne par un trait
posX = 100; // on passe à la ligne suivante
posY += hautY;
if (posFin== sb.length())
break;
posDebut = posFin;
while (posDebut < sb.length() && sb.substring(posDebut,posDebut+1).equals(" "))
posDebut ++;
posFin = posDebut+80;
if (posFin > sb.length())
posFin = sb.length();
nbrPx = fontMetrics.stringWidth(sb.substring(posDebut,posFin));
}
svgGraphics2D.getRoot(svgRoot);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestSVGDocument().setVisible(true);
}
});
}
// Variables declaration - do not modify
public be.cercle.common.swing.CePanel cePanelDocument;
public be.cercle.common.swing.CeScrollPane ceScrollPaneDoc;
public javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
} |
Partager