📄 graph.java
字号:
float getMaxValue() {
if (!this.actualMaximumValue) {
this.maximumValue = this.getMaxValue(0, this.yValues.size());
this.actualMaximumValue = true;
}
return this.maximumValue;
}
float getMaxValue(int a, int b) {
float maxValue = 0.0f;
for (int i=a; i<b; i++)
maxValue = Math.max(maxValue, yValues.elementAt(i));
return maxValue;
}
float getMaxValue(float a, float b) {
int ia = (int)(a*yValues.size());
int ib = (int)(b*yValues.size());
return getMaxValue(ia, ib);
}
int getMaxValueIndex(int a, int b) {
float maxValue = 0.0f;
int maxIndex = a;
for (int i=a; i<b; i++) {
if (yValues.elementAt(i) >= maxValue) {
maxValue = yValues.elementAt(i);
maxIndex = i;
}
}
return maxIndex;
}
// float getMinValue() {
// if (!this.actualMinimumValue) {
// float minValue = Float.POSITIVE_INFINITY;
// for (int i=0; i<yValues.size(); i++)
// minValue = Math.min(minValue, yValues.elementAt(i));
//
// this.minimumValue = minValue;
// this.actualMinimumValue = true;
// }
// return this.minimumValue;
// }
float getMinValue() {
if (!this.actualMinimumValue) {
this.minimumValue = this.getMinValue(0, this.yValues.size());
this.actualMinimumValue = true;
}
return this.minimumValue;
}
float getMinValue(int a, int b) {
float minValue = Float.POSITIVE_INFINITY;
for (int i=a; i<b; i++)
minValue = Math.min(minValue, yValues.elementAt(i));
return minValue;
}
float getMinValue(float a, float b) {
int ia = (int)(a*yValues.size());
int ib = (int)(b*yValues.size());
return getMinValue(ia, ib);
}
int getMinValueIndex(int a, int b) {
float minValue = Float.POSITIVE_INFINITY;
int minIndex = b;
for (int i=a; i<b; i++) {
if (yValues.elementAt(i) <= minValue) {
minValue = yValues.elementAt(i);
minIndex = i;
}
}
return minIndex;
}
//
public BufferedImage renderHorizontally(int width, int height) {
BufferedImage content = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
BufferedImage axis = new BufferedImage(width+40, height+40, BufferedImage.TYPE_INT_RGB);
Graphics2D graphicContent = content.createGraphics();
Graphics2D graphicAxis = axis.createGraphics();
Rectangle backRect = new Rectangle(0,0,width+40,height+40);
graphicAxis.setColor(Color.LIGHT_GRAY);
graphicAxis.fill(backRect);
graphicAxis.draw(backRect);
backRect = new Rectangle(0,0,width,height);
graphicContent.setColor(Color.WHITE);
graphicContent.fill(backRect);
graphicContent.draw(backRect);
int x,y,x0,y0;
x=0;y=0;
graphicContent.setColor(Color.GREEN);
for (int i=0; i<this.yValues.size(); i++) {
x0=x; y0=y;
x = (int) ( ( (float)i / this.yValues.size() ) * width );
y = (int) ( ( (float) 1 - (this.yValues.elementAt(i) / this.getMaxValue())) * height );
graphicContent.drawLine(x0,y0,x,y);
}
if (this.peaks!=null) { // uz boli vyhladane aj peaky, renderujeme aj tie
graphicContent.setColor(Color.RED);
int i = 0;
double multConst = (double)width / this.yValues.size();
for (Peak p : this.peaks) {
graphicContent.drawLine((int)(p.left * multConst), 0, (int)(p.center * multConst),30);
graphicContent.drawLine((int)(p.center * multConst), 30, (int)(p.right * multConst),0);
graphicContent.drawString((i++)+"." ,(int)(p.center * multConst) -5, 42);
}
}
graphicAxis.drawImage(content,35,5,null);
graphicAxis.setColor(Color.BLACK);
graphicAxis.drawRect(35,5,content.getWidth(), content.getHeight());
for (int ax = 0; ax < content.getWidth(); ax += 50) {
graphicAxis.drawString(new Integer(ax).toString() , ax + 35, axis.getHeight()-10);
graphicAxis.drawLine(ax+35, content.getHeight()+5 ,ax+35, content.getHeight()+15);
}
for (int ay = 0; ay < content.getHeight(); ay += 20) {
graphicAxis.drawString(
new Integer(new Float((1-(float)ay/content.getHeight())*100).intValue()).toString() + "%"
, 1 ,ay + 15);
graphicAxis.drawLine(25,ay+5,35,ay+5);
}
graphicContent.dispose();
graphicAxis.dispose();
return axis;
}
public BufferedImage renderVertically(int width, int height) {
BufferedImage content = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
BufferedImage axis = new BufferedImage(width+10, height+40, BufferedImage.TYPE_INT_RGB);
Graphics2D graphicContent = content.createGraphics();
Graphics2D graphicAxis = axis.createGraphics();
Rectangle backRect = new Rectangle(0,0,width+40,height+40);
graphicAxis.setColor(Color.LIGHT_GRAY);
graphicAxis.fill(backRect);
graphicAxis.draw(backRect);
backRect = new Rectangle(0,0,width,height);
graphicContent.setColor(Color.WHITE);
graphicContent.fill(backRect);
graphicContent.draw(backRect);
int x,y,x0,y0;
x=width;y=0;
graphicContent.setColor(Color.GREEN);
for (int i=0; i<this.yValues.size(); i++) {
x0=x; y0=y;
y = (int) ( ( (float)i / this.yValues.size() ) * height );
x = (int) ( ( (float) (this.yValues.elementAt(i) / this.getMaxValue())) * width );
graphicContent.drawLine(x0,y0,x,y);
}
if (this.peaks!=null) { // uz boli vyhladane aj peaky, renderujeme aj tie
graphicContent.setColor(Color.RED);
int i = 0;
double multConst = (double)height / this.yValues.size();
for (Peak p : this.peaks) {
graphicContent.drawLine(width,(int)(p.left * multConst), width-30, (int)(p.center * multConst));
graphicContent.drawLine(width-30, (int)(p.center * multConst), width, (int)(p.right * multConst));
graphicContent.drawString((i++)+"." ,width-38, (int)(p.center * multConst)+5);
}
}
graphicAxis.drawImage(content,5,5,null);
graphicAxis.setColor(Color.BLACK);
graphicAxis.drawRect(5,5,content.getWidth(), content.getHeight());
// for (int ax = 0; ax < content.getWidth(); ax += 50) {
// graphicAxis.drawString(new Integer(ax).toString() , ax + 35, axis.getHeight()-10);
// graphicAxis.drawLine(ax+35, content.getHeight()+5 ,ax+35, content.getHeight()+15);
// }
//
// for (int ay = 0; ay < content.getHeight(); ay += 20) {
// graphicAxis.drawString(
// new Integer(new Float((1-(float)ay/content.getHeight())*100).intValue()).toString() + "%"
// , 1 ,ay + 15);
// graphicAxis.drawLine(25,ay+5,35,ay+5);
// }
graphicContent.dispose();
graphicAxis.dispose();
return axis;
}
public void rankFilter(int size) {
int halfSize = size/2;
//Vector<Float> clone = (Vector<Float>)this.yValues.clone();
Vector<Float> clone = new Vector<Float>(this.yValues);
for (int i=halfSize; i < this.yValues.size() - halfSize; i++) {
float sum = 0;
for (int ii = i - halfSize; ii<i+halfSize; ii++) {
sum+=clone.elementAt(ii);
}
this.yValues.setElementAt(sum / size, i);
}
}
public int indexOfLeftPeakRel(int peak, double peakFootConstantRel) {
int index=peak;
for (int i=peak; i>=0; i--) {
index = i;
if (yValues.elementAt(index) < peakFootConstantRel*yValues.elementAt(peak) ) break;
}
return Math.max(0,index);
}
public int indexOfRightPeakRel(int peak, double peakFootConstantRel) {
int index=peak;
for (int i=peak; i<yValues.size(); i++) {
index = i;
if (yValues.elementAt(index) < peakFootConstantRel*yValues.elementAt(peak) ) break;
}
return Math.min(yValues.size(), index);
}
public float averagePeakDiff(Vector<Peak> peaks) { // not used
float sum = 0;
for (Peak p : peaks)
sum+= p.getDiff();
return sum/peaks.size();
}
public float maximumPeakDiff(Vector<Peak> peaks, int from, int to) {
float max = 0;
for (int i=from; i<=to; i++)
max = Math.max(max,peaks.elementAt(i).getDiff());
return max;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -