Skip to content

Commit

Permalink
v0.1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelalejandromorenobarrientos committed Apr 26, 2016
1 parent ad417fe commit d04fcde
Show file tree
Hide file tree
Showing 22 changed files with 619 additions and 7 deletions.
82 changes: 82 additions & 0 deletions src/jdrafting/geom/JDMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.util.LinkedList;
import java.util.List;

/**
* Math utility operations
Expand Down Expand Up @@ -191,6 +193,86 @@ public static double projection( Point2D v, Point2D w )
return abs( scalar( v, w ) / v.distance( 0, 0 ) );
}

/**
* Get incenter of a triangle
* @param a vertex
* @param b vertex
* @param c vertex
* @return the incenter
*/
public static Point2D incenter( Point2D A, Point2D B, Point2D C )
{
double a = vector( B, C ).distance( 0, 0 );
double b = vector( A, C ).distance( 0, 0 );
double c = vector( A, B ).distance( 0, 0 );
double abc = a + b + c;

return new Point2D.Double(
( a * A.getX() + b * B.getX() + c * C.getX() ) / abc,
( a * A.getY() + b * B.getY() + c * C.getY() ) / abc );
}

/**
* Get ortocenter of a triangle
* @param A vertex
* @param B vertex
* @param C vertex
* @return the ortocenter or null if vertex are aligned
*/
public static Point2D ortocenter( Point2D A, Point2D B, Point2D C )
{
Point2D vBC = vector( B, C );
Point2D nBC = normal( vBC );
Point2D vAC = vector( A, C );
Point2D nAC = normal( vAC );

return linesIntersection( A, sumVectors( A, nBC ),
B, sumVectors( B, nAC ) );
}

/**
* Get baricenter of a triangle
* @param a vertex
* @param b vertex
* @param c vertex
* @return the baricenter
*/
public static Point2D baricenter( Point2D A, Point2D B, Point2D C )
{
List<Point2D> vertex = new LinkedList<>();
vertex.add( A );
vertex.add( B );
vertex.add( C );

return centroid( vertex );
}

/**
* Get circumcenter of a triangle
* @param a vertex
* @param b vertex
* @param c vertex
* @return the circumcenter
*/
public static Point2D circumcenter( Point2D A, Point2D B, Point2D C )
{
double Ax = A.getX(), Ay = A.getY();
double Bx = B.getX(), By = B.getY();
double Cx = C.getX(), Cy = C.getY();
double dCBx = Cx - Bx, dBCy = By - Cy;
double dACx = Ax - Cx, dCAy = Cy - Ay;
double dBAx = Bx - Ax, dABy = Ay - By;
double d = 2 * ( Ax * dBCy + Bx * dCAy + Cx * dABy );

return new Point2D.Double(
( ( Ax * Ax + Ay * Ay ) * dBCy
+ ( Bx * Bx + By * By ) * dCAy
+ ( Cx * Cx + Cy * Cy ) * dABy ) / d,
( ( Ax * Ax + Ay * Ay ) * dCBx
+ ( Bx * Bx + By * By ) * dACx
+ ( Cx * Cx + Cy * Cy ) * dBAx ) / d );
}

