package com.fr.design.mainframe;

import com.fr.design.constants.UIConstants;
import com.fr.design.gui.iscrollbar.UIScrollBar;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;

/**
 * Author : daisy
 * Date: 13-9-11
 * Time: 上午10:14
 */
public class DottedLine extends JPanel {
	public static final int HEIGHT = 3;
	private static final int BLUE_WIDTH = 12;//蓝色线长度，白色线长度是其半
	private int direction = UIScrollBar.HORIZONTAL;

	public DottedLine(int direction, int width) {
		//水平方向的虚线框
		if (direction == UIScrollBar.HORIZONTAL) {
			this.setPreferredSize(new Dimension(width, HEIGHT));
		} else {
			this.setPreferredSize(new Dimension(HEIGHT, width));
		}
		this.direction = direction;
		this.setBackground(new Color(255, 255, 255));//设置白色背景
	}

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		//如果是水平方向的
		if (direction == UIScrollBar.HORIZONTAL) {

			paintHorizontal(g2d);
		} else {
			paintVertical(g2d);
		}

	}


	private void paintHorizontal(Graphics2D g2d) {
		int totalWidth = getWidth();
		int paintX = 0;


		while (paintX < totalWidth) {
			int endX = paintX + BLUE_WIDTH;
			if (endX > totalWidth) {
				endX = totalWidth;
			}

			int[] x = new int[]{paintX, endX, endX, paintX, paintX};
			int[] y = new int[]{0, 0, HEIGHT, HEIGHT, 0};
			paintBlueArea(g2d, x, y);
			paintX += BLUE_WIDTH + BLUE_WIDTH / 2;
		}

	}


	private void paintVertical(Graphics2D g2d) {
		int totalHeight = getHeight();
		int paintY = 0;
		while (paintY < totalHeight) {
			int endY = paintY + BLUE_WIDTH;
			if (endY > totalHeight) {
				endY = totalHeight;
			}
			int[] x = new int[]{0, HEIGHT, HEIGHT, 0, 0};
			int[] y = new int[]{paintY, paintY, endY, endY, paintY};
			paintBlueArea(g2d, x, y);
			paintY += BLUE_WIDTH + BLUE_WIDTH / 2;
		}

	}


	private void paintBlueArea(Graphics2D g2d, int[] x, int[] y) {
		g2d.setPaint(UIConstants.DOTTED_LINE_COLOR);
		GeneralPath generalPath = new GeneralPath(Path2D.WIND_EVEN_ODD, x.length);
		generalPath.moveTo(x[0], y[0]);

		for (int index = 1; index < x.length; index++) {
			generalPath.lineTo(x[index], y[index]);
		}
		generalPath.closePath();
		g2d.fill(generalPath);
	}


}