package uk.ac.leeds.ccg.projects.MedAction.FuzzyInference; import java.awt.*; import java.awt.image.*; import java.awt.geom.*; import java.util.Vector; import java.io.*; import javax.swing.*; import uk.ac.leeds.ccg.geotools.*; /* * FuzzyPanel.java * * Created on June 25, 2002, 12:45 AM */ /** * it provide a GUI to define a variable's membership function, user can move a point * add, delete, fuzzy set. * @author Jianhui Jin */ public class FuzzyPanel extends javax.swing.JPanel { // this Width is for the X axis arrow in the rightest side, screen size double xWidth = 0.0; // the rightest X will be 20 less that the xLine // this height is for the y axis arrow in the upper side, screen size double yHeight = 0.0; // the highest Y will be 20 less than the line except the mirgin Line2D.Double yLine = null; // y left side Coordinate Line Line2D.Double xLine = null; // x Coordinate Line Line2D.Double yLine2 = null; // y right side Coordinate Line // double xPosition = 0;// for x starting position // double yPosition = 0;// for y starting position /* * Coordiante Start Point */ FuzzyPoint coordinateStartPoint = new FuzzyPoint( 0.0d, 0.0d ); // editing variable FuzzyVariable fVariable = null; // name for this variable String name = ""; // how many time clicked private int pointClicked = 0; Polygon xArrow = null; Polygon yArrow1 = null; Polygon yArrow2 = null; // could forbide adding and deleting fuzzy set protected boolean isMemberAddDeleteEnabled = true; /** * * @return get how many times clicked on the same point */ public int getPointClicked() { return pointClicked; } public void setPointClicked() { this.pointClicked ++; } java.awt.event.MouseMotionAdapter MouseMove = new java.awt.event.MouseMotionAdapter() { public void mouseDragged(java.awt.event.MouseEvent evt) { formMouseDragged(evt); } }; //for test use Object[][] md=null; Vector mVector=new Vector(); Vector dVector=new Vector(); /** * paint the panel * @param g */ public void paintComponent(Graphics g){ super.paintComponent(g); // initialize the parameters. //fuzzyImage=(BufferedImage) this.createImage(width , height); // initialise the values for this coordiante initCoordinate(); // draw the coordinate drawCoordinate( (Graphics2D) g ); // draw a image of this variable drawFuzzyImage( fVariable, (Graphics2D) g ); //((Graphics2D) g).drawImage(fuzzyImage,0,0,this); // if edit member number allowed, set menuitem enable as true, otherwise as false // since in a fuzzy logic model, change member number will broke the rule model's rules // which might has the member that variable have deleted. if ( this.isMemberAddDeleteEnabled() ) { this.Add.setEnabled( true ); this.Delete.setEnabled( true ); } else { this.Add.setEnabled( false ); this.Delete.setEnabled( false ); } } /** * enabled add or delete fuzzy set * @param enabled */ public void setMemberAddDeleteEnabled( boolean enabled ) { this.isMemberAddDeleteEnabled = enabled; } /** * is add and delete enabled. * @return */ public boolean isMemberAddDeleteEnabled() { return this.isMemberAddDeleteEnabled; } // draw coordinate private void drawCoordinate(Graphics2D graphic) { // draw origin point FuzzyPoint start = getCoordinateStartPoint(); graphic.setColor( Color.black ); graphic.setStroke( new BasicStroke( 3.0f ) ); // draw y axis graphic.draw( yLine ); // draw x axis graphic.draw( xLine ); // draw right side y axis. graphic.draw( yLine2 ); graphic.setColor( Color.black ); // draw x axis arrow graphic.draw( xArrow ); graphic.fill( xArrow ); // draw left y axis arrow graphic.draw( yArrow1 ); graphic.fill( yArrow1 ); // draw right y axis arrow graphic.draw( yArrow2 ); graphic.fill( yArrow2 ); // draw those text graphic.drawString( "Membership Degree", 5, 25 ); graphic.drawString( "0.0", (int) start.getX() - 5, (int) start.getY() + 9 ); graphic.drawString( "1.0" + name + " value", (int) xWidth - 10, (int) start.getY() + 11 ); graphic.drawString( "1.0", (int) start.getX() - 5, (int) ( start.getY() - yHeight - 3 ) ); } // initialise the coordiante private void initCoordinate() { int width = getWidth(); // panel width int height = getHeight(); // panel height double xPosition = 20.0d; double yPosition = height - 50.0d; setCoordinateStartPoint( new FuzzyPoint( xPosition, yPosition ) ); FuzzyPoint coordinateStartPoint = getCoordinateStartPoint(); FuzzyPoint xLineTopPoint = new FuzzyPoint( width - xPosition, yPosition ); FuzzyPoint yLine1TopPoint = new FuzzyPoint( xPosition, 30.d ); yLine = new Line2D.Double( coordinateStartPoint.getX(), coordinateStartPoint.getY(), yLine1TopPoint.getX(), yLine1TopPoint.getY() ); // y left side Coordinate Line xLine = new Line2D.Double( coordinateStartPoint.getX(), coordinateStartPoint.getY(), xLineTopPoint.getX(), xLineTopPoint.getY() ); // X Coordinae Line xWidth = xLineTopPoint.getX() - coordinateStartPoint.getX() - 20.0d; // x Width start from the start point and the rightest X will be 20 less that the xLine yHeight = coordinateStartPoint.getY() - yLine1TopPoint.getY() - 20.0d; // yHeight start from the start pointthe highest Y will be 20 less than the yline except the mirgin yLine2 = new Line2D.Double( coordinateStartPoint.getX() + xWidth, coordinateStartPoint.getY(), coordinateStartPoint.getX() + xWidth, yLine1TopPoint.getY() ); // yLine2 ends the coordinate in the right side FuzzyPoint yLine2TopPoint = new FuzzyPoint( xPosition + xWidth, 30.0d ); int[] xArrowPointX = { (int) xLineTopPoint.getX(), (int) ( xLineTopPoint.getX() - 15.0d ), (int) ( xLineTopPoint.getX() - 15.0d ) }; int[] xArrowPointY = { (int) ( xLineTopPoint.getY() ), (int) ( xLineTopPoint.getY() - 4.0d ) , (int) ( xLineTopPoint.getY() + 4.0d ) }; xArrow = new Polygon( xArrowPointX, xArrowPointY, 3 ); int[] y1ArrowPointX = { (int) yLine1TopPoint.getX(), (int) ( yLine1TopPoint.getX() - 4.0d ), (int) ( yLine1TopPoint.getX() + 4.0d ) }; int[] y1ArrowPointY = { (int) yLine1TopPoint.getY(), (int) ( yLine1TopPoint.getY() + 15.0d ) , (int) ( yLine1TopPoint.getY() + 15.0d ) }; yArrow1 = new Polygon( y1ArrowPointX, y1ArrowPointY , 3 ); int[] y2ArrowPointX = { (int) yLine2TopPoint.getX(), (int) ( yLine2TopPoint.getX() - 4.0d ), (int) ( yLine2TopPoint.getX() + 4.0d ) }; int[] y2ArrowPointY = { (int) ( yLine2TopPoint.getY() ), (int) ( yLine2TopPoint.getY() + 15.0d ) , (int) ( yLine2TopPoint.getY() + 15.0d ) }; yArrow2 = new Polygon( y2ArrowPointX, y2ArrowPointY, 3 ); } // draw editting variable private void drawFuzzyImage( FuzzyVariable fVariable, Graphics2D graphic ){ //graphic= fuzzyImage.createGraphics(); graphic.setStroke( new BasicStroke( 1.0f ) ); AbstractFuzzySet[] set = fVariable.getFuzzySet(); for( int i = 0; i < set.length; i ++ ) drawFuzzyMember( graphic, set[ i ] ); } // draw a fuzzy set private void drawFuzzyMember( Graphics2D g, AbstractFuzzySet set ){ g.setColor( set.getColor() ); FuzzyPoint[] ps = set.getFuzzyPoint(); for( int i = 0; i