When I attached a profiler (NetBeans) to my application, I found that half of the cpu time used by the paint mthod belonged to the drawLine(); method I use to draw a large grid across a large window (1000 by 650).
The grid has to be refreshed constantly, as I implemented panning as so:
/*
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);
Is there anyway to reduce cpu usage (drastically)?
*the view Rectangle is for use in panning, its the size of the window but 'ajusts' its position to accommodate the pan variables.