/* MODE D'EMPLOI * ============= * - creer une ligne qui apparaitra dans le cadran superieur droit * - creer une seconde ligne respectant la meme contrainte * - appeler draw2lines() * - cliquer sur line1 puis sur line2 * - la premiere apparait en jaune, la seconde en bleu, SOPC en rouge */ package line_v5; import javax.swing.*; import java.awt.*; import java.awt.event.*; /* * Example of how to use a LineDrawer to draw a line ax+by+c=0 * [very bad programming : should be totally rewritten !] */ /** * Class LineDrawer - To draw a given line in a 2D basic graphical panel. * @author am; modified DB, JC, DB * @version 09/2003; modified 10/2004, 09/2006 */ class LineDrawer extends JPanel { private final int leftMargin = 20 ; private final int rightMargin = 20 ; private final int topMargin = 20 ; private final int bottomMargin = 20 ; private final Color bg = new Color(222,222,222); // Color.lightGray; private final Color fg = Color.black; private int zeroX; private int zeroY; private int maxX; private int maxY; private int boundBoxX; private int boundBoxY; private Line droite1, droite2; private static JFrame f=null; private static LineDrawer lineDrawer; /* Method drawLine * Draw a line ax+by+c=0 in a 2D basic graphical panel * (try, for example, with : a=0.7, b=3.0, c=-210) */ public static void draw2Lines( final Line lin1, final Line lin2 ) { // Define the drawing frame dimensions final int width = 800 ; final int height = 600 ; // Create a drawing frame if (f==null) { f = new JFrame( "LineDrawer demo" ); f.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { // System.exit( 0 ); f=null; } // windowClosing() } // WindowAdapter ); // addWindowListener() // Create the LineDrawer panel and insert it into the frame lineDrawer = new LineDrawer( lin1, lin2 ); f.getContentPane().add( lineDrawer, BorderLayout.CENTER ); f.setSize( new Dimension( width, height ) ); f.setVisible( true ); } // if no frame else System.out.println( "Fermer d'abord la fenêtre graphique !" ); // TODO: permettre de redessiner d'autres droites sans fermer } // draw2Lines() /** * Draw the given line * Draw the line ax+by+c=0 in a fixed drawing area without scaling. * If the line doesn't cross this zone, an error message is prompted. * @param li a Line */ private void drawLine( final Graphics g, final Line li, final int numLine ) { double a, b, c ; // Coefficients of the line ax+by+c=0 to draw String e; // equation de la droite // Initialize the coefficients of the line ax+by+c=0 a = li.getA() ; b = li.getB() ; c = li.getC() ; e = li.toString(); if ( a==0.0 && b==0.0 ) { g.drawString( e + " is not a line !", maxX/3, zeroY/(5-numLine)) ; return; } // Draw line ax+by+c=0 /* Il faut tester l'intersection de la droite avec la bounding box */ double x,y; double Px[] = new double[2]; double Py[] = new double[2]; int nbPoints = 0; if ( (b != 0) && (nbPoints < 2)){ x = 0; y = -c/b; //system.out.println("Intersection x=0 en : "+ y); if( (y >= 0) && (y <= boundBoxY)){ Px[nbPoints] = x; Py[nbPoints] = y; nbPoints ++; } if(nbPoints < 2){ x = boundBoxX; y = (-c -a*x)/b; //system.out.println("Intersection x=" + boundBoxX + " en y = " + y); if( (y >= 0) && (y <= boundBoxY)){ Px[nbPoints] = x; Py[nbPoints] = y; nbPoints ++; } } } if ( (a != 0) && (nbPoints < 2)) { y = 0; x = -c/a; //system.out.println("Intersection y=0 en x = "+ x); if( (x > 0) && (x < boundBoxX)){ Px[nbPoints] = x; Py[nbPoints] = y; nbPoints ++; } if(nbPoints < 2){ y = boundBoxY; x = (-c - b*y)/a; //system.out.println("Intersection y =" + boundBoxY + " en x = " + x); if( (x > 0) && (x < boundBoxX)){ Px[nbPoints] = x; Py[nbPoints] = y; nbPoints ++; } } } //system.out.println("nbIntersection : " + nbPoints); /* Si la doite intersect la bounding box */ if( (nbPoints == 2) && ( (Px[0] != Px[1]) || (Py[0] != Py[1]) ) ){ /* On met les points à la meme echelle que l'affichage */ Px[0] = Px[0] * 100 + zeroX; Px[1] = Px[1] * 100 + zeroX; Py[0] = -Py[0] * 100 + zeroY; Py[1] = -Py[1] * 100 + zeroY; //system.out.println("P1 = (" + Px[0] + "," + Py[0] + ") P2 = (" + Px[1] + "," + Py[1] + ")"); g.drawLine((int)Px[0], (int)Py[0], (int)Px[1], (int)Py[1]); } else { // if no, prompt a message of error in the drawing area g.drawString("Line " + e + " is out of drawing bounds", maxX/3, zeroY/(5-numLine)) ; } } // drawLine() /** * Create a 2D graphical panel to draw lines */ private LineDrawer( final Line d1, final Line d2 ) { droite1 = d1; droite2 = d2; // Initialize drawing colors, border, opacity. setBackground(bg); setForeground(fg); setBorder(BorderFactory.createCompoundBorder( BorderFactory.createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder())); } // LineDrawer() /** prepare drawing zone */ public void paintComponent(final Graphics g) { super.paintComponent(g); //clears the background // getInsets() = get the external padding of the component (i.e. the // minimum amount of space between the component and the edges of // its display area // getWidth() = get the current width of the component, measured in // pixels Insets insets = getInsets(); zeroX = insets.left + leftMargin; zeroY = getHeight() - insets.bottom - bottomMargin ; maxX = getWidth() - insets.right - rightMargin ; maxY = insets.top + topMargin; boundBoxX = (maxX - zeroX) / 100; boundBoxY = (zeroY - maxY) / 100; //system.out.println("(zeroX,zeroY) = ("+ zeroX + "," + zeroY + ") (maxX, maxY) = ("+ maxX + "," + maxY + // ") (boundBoxX,boundBoxY) = ("+ boundBoxX + "," + boundBoxY + ")"); //g.drawLine(x1, y1, x2, y2); // +----> x // | // y // Draw axis x (horizontal axis) g.drawLine(zeroX, zeroY, maxX , zeroY); g.drawString("x", maxX+5, zeroY+3); // Draw axis y (vertical axis) g.drawLine(zeroX, zeroY, zeroX, maxY); g.drawString("y", zeroX-3, maxY-3); // Mark point (0,0) g.drawString("(0,0)",zeroX-10, zeroY+15); // Draw scale x for (int i = 100 ; i < maxX ; i+=100 ) { g.drawLine(i+zeroX, zeroY-5, i+zeroX, zeroY+5); g.drawString(String.valueOf(i), i+zeroX-7, zeroY-7); } // for // Draw scale y for (int j = 100 ; j < zeroY ; j+=100 ) { g.drawLine(zeroX-5, zeroY-j, zeroX+5, zeroY-j); g.drawString(String.valueOf(j), zeroX-20, zeroY-j-5); } // for g.setColor(Color.yellow); drawLine( g, droite1, 1 ); g.setColor(Color.blue); drawLine( g, droite2, 2 ); g.setColor(Color.red); g.drawString( droite1.estSOPC(droite2), maxX/3, zeroY/2 ); g.setColor(fg); } // paintComponent() } // LineDrawer