33import java .awt .Color ;
44import java .awt .Graphics ;
55import java .awt .Graphics2D ;
6+ import java .awt .event .ActionEvent ;
7+ import java .awt .event .ActionListener ;
68import java .util .ArrayList ;
7- import java .util .List ;
89
10+ import javax .swing .JButton ;
11+ import javax .swing .JLabel ;
912import javax .swing .JPanel ;
1013
14+ @ SuppressWarnings ("serial" )
1115public class GraphingPanel extends JPanel {
1216 private final static int MAX_STAT = 1000 ;
13- private final static int SIZE_CUT = 10 ;
14- private final static int CAPACITY = MAX_STAT / SIZE_CUT ;
15- private ArrayList <Integer > data = new ArrayList <Integer >(CAPACITY );
17+ private static int SIZE_CUT = 10 ;
18+ private static int CAPACITY = MAX_STAT / SIZE_CUT ;
19+ private ArrayList <Integer > graphData = new ArrayList <Integer >(CAPACITY );
20+ private ArrayList <Integer > pureData = new ArrayList <Integer >();
1621 private double max = 0 ;
1722
1823 public GraphingPanel () {
19- clear ();
24+ JButton btnIncreaseAccuracy = new JButton ("-" );
25+ JButton btnDecreaseAccuracy = new JButton ("+" );
26+ JButton btnReset = new JButton ("Reset" );
27+ JLabel lbl = new JLabel ("Grouping: " + SIZE_CUT );
28+ btnIncreaseAccuracy .addActionListener (new ActionListener () {
29+ @ Override
30+ public void actionPerformed (ActionEvent e ) {
31+ SIZE_CUT -= 1 ;
32+ if (SIZE_CUT <= 0 ) {
33+ SIZE_CUT = 1 ;
34+ }
35+ CAPACITY = MAX_STAT / SIZE_CUT ;
36+ graphData = new ArrayList <Integer >(CAPACITY );
37+ lbl .setText ("Grouping: " + SIZE_CUT );
38+ clearGraphData ();
39+ for (int i : pureData ) {
40+ addValue (i , false );
41+ }
42+ repaint ();
43+ }
44+ });
45+ btnDecreaseAccuracy .addActionListener (new ActionListener () {
46+ @ Override
47+ public void actionPerformed (ActionEvent e ) {
48+ SIZE_CUT += 1 ;
49+ int max = MAX_STAT / 10 ;
50+ if (SIZE_CUT >= max ) {
51+ SIZE_CUT = max ;
52+ }
53+ CAPACITY = MAX_STAT / SIZE_CUT ;
54+ graphData = new ArrayList <Integer >(CAPACITY );
55+ lbl .setText ("Grouping: " + SIZE_CUT );
56+ clearGraphData ();
57+ for (int i : pureData ) {
58+ addValue (i , false );
59+ }
60+ repaint ();
61+ }
62+ });
63+ btnReset .addActionListener (new ActionListener () {
64+ @ Override
65+ public void actionPerformed (ActionEvent e ) {
66+ pureData .clear ();
67+ clearGraphData ();
68+ repaint ();
69+ }
70+ });
71+ add (lbl );
72+ add (btnIncreaseAccuracy );
73+ add (btnDecreaseAccuracy );
74+ add (btnReset );
75+ clearGraphData ();
2076 }
2177
2278 @ Override
@@ -31,50 +87,56 @@ public void paintComponent(Graphics gg) {
3187 g .setColor (Color .black );
3288
3389 int minY = 0 , maxY = 1 ;
34- for (int i = 0 ; i < data .size (); i ++) {
35- int val = data .get (i );
90+ for (int i = 0 ; i < graphData .size (); i ++) {
91+ int val = graphData .get (i );
3692 if (val > 0 ) {
3793 if (minY == 0 ) {
3894 minY = i ;
3995 maxY = i + 1 ;
40- }
41- else {
96+ } else {
4297 maxY = i ;
4398 }
4499 }
45100 }
46101 for (int i = minY ; i < maxY ; i ++) {
47- int val = data .get (i );
102+ int val = graphData .get (i );
48103 double he = val == 0 ? 0 : val / max ;
49104 int rw = w / (maxY - minY );
50- int rx = (i - minY ) * rw ;
105+ int rx = (i - minY ) * rw ;
51106 int rh = (int ) Math .round ((he ) * h / 1.3 );
52107 int ry = h - rh ;
53108 g .fillRect (rx , ry , rw , rh );
54109 }
55110 }
56111
57- public void clear () {
112+ private void clearGraphData () {
58113 max = 0 ;
59- while (data .size () <= CAPACITY ) {
60- data .add (0 );
114+ while (graphData .size () <= CAPACITY ) {
115+ graphData .add (0 );
61116 }
62- for (int i = 0 ; i < data .size (); i ++) {
63- data .set (i , 0 );
117+ for (int i = 0 ; i < graphData .size (); i ++) {
118+ graphData .set (i , 0 );
64119 }
65120 }
66121
67- public void addValue (int i ) {
122+ private void addValue (int i , boolean addData ) {
123+ if (addData )
124+ pureData .add (i );
68125 if (i > MAX_STAT ) {
69126 i = MAX_STAT ;
70127 }
128+
71129 i /= SIZE_CUT ;
72130 //
73- int val = data .get (i );
131+ int val = graphData .get (i );
74132 if (val + 1 > max ) {
75133 max = val + 1 ;
76134 }
77- data .set (i , val + 1 );
135+ graphData .set (i , val + 1 );
78136 repaint ();
79137 }
138+
139+ public void addValue (int i ) {
140+ addValue (i , true );
141+ }
80142}
0 commit comments