在java paint方法中有效地绘制网格

时间:2015-01-29 04:06:33

标签: java netbeans paint cpu-usage

当我将一个分析器(NetBeans)连接到我的应用程序时,我发现paint mthod使用的一半cpu时间属于drawLine();我用来在大窗口(1000乘650)上绘制一个大网格的方法。

网格必须不断更新,因为我实现了平移:

/*
A simplified section of the MAIN class
*/
//size of grid squares
public static Dimension pixelgridsize = new Dimension(50,50);
//size in grid squares - initiated in main.
public static Dimension gridsize ;
//panning
public static double panx = 0;
public static double pany = 0;
@Override
public void paint(Graphics g2) {
    Graphics2D g = (Graphics2D)g2;
    Color thing = g.getColor();
    for(int i  = 0; i<gridsize.width;i++){
        if((i*pixelgridsize.width)>view.getX()&&
        (i*pixelgridsize.width)<view.x+view.width){
        g.drawLine((int)(i*pixelgridsize.width-panx), 0,(int)(i*pixelgridsize.width-panx), 700);}
    }
    for(int i  = 0; i<gridsize.height;i++){
        if((i*pixelgridsize.height)>view.getY()&&
    (i*pixelgridsize.height)<view.y+view.height){
        g.drawLine(0,(int)(i*pixelgridsize.height-pany), 1000, (int)(i*pixelgridsize.height-pany));}
    }
    g.setColor(thing);

有没有减少CPU使用率(大幅度)?

*视图Rectangle用于平移,它是窗口的大小,但“调整”它的位置以容纳平移变量。

0 个答案:

没有答案