package com.poisson;
import javax.swing.*;
import java.awt.*;
/**
* 画坐标Service
*/
public class PaintCoordinateService {
private static final int X = 680;
private static final int Y = 30;
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public static void paintCoordinate(Double[] xNumberArray, Double[] yNumberArray) {
StringBuffer logBuffer = new StringBuffer("paintCoordinate: \n");
for (int i = 0; i < xNumberArray.length; i++) {
logBuffer.append("(").append(xNumberArray[i]).append(",").append(yNumberArray[i]).append(") - ").append(i).append("\n");
}
System.err.println(logBuffer);
MyPanel myPanel = new MyPanel(xNumberArray, yNumberArray);
myPanel.setBorder(BorderFactory.createTitledBorder("绘制图像区"));
myPanel.setBounds(X, Y, WIDTH, HEIGHT);
PoissonFrame.getFrame().add(myPanel);
PoissonFrame.getFrame().repaint();
}
private static class MyPanel extends JScrollPane {
private static final int X_START = 100;
private static final int X_END = 570;
private static final int ARROW_LENGTH = 10;
private static final int Y_START = 570;
private static final int Y_END = 30;
private static final int X_LENGTH = X_END - X_START;
private static final int Y_LENGTH = Y_START - Y_END;
private static final int NUMBER_LENGTH = 10;
private static final double X_SCALE_MIN_UNIT = 0.0000001;
private static final int X_SCALE_INCREASE_FACTOR_5 = 5;
private static final int X_SCALE_INCREASE_FACTOR_2 = 2;
private static final int X_SCALE_NUM = 5;
private static final int X_SCALE_UNIT_LENGTH = 10;
private static final int X_SCALE_DIGITAL_HEIGHT = 15;
private static final double Y_SCALE_MIN_UNIT = 0.0000001;
private static final int Y_SCALE_INCREASE_FACTOR_5 = 5;
private static final int Y_SCALE_INCREASE_FACTOR_2 = 2;
private static final int Y_SCALE_NUM = 5;
private static final int Y_SCALE_UNIT_LENGTH = 10;
private static final int Y_SCALE_DIGITAL_HEIGHT = 40;
private static boolean init = false;
private Double[] xNumberArray;
private Double[] yNumberArray;
private Double xMaxNumber;
private Double yMaxNumber;
public MyPanel(Double[] xNumberArray, Double[] yNumberArray) {
this.xNumberArray = xNumberArray;
this.yNumberArray = yNumberArray;
for (int i = 0; i < xNumberArray.length; i++) {
if (xMaxNumber == null || xMaxNumber < xNumberArray[i]) {
xMaxNumber = xNumberArray[i];
}
}
for (int i = 0; i < yNumberArray.length; i++) {
if (yMaxNumber == null || yMaxNumber < yNumberArray[i]) {
yMaxNumber = yNumberArray[i];
}
}
}
@Override
public void paintComponent(Graphics g) {
if (init) {
System.err.println("paintComponent init=true,ignore");
PoissonFrame.getFrame().repaint();
// return;
}
init = true;
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
Color c = new Color(200, 70, 0);
g2D.setColor(c);
/**
* 画X轴
*/
g2D.drawLine(X_START, Y_START, X_END, Y_START);
/**
* 画Y轴
*/
g2D.drawLine(X_START, Y_START, X_START, Y_END);
/**
* 画X轴上箭头
*/
g2D.drawLine(X_END, Y_START, X_END - ARROW_LENGTH, Y_START - ARROW_LENGTH);
/**
* 画X轴下箭头
*/
g2D.drawLine(X_END, Y_START, X_END - ARROW_LENGTH, Y_START + ARROW_LENGTH);
/**
* 画Y轴左箭头
*/
g2D.drawLine(X_START, Y_END, X_START - ARROW_LENGTH, Y_END + ARROW_LENGTH);
/**
* 画Y轴右箭头
*/
g2D.drawLine(X_START, Y_END, X_START + ARROW_LENGTH, Y_END + ARROW_LENGTH);
/**
* 画原点
*/
g2D.drawString( "0", X_START - NUMBER_LENGTH, Y_START + NUMBER_LENGTH);
double xUnit = Double.valueOf(X_LENGTH) / xMaxNumber;
double yUnit = Double.valueOf(Y_LENGTH) / yMaxNumber;
/**
* 把相邻的两个点连线
*/
for (int i = 0; i < xNumberArray.length - 1; i++) {
System.err.printf("paintComponent g2D.drawLine(%s,%s,%s,%s) \n", X_START + (int) (xNumberArray[i] * xUnit), Y_START - (int) (yNumberArray[i] * yUnit), X_START + (int) (xNumberArray[i + 1] * xUnit), Y_START - (int) (yNumberArray[i + 1] * yUnit));
g2D.drawLine(X_START + (int) (xNumberArray[i] * xUnit), Y_START - (int) (yNumberArray[i] * yUnit),
X_START + (int) (xNumberArray[i + 1] * xUnit), Y_START - (int) (yNumberArray[i + 1] * yUnit));
g2D.drawLine(1200, 400, 1300, 500);
}
System.err.printf("paintComponent xUnit=%s,yUnit=%s,xMaxNumber=%s,yMaxNumber=%s \n", xUnit, yUnit, xMaxNumber, yMaxNumber);
/**
* 计算合适的X轴刻度
*/
Double xScaleUnit = X_SCALE_MIN_UNIT;
int currentXScaleIncreaseFactor = X_SCALE_INCREASE_FACTOR_5;
while (true) {
if (xMaxNumber / X_SCALE_NUM < xScaleUnit) {
break;
}
xScaleUnit = xScaleUnit * currentXScaleIncreaseFactor;
currentXScaleIncreaseFactor = currentXScaleIncreaseFactor == X_SCALE_INCREASE_FACTOR_5 ? X_SCALE_INCREASE_FACTOR_2 : X_SCALE_INCREASE_FACTOR_5;
}
currentXScaleIncreaseFactor = currentXScaleIncreaseFactor == X_SCALE_INCREASE_FACTOR_5 ? X_SCALE_INCREASE_FACTOR_2 : X_SCALE_INCREASE_FACTOR_5;
xScaleUnit = xScaleUnit / currentXScaleIncreaseFactor;
System.err.printf("paintComponent xScaleUnit=%s \n", xScaleUnit);
/**
* 计算合适的Y轴刻度
*/
Double yScaleUnit = Y_SCALE_MIN_UNIT;
int currentYScaleIncreaseFactor = Y_SCALE_INCREASE_FACTOR_5;
while (true) {
if (yMaxNumber / Y_SCALE_NUM < yScaleUnit) {
break;
}
yScaleUnit = yScaleUnit * currentYScaleIncreaseFactor;
currentYScaleIncreaseFactor = currentYScaleIncreaseFactor == Y_SCALE_INCREASE_FACTOR_5 ? Y_SCALE_INCREASE_FACTOR_2 : Y_SCALE_INCREASE_FACTOR_5;
}
currentXScaleIncreaseFactor = currentXScaleIncreaseFactor == X_SCALE_INCREASE_FACTOR_5 ? X_SCALE_INCREASE_FACTOR_2 : X_SCALE_INCREASE_FACTOR_5;
yScaleUnit = yScaleUnit / currentXScaleIncreaseFactor;
System.err.printf("paintComponent yScaleUnit=%s \n", yScaleUnit);
/**
* 画X轴的刻度
*/
Double xScaleUnitLength = (X_LENGTH / xMaxNumber) * xScaleUnit;
int xTimes = 0;
while (true) {
if (xScaleUnit * xTimes > xMaxNumber) {
break;
}
if (xTimes == 0) {
xTimes++;
continue;
}
String xScaleText = prettifyScaleText(String.valueOf((xScaleUnit >= 1 ? xScaleUnit.intValue() : xScaleUnit) * xTimes));
System.err.printf("paintComponent paint xScale=%s \n", xScaleText