本次将使用Java程序描写金刚石的一笔绘过程,将其输出成一张图片
下面展示一些 内联代码片
。
public class Polaris {
public static void main(String[] args) throws IOException {
//在内存中创建一个长80,宽30的图片,默认黑色背景
//参数一:长
//参数二:宽
//参数三:颜色
int width = 640;
int height = 480;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取图片
Graphics g = image.getGraphics();
//设置图片颜色为灰色
g.setColor(Color.GRAY);
//填充图片
g.fillRect(0, 0, width, height);
g.setColor(Color.red);
int n=7,i,j;
double t=3.14159*2/n,r=150.0;
double x0=300.0,y0=200.0;
int[] x =new int[7];
int[] y = new int[7];
for(i=0;i<n;i++)
{
x[i]= (int) (r*cos(i*t)+x0);
y[i]= (int) (r*sin(i*t)+y0);
}
for(i=1;i<=n/2;i++) {
for (j = 0; j < n; j++) {
if ((j + i) >= n) {
int temp = (j + i) % n;
g.drawLine(x[j], y[j], x[temp], y[temp]);
} else {
g.drawLine(x[j], y[j], x[j + i], y[j + i]);
}
}
}
ImageIO.write(image, "PNG", new File("D:\\Users\\frame\\graphics\\src\\edu\\ujs\\test1\\1.PNG"));
}
}