Skip to content

Commit 8a2b5c7

Browse files
committed
init
1 parent 755afc3 commit 8a2b5c7

16 files changed

Lines changed: 1141 additions & 0 deletions

‎.classpath‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="test"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

‎.project‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaPlot</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
line.separator=\r\n
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.6

‎bin/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/com
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.sin.java.plot;
2+
3+
/**
4+
* Matlab上plot功能类似的Java实现,方便绘制波形
5+
*
6+
* @author RobinTang
7+
* @version 1.0
8+
* @see https://github.com/sintrb/JavaPlot
9+
*/
10+
public class JavaPlot {
11+
public static final String VERSION = "1.0";
12+
}

‎src/com/sin/java/plot/Plot.java‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package com.sin.java.plot;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.sin.java.plot.model.DrawableObject;
7+
import com.sin.java.plot.util.XLog;
8+
9+
/***
10+
* UI方法,提供图形绘制接口
11+
*
12+
* @author RobinTang
13+
*
14+
*/
15+
16+
final public class Plot {
17+
private static List<PlotFrame> plotFrameStack = null;
18+
private static PlotFrame currenPlotFrame = null;
19+
private static int plotFrameConter = 1;
20+
private static boolean autoFigure = false; // 自动创建图形窗口
21+
22+
/**
23+
* 初始化图形窗口栈
24+
*/
25+
private static void initPlotFrameStack() {
26+
if (plotFrameStack == null) {
27+
plotFrameStack = new ArrayList<PlotFrame>();
28+
}
29+
}
30+
31+
/**
32+
* 初始化当前图形窗口
33+
*/
34+
private static void initCurrentPlotFrame() {
35+
if (currenPlotFrame == null) {
36+
figrue();
37+
}
38+
}
39+
40+
// like matlab
41+
42+
/**
43+
* 开图形保存
44+
*/
45+
public static void hold_on() {
46+
initCurrentPlotFrame();
47+
currenPlotFrame.setHoldOn(true);
48+
}
49+
50+
/**
51+
* 关图形保持
52+
*/
53+
public static void hold_off() {
54+
initCurrentPlotFrame();
55+
currenPlotFrame.setHoldOn(false);
56+
}
57+
58+
/**
59+
* 开自动创建图形窗口
60+
*/
61+
public static void autofigure_on() {
62+
autoFigure = true;
63+
}
64+
65+
/**
66+
* 关自动创建图形窗口
67+
*/
68+
public static void autofigure_off() {
69+
autoFigure = false;
70+
}
71+
72+
/**
73+
* 打开图形窗口
74+
*/
75+
public static PlotFrame figrue() {
76+
return figrue(plotFrameConter);
77+
}
78+
79+
public static PlotFrame figrue(String title) {
80+
initPlotFrameStack();
81+
currenPlotFrame = null;
82+
for (PlotFrame fm : plotFrameStack) {
83+
if (title.equals(fm.getTitle())) {
84+
currenPlotFrame = fm;
85+
break;
86+
}
87+
}
88+
if (currenPlotFrame == null)
89+
addPlotFrame(new PlotFrame(title));
90+
return currenPlotFrame;
91+
}
92+
93+
public static PlotFrame figrue(int index) {
94+
return figrue("Figure " + index);
95+
}
96+
97+
/**
98+
* 绘图
99+
*/
100+
public static PlotFrame plot(DrawableObject drawableObject) {
101+
if (autoFigure)
102+
figrue();
103+
else
104+
initCurrentPlotFrame();
105+
currenPlotFrame.plot(drawableObject);
106+
Plot.suit();
107+
return currenPlotFrame;
108+
}
109+
110+
public static PlotFrame plot(double[] y) {
111+
return plot(new DrawableObject(y));
112+
}
113+
114+
public static PlotFrame plot(double[] x, double[] y) {
115+
return plot(new DrawableObject(x, y));
116+
}
117+
118+
public static PlotFrame plot(double[][] pts) {
119+
return plot(new DrawableObject(pts[0], pts[1]));
120+
}
121+
122+
public static PlotFrame plot(double[] x, String pams) {
123+
return plot(new DrawableObject(x, pams));
124+
}
125+
126+
public static PlotFrame plot(double[] x, double[] y, String pams) {
127+
return plot(new DrawableObject(x, y, pams));
128+
}
129+
130+
public static PlotFrame plot(double[][] pts, String pams) {
131+
return plot(new DrawableObject(pts[0], pts[1], pams));
132+
}
133+
134+
/**
135+
* 设置显示范围
136+
*/
137+
public static void axis(double sx, double ex, double sy, double ey) {
138+
initCurrentPlotFrame();
139+
currenPlotFrame.axis(sx, ex, sy, ey);
140+
}
141+
142+
/**
143+
* 最佳视野
144+
*/
145+
public static void suit() {
146+
initCurrentPlotFrame();
147+
currenPlotFrame.suit();
148+
}
149+
150+
// internal
151+
public static void addPlotFrame(PlotFrame frame) {
152+
XLog.log("add plotframe %s", frame.getTitle());
153+
initPlotFrameStack();
154+
++plotFrameConter;
155+
plotFrameStack.add(frame);
156+
currenPlotFrame = frame;
157+
}
158+
159+
public static boolean removePlotFrame(PlotFrame frame) {
160+
XLog.log("remove plotframe %s", frame.getTitle());
161+
plotFrameStack.remove(frame);
162+
return plotFrameStack.size() != 0;
163+
}
164+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.sin.java.plot;
2+
3+
import java.awt.GraphicsConfiguration;
4+
import java.awt.HeadlessException;
5+
import java.awt.event.WindowAdapter;
6+
import java.awt.event.WindowEvent;
7+
8+
import javax.swing.JFrame;
9+
10+
import com.sin.java.plot.model.DrawableObject;
11+
12+
/***
13+
* 绘图窗口,对应matlab的firgue
14+
* @author RobinTang
15+
*
16+
*/
17+
public class PlotFrame extends JFrame {
18+
private static final long serialVersionUID = 1L;
19+
20+
/**
21+
* 构造方法
22+
*/
23+
public PlotFrame() throws HeadlessException {
24+
super();
25+
this.commonInit();
26+
}
27+
28+
public PlotFrame(GraphicsConfiguration gc) {
29+
super(gc);
30+
this.commonInit();
31+
}
32+
33+
public PlotFrame(String title, GraphicsConfiguration gc) {
34+
super(title, gc);
35+
this.commonInit();
36+
}
37+
38+
public PlotFrame(String title) throws HeadlessException {
39+
super(title);
40+
this.commonInit();
41+
}
42+
43+
44+
private PlotPanle plotPanle = null;
45+
// 公共初始化
46+
private void commonInit(){
47+
plotPanle = new PlotPanle();
48+
this.add(plotPanle);
49+
this.setSize(300, 200);
50+
this.setLocationRelativeTo(null);
51+
this.addWindowListener(new WindowAdapter() {
52+
@Override
53+
public void windowClosing(WindowEvent e) {
54+
if(!Plot.removePlotFrame(PlotFrame.this)){
55+
System.exit(0);
56+
}
57+
}
58+
});
59+
this.setVisible(true);
60+
}
61+
62+
/**
63+
* 设置绘图保持
64+
*/
65+
public void setHoldOn(boolean holdOn) {
66+
this.plotPanle.setHoldOn(holdOn);
67+
}
68+
69+
/**
70+
* 绘图
71+
*/
72+
public void plot(DrawableObject drawableObject){
73+
this.plotPanle.plot(drawableObject);
74+
}
75+
76+
/**
77+
* 设置显示范围
78+
*/
79+
public void axis(double sx, double ex, double sy, double ey) {
80+
this.plotPanle.axis(sx, ex, sy, ey);
81+
}
82+
83+
/**
84+
* 最佳视野
85+
*/
86+
public void suit(){
87+
this.plotPanle.suit();
88+
}
89+
}

0 commit comments

Comments
 (0)