1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.diyefi.openlogviewer.graphing;
24
25 import java.awt.Color;
26 import java.awt.Graphics;
27 import java.awt.Graphics2D;
28
29 import javax.swing.JPanel;
30
31 import org.diyefi.openlogviewer.OpenLogViewerApp;
32 import org.diyefi.openlogviewer.genericlog.GenericLog;
33
34
35
36
37
38 public class GraphPositionPanel extends JPanel {
39
40 public GraphPositionPanel() {
41 super();
42 init();
43 }
44
45 private void init(){
46 this.setOpaque(true);
47 this.setLayout(null);
48 genLog = new GenericLog();
49 majorGraduationColor = Color.GRAY;
50 positionDataColor = majorGraduationColor;
51 backgroundColor = Color.BLACK;
52 setGraduationSpacing();
53 validSnappingPositions = new boolean[this.getWidth()];
54 }
55
56 @Override
57 public void paint(Graphics g) {
58 if (!this.getSize().equals(this.getParent().getSize())) {
59 this.setSize(this.getParent().getSize());
60 }
61 setGraduationSpacing();
62 Graphics2D g2d = (Graphics2D) g;
63 g2d.setColor(backgroundColor);
64 g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
65 if (genLog.getLogStatus() == GenericLog.LOG_NOT_LOADED) {
66 paintPositionBar(g2d);
67 } else if (genLog.getLogStatus() == GenericLog.LOG_LOADING) {
68 paintPositionBar(g2d);
69 } else if (genLog.getLogStatus() == GenericLog.LOG_LOADED) {
70 paintPositionBar(g2d);
71 paintPositionData(g2d);
72 setupMouseCursorLineSnappingPositions();
73 }
74 }
75
76 private void paintPositionBar(Graphics2D g2d){
77 int center = this.getWidth() / 2;
78 double graphPosition = OpenLogViewerApp.getInstance().getEntireGraphingPanel().getGraphPosition();
79 int zoom = OpenLogViewerApp.getInstance().getEntireGraphingPanel().getZoom();
80 double count = graphPosition * zoom;
81 count = Math.round(count);
82 g2d.setColor(majorGraduationColor);
83 for(int i = center; i > 0; i--){
84 if(count % (majorGraduationSpacing * zoom) == 0){
85 g2d.drawLine(i, 0, i, 6);
86 }
87 count--;
88 }
89 count = graphPosition * zoom;
90 count = Math.round(count);
91 for(int i = center; i < this.getWidth(); i++){
92 if(count % (majorGraduationSpacing * zoom) == 0){
93 g2d.drawLine(i, 0, i, 6);
94 }
95 count++;
96 }
97 g2d.drawLine(0, 0, this.getWidth(), 0);
98 }
99
100 private void paintPositionData(Graphics2D g2d){
101 int center = this.getWidth() / 2;
102 double graphPosition = OpenLogViewerApp.getInstance().getEntireGraphingPanel().getGraphPosition();
103 int zoom = OpenLogViewerApp.getInstance().getEntireGraphingPanel().getZoom();
104 double count = graphPosition * zoom;
105 count = Math.round(count);
106 g2d.setColor(positionDataColor);
107 for(int i = center; i > 0; i--){
108 if(count % (majorGraduationSpacing * zoom) == 0){
109 String positionDataString = Integer.toString((int)(count / zoom));
110 g2d.drawString(positionDataString, i - 10, 18);
111 }
112 count--;
113 }
114 count = graphPosition * zoom;
115 count = Math.round(count);
116 for(int i = center; i < this.getWidth(); i++){
117 if(count % (majorGraduationSpacing * zoom) == 0){
118 String positionDataString = Integer.toString((int)(count / zoom));
119 g2d.drawString(positionDataString, i - 10, 18);
120 }
121 count++;
122 }
123 }
124
125 private void setupMouseCursorLineSnappingPositions(){
126 int center = this.getWidth() / 2;
127 validSnappingPositions = new boolean[this.getWidth()];
128 double graphPosition = OpenLogViewerApp.getInstance().getEntireGraphingPanel().getGraphPosition();
129 int zoom = OpenLogViewerApp.getInstance().getEntireGraphingPanel().getZoom();
130 double count = graphPosition * zoom;
131 count = Math.round(count);
132
133 for(int i = center; i > 0; i--){
134 if(count % zoom == 0){
135 validSnappingPositions[i] = true;
136 }
137 count--;
138 }
139 count = graphPosition * zoom;
140 count = Math.round(count);
141
142 for(int i = center; i < this.getWidth(); i++){
143 if(count % zoom == 0){
144 validSnappingPositions[i] = true;
145 }
146 count++;
147 }
148 }
149
150 public void setLog(GenericLog log) {
151 genLog = log;
152 repaint();
153 }
154
155 private void setGraduationSpacing(){
156 int zoom = 1;
157 if(OpenLogViewerApp.getInstance() != null){
158 zoom = OpenLogViewerApp.getInstance().getEntireGraphingPanel().getZoom();
159 }
160 if(zoom > 64){
161 majorGraduationSpacing = 1;
162 } else if(zoom > 32){
163 majorGraduationSpacing = 2;
164 } else if(zoom > 16){
165 majorGraduationSpacing = 5;
166 } else if(zoom > 8){
167 majorGraduationSpacing = 10;
168 } else if(zoom > 4){
169 majorGraduationSpacing = 20;
170 } else if(zoom > 2){
171 majorGraduationSpacing = 25;
172 } else if(zoom > 1){
173 majorGraduationSpacing = 50;
174 } else {
175 majorGraduationSpacing = 100;
176 }
177 }
178
179 public int getBestSnappingPosition(int xMouseCoord){
180 int bestPosition = 0;
181 if(validSnappingPositions[xMouseCoord]){
182 bestPosition = xMouseCoord;
183 } else {
184 boolean found = false;
185 int startPosition = xMouseCoord;
186 for(int distance = 1; !found; distance++){
187 int next = startPosition + distance;
188 int prev = startPosition - distance;
189 if(next > validSnappingPositions.length - 1 || prev < 0){
190 bestPosition = xMouseCoord;
191 found = true;
192 } else if(validSnappingPositions[next]){
193 bestPosition = next;
194 found = true;
195 } else if(validSnappingPositions[prev]){
196 bestPosition = prev;
197 found = true;
198 }
199 }
200 }
201 return bestPosition;
202 }
203
204 private GenericLog genLog;
205 private Color majorGraduationColor;
206 private Color positionDataColor;
207 private Color backgroundColor;
208 private int majorGraduationSpacing;
209 private boolean[] validSnappingPositions;
210 private static final long serialVersionUID = -7808475370693818838L;
211
212 }