/* * [RoundCornerMath.java] * * Summary: draw RoundCornerMath. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2011-01-29 initial version. */ package com.mindprod.connectors; import com.mindprod.connectors.RoundPolygon.RoundCornerCoordinates; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; /** * draw RoundCornerMath. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-01-29 initial version. * @since 2011-01-29 */ class RoundCornerMath extends ConnectorPanel { private static final int ANGLE_MARKER_RADIUS = 50; private static final int INNER_COIL_RADIUS = 20; private static final int LINE_FOR_COIL = 6; private static final int LINE_FOR_CONSTRUCTION = 10; private static final int LINE_FOR_RESULTS = 20; private static final int OUTER_COIL_RADIUS = 40; private static final int ROUNDING_RADIUS = 200; private static final int X1 = 100; private static final int X3 = 900; private static final int X5 = 200; private static final int Y1 = 900; private static final int Y3 = 600; private static final int Y5 = 50; private static final Color AXIS_COLOR = new Color( 0x0802000 ); private static final Color CONSTRUCTION_COLOR = new Color( 0x000000 ); private static final Color LETTERING_COLOR = new Color( 0x32cd32 ); private static final Color RESULTS_COLOR = new Color( 0x80ff0000, true ); private static final Color THETA_COLOR = Color.RED; RoundCornerMath() { super( 1000, 1000 ); } /** * Fill in an angle with a pie shape to mark it. * * @param g graphics context to draw on * @param x1 first point * @param y1 first point * @param x2 middle point * @param y2 middle point * @param x3 end point * @param y3 end point * @param radius radius of pie mark. */ private static void markAngle( Graphics g, int x1, int y1, int x2, int y2, int x3, int y3, int radius ) { double startAngle = Math.atan2( y2 - y1, x1 - x2 ); if ( startAngle < 0 ) { startAngle += Math.PI * 2; } double endAngle = Math.atan2( y2 - y3, x3 - x2 ); if ( endAngle < 0 ) { endAngle += Math.PI * 2; } double arcAngle = endAngle - startAngle; if ( arcAngle < 0 ) { arcAngle += Math.PI * 2; } if ( arcAngle > Math.PI ) { double temp = startAngle; startAngle = endAngle; endAngle = temp; arcAngle = endAngle - startAngle; if ( arcAngle < 0 ) { arcAngle += Math.PI * 2; } } g.fillArc( x2 - radius, y2 - radius, radius * 2, radius * 2, ( int ) Math.floor( Math.toDegrees( startAngle ) + .5 ), ( int ) Math.floor( Math.toDegrees( arcAngle ) + .5 ) ); } /** * draw diagram of the math used to computer rounded corners * * @param g where to paint */ public void paintComponent( Graphics g ) { super.paintComponent( g ); final Graphics2D g2d = ( Graphics2D ) g; RoundCornerCoordinates c = RoundPolygon.computeCoordinates( X1, Y1, X3, Y3, X5, Y5, ROUNDING_RADIUS ); final int xb = c.getXb(); final int yb = c.getYb(); final int hb = c.getHb(); final int x2 = c.getX2(); final int y2 = c.getY2(); final int x4 = c.getX4(); final int y4 = c.getY4(); final int startAngle = c.getStartAngle(); final int arcAngle = c.getArcAngle(); final int x6 = xb + ROUNDING_RADIUS; final int y6 = yb + ROUNDING_RADIUS; // mark equal angles with a pie g2d.setColor( THETA_COLOR ); // now in drawing coordinates. markAngle( g2d, X1, Y1, X3, Y3, x6, y6, ANGLE_MARKER_RADIUS ); markAngle( g2d, x6, y6, X3, Y3, X5, Y5, ANGLE_MARKER_RADIUS ); markAngle( g2d, x6, y6, x2, y2, x4, y4, ANGLE_MARKER_RADIUS ); markAngle( g2d, x2, y2, x4, y4, x6, y6, ANGLE_MARKER_RADIUS ); g2d.setStroke( new BasicStroke( LINE_FOR_CONSTRUCTION ) ); g2d.setColor( AXIS_COLOR ); g2d.drawLine( 0, Y3, 1000, Y3 ); // x axis g2d.drawLine( X3, 0, X3, 1000 ); // y axis // draw construction lines g2d.setStroke( new BasicStroke( LINE_FOR_CONSTRUCTION ) ); g2d.setColor( CONSTRUCTION_COLOR ); g2d.drawLine( X1, Y1, X3, Y3 ); g2d.drawLine( X1, Y1, X1, Y3 ); g2d.drawRect( xb, yb, hb, hb ); g2d.drawOval( xb, yb, hb, hb ); g2d.drawLine( X3, Y3, X5, Y5 ); g2d.drawPolygon( new int[] { x6, x2, x4 }, new int[] { y6, y2, y4 }, 3 ); g2d.drawLine( X3, Y3, x6, y6 ); g2d.setStroke( new BasicStroke( LINE_FOR_RESULTS ) ); g2d.setColor( RESULTS_COLOR ); g2d.drawLine( X1, Y1, x2, y2 ); g2d.drawArc( xb, yb, hb, hb, startAngle, arcAngle ); g2d.drawLine( x4, y4, X5, Y5 ); // show start and end angle g2d.setStroke( new BasicStroke( LINE_FOR_COIL ) ); g2d.drawArc( x6 - INNER_COIL_RADIUS, y6 - INNER_COIL_RADIUS, INNER_COIL_RADIUS * 2, INNER_COIL_RADIUS * 2, 0, c.getStartAngle() ); g2d.drawArc( x6 - OUTER_COIL_RADIUS, y6 - OUTER_COIL_RADIUS, OUTER_COIL_RADIUS * 2, OUTER_COIL_RADIUS * 2, 0, c.getStartAngle() + c.getArcAngle() ); // mark the equal angles g2d.setColor( LETTERING_COLOR ); g2d.setFont( new Font( "dialog", Font.PLAIN, 70 ) ); drawCenteredString( g2d, "1", X1, Y1 ); drawCenteredString( g2d, "2", x2, y2 ); drawCenteredString( g2d, "3", X3, Y3 ); drawCenteredString( g2d, "4", x4, y4 ); drawCenteredString( g2d, "5", X5, Y5 ); drawCenteredString( g2d, "b", xb, yb ); drawCenteredString( g2d, "6", x6, y6 ); } }