import java.awt.*; import javax.swing.*; import java.util.*; public class DistPanel extends JPanel { int x = 0; double min = Double.MAX_VALUE; double max = 0; double maxvahe = 0; double eelmine = -1; Vector p = null; double vahem = 2; public void paintComponent(Graphics g) { super.paintComponent(g); //paint background //Insets insets = getInsets(); //int currentWidth = getWidth() - insets.left - insets.right; //int currentHeight = getHeight() - insets.top - insets.bottom; //Draw image at its natural size first. g.setColor(Color.yellow); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.blue.darker()); g.drawLine(20, getHeight() - 20, 20, 20); g.drawLine(10, 20, 30, 20); g.drawString("1.0", 2, 31); g.drawLine(10, getHeight()/2 , 30, getHeight()/2 ); g.drawString("0.5", 2, getHeight()/2+11); g.drawLine(10, getHeight()/4+10 , 30, getHeight()/4+10 ); g.drawLine(10, 3*getHeight()/4-10 , 30, 3*getHeight()/4 -10); g.drawLine(20, getHeight() -20, getWidth()-20, getHeight()-20); g.drawString("0.0", 2, getHeight()-20); g.drawLine((getWidth()-20)/2+10, getHeight() -10, (getWidth()-20)/2+10, getHeight()-30); g.drawLine(getWidth()-20, getHeight() -10, getWidth()-20, getHeight()-30); if (x != 0) { g.drawString((new Double(min)).toString(), 10, getHeight()-7); g.drawString((new Double(max/2)).toString(), getWidth()/2-65, getHeight()-7); g.drawString((new Double(max)).toString(), getWidth()-65, getHeight()-7); // Joonistame pikkuste jaotuse Histogrammina double kursor = min; int c = 0; maxvahe = (max-min)/(((double)x)/vahem); double[] vahemikes = new double[(int)((x/vahem)+1)]; while(c < vahemikes.length) { vahemikes[c] = 0; vahemikes[c] = loendaFaileVahemikus(kursor, kursor+maxvahe); kursor += maxvahe; c++; } int rectX = 20; int deltaX = (getWidth()-40)/vahemikes.length; for(int i = 0; i < vahemikes.length; i++) { int y = (int)( ((double)(getHeight()-40)/(double)x) * vahemikes[i]); int startY = getHeight() -20 - y; g.fillRect(rectX,startY,deltaX, y ); rectX += deltaX; //g.drawString((new Integer(y)).toString(), 100, 80+i*20); //g.drawString((new Double(vahemikes[i])).toString(), 100, 80+i*20); } } g.setColor(getForeground()); } public void setVahem(int v) { vahem = (double)v; } public void setPoints(Vector points) { min = Double.MAX_VALUE; max = 0; maxvahe = 0; eelmine = -1; x = points.size(); Collections.sort(points); for(int i = 0; i < points.size(); i++) { double l = ((Double)points.elementAt(i)).doubleValue(); if (min >= l) min = l; if (max <= l) max = l; if (i == 1){ maxvahe = Math.abs(l-eelmine); } else { double v = Math.abs(l-eelmine); if (v > maxvahe) { maxvahe = v; } } eelmine = l; } // Liidame igaks juhuks ühe juurde, et iga vahemik sisaldaks mõne // faili pikkust maxvahe = maxvahe+1; p = points; if (min == max) max++; } private double loendaFaileVahemikus(double start, double end) { int count = 0; for(int i = 0; i < p.size(); i++) { double l = ((Double)p.elementAt(i)).doubleValue(); if (l < end) { if (l >= start) { count++; } } else { break; } } return (double)count; } }