FontDessin = font.deriveFont(17/zoom); AffineTransform atFont = new AffineTransform(); atFont.scale(1, -1); FontDessin.deriveFont(atFont); g2c.setFont(FontDessin); g2c.drawString(TexteInfo,(int)Xdeb, (int)Ydeb);
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionimport java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Line2D; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author guehenneux * */ public class TestAffine extends JFrame { /** * * @author guehenneux * */ private class Panneau extends JPanel { @Override protected void paintComponent(Graphics graphics) { Graphics2D graphique = (Graphics2D) graphics; /* * une grande ligne */ Shape forme0 = new Line2D.Double(100, 100, 500, 500); /* * une petite ligne */ Shape forme1 = new Line2D.Double(100, 300, 300, 100); /* * transformation affine destinee a renverser le dessin * verticalement (axe y vers le haut) */ AffineTransform transformationAffine = new AffineTransform(); transformationAffine.translate(0, getHeight()); transformationAffine.scale(1, -1); /* * on l'applique sur les formes */ forme0 = transformationAffine.createTransformedShape(forme0); forme1 = transformationAffine.createTransformedShape(forme1); /* * on dessine */ graphique.draw(forme0); graphique.draw(forme1); graphique .drawString( "Ce texte n'est pas influencé par la transormation affine.", 50, 50); } } /** * */ public TestAffine() { super("Test affine"); setSize(600, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); Panneau panneau = new Panneau(); add(panneau); setVisible(true); } /** * * @param args */ public static void main(String[] args) { new TestAffine(); } }
AffineTransform atDessin = new AffineTransform(); atDessin.translate(margeX,canvas1.getHeight()-(margeY)); atDessin.scale(zoom,-zoom); g2c.setFont(FontDessin); String ligne = buf.readLine(); ListModel = new DefaultListModel(); num=0; while (ligne!=null){ if (ligne.length()>=2){ String deb = ligne.substring(0 ,4); if (deb.equals("lig,")) { String[] c = ligne.split(","); style c[0]; couleur c[1]; Xdebut = c[2]; Ydebut = c[3]; Xfin = c[4]; Yfin = c[5]; Xdeb fpf(Xdebut); Ydeb fpf(Ydebut); Xfi = fpf(Xfin); Yfi = fpf(Yfin); if (couleur.equals("1")){g2c.setColor(Color.red);} else if (couleur.equals("2")){g2c.setColor(Color.blue);} else if (couleur.equals("3")){g2c.setColor(Color.ORANGE.darker());} else if (couleur.equals("4")){g2c.setColor(Color.MAGENTA);} else if (couleur.equals("5")){g2c.setColor(Color.cyan.darker());} else if (couleur.equals("6")){g2c.setColor(Color.ORANGE);} else if (couleur.equals("7")){g2c.setColor(Color.GREEN.darker());} else {g2c.setColor(Color.BLACK);} if (jCheckBoxNumLigne.isSelected()){Calcul.calculAngleLigne();} maligne=new Line2D.Float(Xdeb,Ydeb,Xfi,Yfi); maligne=atDessin.createTransformedShape(maligne); g2c.draw(maligne); } if (deb.equals("arc,")) { String[] c = ligne.split(","); style c[0]; couleur c[1]; Xdebut = c[2]; Ydebut = c[3]; Xfin = c[4]; Yfin = c[5]; Xcentre = c[6]; Ycentre = c[7]; Xfi fpf(Xdebut); Yfi fpf(Ydebut); Xdeb = fpf(Xfin); Ydeb = fpf(Yfin); Xcen = fpf(Xcentre); Ycen = fpf(Ycentre);// debut et fin inverses pour y neg rayon=(float) Math.sqrt(((Xcen-Xdeb)*(Xcen-Xdeb))+((Ycen-Ydeb)*(Ycen-Ydeb))); Xhaut=Xcen-(float)rayon; Yhaut=Ycen-(float)rayon; largeur=(float)rayon*2;hauteur=(float)rayon*2; if (couleur.equals("1")){g2c.setColor(Color.red);} else if (couleur.equals("2")){g2c.setColor(Color.blue);} else if (couleur.equals("3")){g2c.setColor(Color.ORANGE.darker());} else if (couleur.equals("4")){g2c.setColor(Color.MAGENTA);} else if (couleur.equals("5")){g2c.setColor(Color.cyan.darker());} else if (couleur.equals("6")){g2c.setColor(Color.ORANGE);} else if (couleur.equals("7")){g2c.setColor(Color.GREEN.darker());} else {g2c.setColor(Color.BLACK);} if (jCheckBoxNumLigne.isSelected()){Calcul.calculAngleLigne();} Calcul.calculAngleArc(); if ((Xdeb==Xfi) && (Ydeb==Yfi)){// si un cercle monCercle=new Ellipse2D.Float(Xhaut,Yhaut,largeur,hauteur); monCercle=(RectangularShape) atDessin.createTransformedShape(monCercle); g2c.draw(monCercle); } else {// si un arc monArc=new Arc2D.Float(Xhaut,Yhaut,largeur,hauteur,(float)angleDebut,(float)angleFin,0); monArc=(RectangularShape) atDessin.createTransformedShape(monArc); g2c.draw(monArc); } } if (deb.equals("cot,")) { String[] c = ligne.split(","); style c[0]; couleur c[1]; Xdebut = c[2]; Ydebut = c[3]; Xfin = c[4]; Yfin = c[5]; Xcentre = c[6]; Ycentre = c[7];TexteCotation = c[8]; Xdeb fpf(Xdebut); Ydeb fpf(Ydebut); Xfi = fpf(Xfin); Yfi = fpf(Yfin); Xcen = fpf(Xcentre); Ycen = fpf(Ycentre); if (jCheckBoxNumLigne.isSelected()==true){} if (VoirCotations.isSelected()==true) { g2c.setColor(Color.GREEN.darker()); maCotation=new Line2D.Float(Xdeb,Ydeb,Xfi,Yfi); maCotation=atDessin.createTransformedShape(maCotation); g2c.draw(maCotation); Line2D maLigneFictiveCotation=new Line2D.Float(Xcen, Ycen, Xcen, Ycen); maLigneFictiveCotation=(Line2D) atDessin.createTransformedShape(maLigneFictiveCotation); g2c.drawString(TexteCotation,(int)maLigneFictiveCotation.getX1(), (int)maLigneFictiveCotation.getY1()); } } if (deb.equals("tex,")) { String[] c = ligne.split(","); style c[0]; couleur c[1]; Xdebut = c[2]; Ydebut = c[3];Xfin = c[4]; Yfin = c[5]; TexteInfo = c[6]; Xdeb fpf(Xdebut); Ydeb fpf(Ydebut); if (jCheckBoxNumLigne.isSelected()){} else { g2c.setColor(Color.GREEN.darker()); Line2D maLigneFictiveTexte=new Line2D.Float(Xdeb, Ydeb, Xdeb, Ydeb); maLigneFictiveTexte=(Line2D) atDessin.createTransformedShape(maLigneFictiveTexte); g2c.drawString(TexteInfo,(int)maLigneFictiveTexte.getX1(), (int)maLigneFictiveTexte.getY1()); } } num++; ListModel.addElement(""+num); jListLignes.setModel(ListModel); if (jCheckBoxNumLigne.isSelected()){numLigne.numLigne();} jComboBoxLigne1.addItem(""+num); jComboBoxLigne2.addItem(""+num); jComboBoxLigne3.addItem(""+num); jComboBoxLigne4.addItem(""+num); } ligne = buf.readLine(); } buf.close(); g2c.dispose();
java.lang.ClassCastException: java.awt.geom.GeneralPath at bru001.form1.VoirCotationsActionPerformed(form1.java:5569) at bru001.form1.actionPerformed(form1.java:2943) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:269) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:246) at java.awt.Component.processMouseEvent(Component.java:5602) at javax.swing.JComponent.processMouseEvent(JComponent.java:3129) at java.awt.Component.processEvent(Component.java:5367) at java.awt.Container.processEvent(Container.java:2010) at java.awt.Component.dispatchEventImpl(Component.java:4068) at java.awt.Container.dispatchEventImpl(Container.java:2068) at java.awt.Component.dispatchEvent(Component.java:3903) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3936) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866) at java.awt.Container.dispatchEventImpl(Container.java:2054) at java.awt.Window.dispatchEventImpl(Window.java:1801) at java.awt.Component.dispatchEvent(Component.java:3903) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
g2c.translate(margeX,canvas1.getHeight()-(margeY)); g2c.scale(zoom,-zoom); g2c.setStroke(new BasicStroke(1/zoom)); Font fontDessin = new Font("Dialog", Font.PLAIN, 17); //fontDessin = font.deriveFont(17/zoom);// bug d'affichage en zoom AffineTransform atFont = new AffineTransform(); atFont.setToScale(1,-1); Font fontMiroir = fontDessin.deriveFont(atFont); g2c.setFont(fontMiroir);