package jp.ac.kyutech.mns.ist;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Rectangle2D;

import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.nodes.PText;


public class MyRect extends PPath {

	public static BasicStroke basicStroke = new BasicStroke(3f,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 90.0f);
	public static BasicStroke dottedStroke = new BasicStroke(3f,BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 90.0f, new float[]{5,10},0);
	public static BasicStroke thinStroke = new BasicStroke(1.5f,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 90.0f);


	/**
	 * 
	 */
	private static final long serialVersionUID = 7341629786437082778L;
	PText innerText;
	int align = 0; //0:left 1:center 2:right
	int valign = 0; //0:top 1:center 2:bottom
	Rectangle2D margin = new Rectangle2D.Float(4f,4f,4f,4f);//上，左，右，下
	float lineWidth = 1.5f;
	float width = 100, height = 100;

	public MyRect(String txt, int w, int h){
		width = w; height = h;
		int txtlen = txt.length();
		String numtext = "";
		if (txtlen == 1) numtext = "  ";
		if (txtlen == 2) numtext = " ";
		numtext = numtext + txt;
//		System.out.println(numtext);
		innerText = new PText(numtext);
//		innerText.setHorizontalAlignment(Component.RIGHT_ALIGNMENT);
		//		setBounds();
//		setPaint(Color.cyan);
		setStrokePaint(null);
//		setStrokePaint(Color.blue);
//		setStroke(getBasicStroke_Solid(lineWidth));
		addChild(innerText);
		//		innerText.setPaint(Color.TRANSLUCENT);
		innerText.setTextPaint(Color.black);
		//		innerText.moveToFront();
//		setPathToRectangle(0f,0f,(float)(innerText.getBounds().getWidth()+margin.getX()+margin.getWidth()),(float)(innerText.getBounds().getHeight()+margin.getY()+margin.getHeight()));
//		innerText.offset(margin.getX(), margin.getY());
//		innerText.validateFullBounds();
		innerText.validateFullPaint();
		rebuild();
	}
	
	public void setText(String txt){
		innerText.setText(txt);
		rebuild();
	}
	public void setSize(float w, float h){
		width = w;
		height = h;
		rebuild();
	}

	public BasicStroke getBasicStroke_Solid(float width){
		return new BasicStroke(width,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 90.0f);
	}

	public void rebuild(){
		setPathToRectangle(0f,0f,width,height);

//		System.out.println(innerText.getText());
//		int i=0;
		float fwidth = (float)(width - margin.getX() - margin.getWidth());
		float fheight = (float)(height - margin.getY() - margin.getHeight());
//		while(i<1){
			// このwidth,heightの範囲におさまるテキストになるよう，拡大率をかえる
			double textw = innerText.getBounds().getWidth();
			double texth = innerText.getBounds().getHeight();
			double ratex = textw/fwidth;
			double ratey = texth/fheight;
			float rate = (ratex > ratey) ? (float)ratex: (float)ratey;
			Font f = innerText.getFont();
			Font newF = f.deriveFont(f.getSize2D()/rate);
//			System.out.println(f.getSize2D());
			innerText.setFont(newF);
			innerText.validateFullPaint();
//			i++;
			
//		}
		innerText.setOffset(margin.getX(),margin.getY());

	}

}
