我正在使用swing编写一个java应用程序,需要在一个正方形上方绘制一个网格。为此,我使用了
我可能做错了什么。以下是网格的代码。
Graphics
类提供的drawLine(...)
方法。除了每条线都需要很长时间来绘制(50条线需要超过20秒...),其他一切都运行良好。我甚至可以实时看到线条的绘制过程。一个奇怪的问题是,水平线比垂直线绘制得快得多(几乎瞬间完成)。我可能做错了什么。以下是网格的代码。
public void drawGrid(Graphics g){
g.setColor(new Color(255, 255, 255, 20));
int width = getWidth();
int height = (int) (width * Utils.PLATE_RATIO);
int step = pixelSize*gridSpacing;
Color bright = new Color(255, 255, 255, 100);
Color transparent = new Color(255, 255, 255, 20);
for(int ix = insets.left + step;
ix < width; ix += step){
if(((ix - insets.left) / step) % 10 == 0){
g.setColor(bright);
}
else{
g.setColor(transparent);
}
g.drawLine(ix, insets.top, ix, height+insets.top);
}
for(int iy = insets.top+step;
iy < (insets.top + height); iy += step){
if(((iy - insets.top) / step) % 10 == 0){
g.setColor(bright);
}
else{
g.setColor(transparent);
}
g.drawLine(insets.left, iy, width + insets.left, iy);
}
}