绘制出图像点的图表点限制了一些计算错误

时间:2016-07-14 02:10:04

标签: java bufferedimage

这是我在下面的代码,用于在空白图像上绘制图表。 问题是getY()返回超出图像高度范围的点 这就是这条线:

yy= mid + ((basePoint - val)*5);
evaluates to:
( 150 + ((90.0f - 92.0f)*5) );
( 150 + ((90.0f - 89.25f)*5) );
( 150 + ((90.0f - 89.0f)*5) );
and so on...
( 150 + ((90.0f - 127.0f)*5) );

当它达到127.0f的高范围时,坐标都会超出图像范围。

如果我减少(* 5),那么差异非常小的点看起来就好像它们在一条直线上,如:89.0f和89.25f会形成一条直线,但我希望89.25f是一个直线略高于89.0f,可见差异。

我可以将该计算更改为其他内容,以便它始终保留在图像边界内吗?即使127.0f超过150.0f?

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import javax.imageio.stream.*;


class plotChart{

int w= 1400;
int h= 300;
BufferedImage chart = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB );

Integer mid = h/2;
Float basePoint = 0.0f;

public Integer getY( Float val ){
Integer y;
Float yy;

yy= mid + ((basePoint - val)*5);
y= yy.intValue();

return y;
}//method end getY

public static void main( String[] args ){
plotChart pp= new plotChart();


Float[] f={ 90.0f, 92.0f, 89.25f, 94.0f, 97.0f, 99.0f, 102.0f, 110.0f, 115.0f, 120.0f, 125.0f, 127.0f, 130.0f, 60.35f, 64.0f, 70.0f, 74.15f, 74.0f, 75.50f, 88.0f  };

Graphics2D gg= pp.chart.createGraphics();
BasicStroke bs = new BasicStroke(3);
gg.setStroke(bs);

Integer x=0; Integer y= pp.mid;
Integer xx, yy;

pp.basePoint = f[0];

for(int i=0; i<f.length; ++i){

xx= i * 20;
yy= pp.getY( f[i] );

gg.drawLine( x, y, xx, yy );

x=xx; y=yy;

//try{ ImageIO.write( pp.chart, "png", new FileOutputStream( "ppChart.png") ); }catch(Exception e){ e.printStackTrace(); }


}//for


gg.dispose();

try{
ImageIO.write( pp.chart, "png", new FileOutputStream( "ppChart.png") );
}catch(Exception e){ e.printStackTrace(); }

}//main ends
}//class ends

1 个答案:

答案 0 :(得分:0)

你可以做到的一种方法就是让图形触及图像的顶部。要做到这一点,你需要从basePoint获得最远的值,并将一个浮点值添加到类“plotChart”(我使用“diff”)。设置basePoint后,此代码将立即执行:

CREATE TRIGGER TR_zmatplant_INSERT 
ON zmatplant  
FOR INSERT  
AS  
BEGIN  
    INSERT INTO filter (mn)  
        SELECT material  
        FROM Inserted  
        INNER JOIN filter ON zmatplant.valcl = zmvclas.valcl  
        WHERE zmvclas.valcl = 'MRO'  
END

然后你需要改变getY()中的代码:

float max = f[0];
for (float fl : f){
    if (fl>max){
        max = fl;
    }
}
float min = f[0];
for (float fl : f){
    if (fl<min){
        min = fl;
    }
}
float maxdiff = max-pp.basePoint;
float mindiff = pp.basePoint-min;
float diff;
if (maxdiff>mindiff){
    diff = maxdiff;
} else {
    diff = mindiff;
}
pp.diff = diff;

这将使图形大小基于图像的大小,因为我们知道basePoint-val的绝对值将始终在0和diff之间(除非是diff),因为diff是距离的最大距离basePoint。最终结果是:

end result when run