/**
* Shape length (poligonal length)
* @param shape the shape
Expand Down
69 changes: 67 additions & 2 deletions src/jdrafting/gui/Application.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package jdrafting.gui;

import static jdrafting.gui.JDUtils.getLargeIcon;
import static jdrafting.gui.JDUtils.getLocaleText;
import static jdrafting.gui.JDUtils.getSmallIcon;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
Expand Down Expand Up @@ -52,6 +54,7 @@
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
Expand Down Expand Up @@ -136,11 +139,14 @@
import jdrafting.gui.controller.actions.SplineAction;
import jdrafting.gui.controller.actions.TextVisibleAction;
import jdrafting.gui.controller.actions.TranslationAction;
import jdrafting.gui.controller.actions.TriangleAction;
import jdrafting.gui.controller.actions.TrianglePointsAction;
import jdrafting.gui.controller.actions.UndoAction;
import jdrafting.gui.controller.actions.VertexAction;
import jdrafting.gui.controller.actions.ZoomAllAction;
import jdrafting.gui.controller.actions.ZoomInOutAction;
import jdrafting.gui.controller.mouse.HandListener;
import jdrafting.gui.controller.mouse.TrianglePointsListener;

/**
* GUI frame class
Expand All @@ -154,7 +160,7 @@ public class Application extends JFrame
//////////////////////
// metainfo
public static final String APPNAME = "JDrafting";
public static final String VERSION = "0.1.7.1"; // Some minor fixes
public static final String VERSION = "0.1.8";
public static final String AUTHOR = "Miguel Alejandro Moreno Barrientos";
public static final String COPYLEFT = "2016";
public static final String PROJECT_PAGE =
Expand Down Expand Up @@ -211,6 +217,7 @@ public class Application extends JFrame
public JMenu menuExercise;
public JMenu menuTools;
public JMenu menuTransform;
public JMenu menuTrianglePoints;
public JMenu menuView;
public JMenu menuAppearance;
public JMenu menuHelp;
Expand All @@ -220,6 +227,7 @@ public class Application extends JFrame
public JButton buttonColor, buttonPointColor;
public JButton buttonRuler, buttonProtactor;
public JCheckBox checkRuler;
public JButton buttonMultiplier;
public JComboBox<BasicStroke> comboLineStyle;
public JToggleButton toggleNames;
public JLabel labelStatus;
Expand Down Expand Up @@ -457,6 +465,26 @@ protected void paintComponent( Graphics g ) {
checkRuler.setMinimumSize( checkRuler.getPreferredSize() );
checkRuler.addActionListener(
evt -> setUseDistance( checkRuler.isSelected() ) );
// multiplier button
rulerProtToolbar.add( buttonMultiplier =
new JButton( "<html><font color=blue size=2>xN</font></html>" ) );
buttonMultiplier.setMaximumSize( buttonMultiplier.getPreferredSize() );
buttonMultiplier.addActionListener( evt -> {
String result = (String) JOptionPane.showInputDialog(
this, getLocaleText( "dist_mult_dlg" ), "xN",
JOptionPane.QUESTION_MESSAGE,
getLargeIcon( "ruler.png" ), null, "1" );
if ( result == null ) return;
try
{
setDistance( getDistance() * Double.valueOf( result ) );
}
catch ( NumberFormatException e )
{
JOptionPane.showMessageDialog(
this, e, "xN", JOptionPane.ERROR_MESSAGE );
}
} );
rulerProtToolbar.add( Box.createHorizontalStrut( 12 ) );
// protractor button
rulerProtToolbar.add( buttonProtactor = new JButton() );
Expand Down Expand Up @@ -715,6 +743,25 @@ public void actionPerformed(ActionEvent e)
menuShapes.add( action = new AngleAction( this ) );
actionMap.put( action.getValue( Action.NAME ), action );
menuShapes.addSeparator();
// Triangle notable points tool
menuShapes.add( menuTrianglePoints = new JMenu() );
menuTrianglePoints.setText( getLocaleText( "triangle_tools" ) );
menuTrianglePoints.setIcon( getSmallIcon( "triangle_popup.png" ) );
menuTrianglePoints.add( action = new TriangleAction( this ) );
actionMap.put( action.getValue( Action.NAME ), action );
menuTrianglePoints.addSeparator();
menuTrianglePoints.add( action = new TrianglePointsAction(
this, TrianglePointsListener.BARICENTER ) );
actionMap.put( action.getValue( Action.NAME ), action );
menuTrianglePoints.add( action = new TrianglePointsAction(
this, TrianglePointsListener.CIRCUMCENTER ) );
actionMap.put( action.getValue( Action.NAME ), action );
menuTrianglePoints.add( action = new TrianglePointsAction(
this, TrianglePointsListener.INCENTER ) );
actionMap.put( action.getValue( Action.NAME ), action );
menuTrianglePoints.add( action = new TrianglePointsAction(
this, TrianglePointsListener.ORTOCENTER ) );
actionMap.put( action.getValue( Action.NAME ), action );
// Rectangle shape
menuShapes.add( action = new RectangleAction( this ) );
actionMap.put( action.getValue( Action.NAME ), action );
Expand Down Expand Up @@ -943,7 +990,6 @@ public void actionPerformed(ActionEvent arg0)
actionMap.put( action.getValue( Action.NAME ), action );

// --- TOOLBARS ACTIONS
// actionbar
actionbar.add( actionMap.get( getLocaleText( "new" ) ) );
actionbar.add( actionMap.get( getLocaleText( "open" ) ) );
actionbar.add( actionMap.get( getLocaleText( "save" ) ) );
Expand Down Expand Up @@ -1000,6 +1046,24 @@ public void actionPerformed(ActionEvent arg0)
shapebar.add( actionMap.get( getLocaleText( "circumference" ) ) );
shapebar.add( actionMap.get( getLocaleText( "angle" ) ) );
shapebar.addSeparator( VSEP );
JButton trianglePopup = new JButton( new AbstractAction() {
@Override
public void actionPerformed( ActionEvent e )
{
Component src = (Component) e.getSource();
JPopupMenu popup = new JPopupMenu();
popup.add( actionMap.get( getLocaleText( "triangle" ) ) );
popup.addSeparator();
popup.add( actionMap.get( getLocaleText( "baricenter" ) ) );
popup.add( actionMap.get( getLocaleText( "ortocenter" ) ) );
popup.add( actionMap.get( getLocaleText( "circumcenter" ) ) );
popup.add( actionMap.get( getLocaleText( "incenter" ) ) );
popup.show( src, (int) src.getBounds().getWidth() - 4, -2 );
}
});
trianglePopup.setToolTipText( getLocaleText( "triangle_tools" ) );
trianglePopup.setIcon( getLargeIcon( "triangle_popup.png" ) );
shapebar.add( trianglePopup );
shapebar.add( actionMap.get( getLocaleText( "rectangle" ) ) );
shapebar.add( actionMap.get( getLocaleText( "ellipse" ) ) );
shapebar.add( actionMap.get( getLocaleText( "polygon" ) ) );
Expand All @@ -1021,6 +1085,7 @@ public void actionPerformed(ActionEvent arg0)
toolbar.add( actionMap.get( getLocaleText( "vertex" ) ) );
toolbar.add( actionMap.get( getLocaleText( "extremes" ) ) );
toolbar.add( actionMap.get( getLocaleText( "divisions" ) ) );
toolbar.add( actionMap.get( "Triangle points" ) );
toolbar.add( actionMap.get( getLocaleText( "inter" ) ) );
toolbar.add( actionMap.get( getLocaleText( "bounds" ) ) );
toolbar.addSeparator( HSEP );
Expand Down
41 changes: 41 additions & 0 deletions src/jdrafting/gui/controller/actions/ActionTrianglePoints.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package jdrafting.gui.controller.actions;

import static jdrafting.gui.JDUtils.getLargeIcon;
import static jdrafting.gui.JDUtils.getLocaleText;
import static jdrafting.gui.JDUtils.getSmallIcon;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.KeyStroke;

import jdrafting.gui.Application;
import jdrafting.gui.controller.mouse.AngleListener;

@SuppressWarnings("serial")
public class ActionTrianglePoints extends AbstractAction
{
private Application app;

public ActionTrianglePoints( Application app )
{
this.app = app;

putValue( NAME, getLocaleText( "angle" ) );
putValue( SHORT_DESCRIPTION, getLocaleText( "angle_des" ) );
putValue( MNEMONIC_KEY, KeyEvent.VK_N );
putValue( ACCELERATOR_KEY,
KeyStroke.getKeyStroke( KeyEvent.VK_6, InputEvent.CTRL_MASK ) );
putValue( SMALL_ICON, getSmallIcon( "angle.png" ) );
putValue( LARGE_ICON_KEY, getLargeIcon( "angle.png" ) );
}

@Override
public void actionPerformed(ActionEvent e)
{
app.getCanvas().setCanvasListener(
new AngleListener( app.getCanvas() ) );
}
}
38 changes: 38 additions & 0 deletions src/jdrafting/gui/controller/actions/TriangleAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package jdrafting.gui.controller.actions;

import static jdrafting.gui.JDUtils.getLargeIcon;
import static jdrafting.gui.JDUtils.getLocaleText;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;

import jdrafting.gui.Application;
import jdrafting.gui.controller.mouse.TriangleListener;

@SuppressWarnings("serial")
public class TriangleAction extends AbstractAction
{
private Application app;

public TriangleAction( Application app )
{
this.app = app;

putValue( NAME, getLocaleText( "triangle" ) );
putValue( SHORT_DESCRIPTION, getLocaleText( "triangle_des" ) );
putValue( MNEMONIC_KEY, KeyEvent.VK_T );
/*putValue( ACCELERATOR_KEY,
KeyStroke.getKeyStroke( KeyEvent.VK_5, InputEvent.CTRL_MASK ) );*/
putValue( SMALL_ICON, getLargeIcon( "triangle.png" ) );
putValue( LARGE_ICON_KEY, getLargeIcon( "triangle.png" ) );
}

@Override
public void actionPerformed(ActionEvent e)
{
app.getCanvas().setCanvasListener(
new TriangleListener( app.getCanvas() ) );
}
}
51 changes: 51 additions & 0 deletions src/jdrafting/gui/controller/actions/TrianglePointsAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package jdrafting.gui.controller.actions;

import static jdrafting.gui.JDUtils.getLargeIcon;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;

import jdrafting.gui.Application;
import jdrafting.gui.controller.mouse.TrianglePointsListener;

@SuppressWarnings("serial")
public class TrianglePointsAction extends AbstractAction
{
private Application app;
private int type;

public TrianglePointsAction( Application app, int type )
{
this.app = app;
this.type = type;

putValue( NAME, TrianglePointsListener.getName( type ) );
ImageIcon large = null;
switch( type )
{
case TrianglePointsListener.INCENTER:
large = getLargeIcon( "incenter.png" );
break;
case TrianglePointsListener.BARICENTER:
large = getLargeIcon( "baricenter.png" );
break;
case TrianglePointsListener.CIRCUMCENTER:
large = getLargeIcon( "circumcenter.png" );
break;
case TrianglePointsListener.ORTOCENTER:
large = getLargeIcon( "ortocenter.png" );
break;
}
putValue( SMALL_ICON, large );
putValue( LARGE_ICON_KEY, large );
}

@Override
public void actionPerformed( ActionEvent e )
{
app.getCanvas().setCanvasListener(
new TrianglePointsListener( app.getCanvas(), type ) );
}
}
2 changes: 1 addition & 1 deletion src/jdrafting/gui/controller/mouse/BisectrixListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ else if ( bis1 == null )
}
canvas.repaint();
}

@Override
public void paintTool( Graphics2D g2 )
{
Expand Down
Loading

0 comments on commit d04fcde

Please sign in to comment.