2
import java.util.Random;
class svg{
public static void main(String[] args){
String f="\""; //use f to stand for \" in order to avoid confusing when writing svg codes.
System.out.format("<?xml version=%s1.0%s standalone=%sno%s?>\n", f,f,f,f);
System.out.format("<!DOCTYPE svg PUBLIC %s-//W3C//DTD SVG 1.1//EN%s\n%shttp://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%s>\n",f,f,f,f);
System.out.format("<svg height=%s800%s width=%s800%s xmlns=%shttp://www.w3.org/2000/svg%s version=%s1.1%s>\n",f,f,f,f,f,f,f,f);
verticleAliens();
System.out.println("</svg>");
}
public static void verticleAliens(){
String f="\"";
for(int i=0; i<=800; i+=100){
String randomColor1 = color();
String randomColor2 = color();
String randomColor3 = color();
String randomColor4 = color();
String randomColor5 = color();
String randomColor6 = color();
String randomColor7 = color();
System.out.format("<circle cx=%s50%s cy=%s%d%s r=%s20%s stroke=%s"+randomColor1+"%s stroke-width=%s3%s fill=%s"+randomColor2+"%s />\n", f,f,f,60+i,f,f,f,f,f,f,f,f,f);
System.out.format("<line x1=%s50%s y1=%s%d%s x2=%s50%s y2=%s%d%s stroke=%s"+randomColor3+"%s stroke-width=%s5%s />\n", f,f,f,50+i,f,f,f,f,30+i,f,f,f,f,f);
System.out.format("<line x1=%s40%s y1=%s%d%s x2=%s20%s y2=%s%d%s stroke=%s"+randomColor4+"%s stroke-width=%s3%s />\n", f,f,f,60+i,f,f,f,f,50+i,f,f,f,f,f);
System.out.format("<circle cx=%s36%s cy=%s%d%s r=%s3%s fill=%s"+randomColor5+"%s />\n", f,f,f,15+i,f,f,f,f,f);
System.out.format("<rect x=%s40%s y=%s%d%s width=%s20%s height=%s20%s style=%sfill:"+randomColor6+";stroke-width:3;stroke:"+randomColor7+"%s/>\n\n",f,f,f,10+i,f,f,f,f,f,f,f);
}
}
public static String color(){
Random randomGenerator = new Random();
int r = randomGenerator.nextInt(256);
int g = randomGenerator.nextInt(256);
int b = randomGenerator.nextInt(256);
return String.format("#%02x%02x%02x",r,g,b);
}
}
这些Java代码可以生成具有连续8个不同的“外星人”一个SVG图像,这里是图像:如何循环8 * 1 svg图像(由java生成)以获得8 * 8 svg图像?
就可以了,这个SVG图片有8 * 1不同外星人。但我想获得一个8 * 8的外星人svg图像(外星人应该有水平相同的颜色模式)。我如何编写循环来实现(或者,我怎样才能修改我的for循环)?谢谢!
注意:如果你想尝试在您的计算机上这些代码,你可以使用java svg>aliens.svg
生成一个SVG文件,该文件名
aliens.svg
编译程序后。然后在浏览器中打开它。
两个嵌套循环怎么样?行的外层,列的内层。只在外部循环开始时,你定义了颜色,所以连续的每个外星人看起来都是一样的 –
哇谢谢!!!我已经解决了! – Turf
总是乐意提供帮助。也许你可以发布固定代码作为答案,以便问题得到解决? –