sách gpt4 ai đã đi

java - 如何循环 8*1 svg 图像(由 java 生成)以获得 8*8 svg 图像?

In lại 作者:行者123 更新时间:2023-11-30 11:04:12 31 4
mua khóa gpt4 Nike

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("\n", f,f,f,f);
System.out.format("\n",f,f,f,f);
System.out.format("\n",f,f,f,f,f,f,f,f);
verticleAliens();
System.out.println("");
}



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("\n", f,f,f,60+i,f,f,f,f,f,f,f,f,f);
System.out.format("\n", f,f,f,50+i,f,f,f,f,30+i,f,f,f,f,f);
System.out.format("\n", f,f,f,60+i,f,f,f,f,50+i,f,f,f,f,f);
System.out.format("\n", f,f,f,15+i,f,f,f,f,f);
System.out.format("\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图片,图片如下:

nhập mô tả hình ảnh ở đây

如你所愿,这张 svg 图片有 8*1 个不同的外星人。但我想得到一个 8*8 的外星人 svg 图像(外星人应该在水平方向上有相同的颜色图案)。我如何编写循环来实现这一点(或者,我如何修改我的 for 循环)?谢谢!

注意:如果您想在您的计算机上尝试这些代码,您可以使用java svg>aliens.svg 生成一个名为

的svg 文件

aliens.svg

编译程序后。然后在浏览器中打开它。

1 Câu trả lời

@Manuel_Jain 给了我一个非常有用的建议,我可以编写两个嵌套循环来解决我的问题。这是更新后的代码:

import java.util.Random;

class svg{
public static void main(String[] args){
//use slashQuote to stand for \" in order to avoid confusing when modifying svg codes.
String slashQuote="\"";
System.out.format("\n",
slashQuote,slashQuote,slashQuote,slashQuote);
System.out.format("\n",
slashQuote,slashQuote,slashQuote,slashQuote);
System.out.format("\n",
slashQuote,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote);
aliens();
System.out.println("");
}


//Most of works were done in this function. It prints all aliens.
public static void aliens(){
//use slashQuote to stand for \" in order to avoid confusing when modifying svg codes.
String slashQuote="\"";

//This loop generates different aliens (because of randomColor) verticlely. It is looping 1*8 aliens to get 8*8 aliens.
for(int i=0; i<=800; i+=100){

//get 7 colors by calling color() function for 7 times. The number is 7 because there will be 7 different color in one alien.
String randomColor1 = color();
String randomColor2 = color();
String randomColor3 = color();
String randomColor4 = color();
String randomColor5 = color();
String randomColor6 = color();
String randomColor7 = color();

//This loop generates identicle aliens horizontaly. It is looping a 1*1 alien to get 1*8 aliens.
//It will not affected by "String randomColorN = color();" because I only defined colors in outer loop.
for(int j=0;j<=800;j+=100){
//Here is my alien!!!!!!!!!!!!!!!!!!!!!
System.out.format("
"%s stroke-width=%s3%s fill=%s"+randomColor2+"%s />\n",
slashQuote,50+j,slashQuote,slashQuote,60+i,slashQuote,slashQuote,slashQuote,slashQuote,
slashQuote,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote);

System.out.format("
"%s stroke-width=%s5%s />\n",
slashQuote,50+j,slashQuote,slashQuote,50+i,slashQuote,slashQuote,50+j,slashQuote,
slashQuote,30+i,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote);

System.out.format("
"%s stroke-width=%s3%s />\n",
slashQuote,40+j,slashQuote,slashQuote,60+i,slashQuote,slashQuote,20+j,slashQuote,
slashQuote,50+i,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote);
System.out.format("\n",
slashQuote,36+j,slashQuote,slashQuote,15+i,slashQuote,slashQuote,slashQuote,slashQuote,slashQuote);
System.out.format("
";stroke-width:3;stroke:"+randomColor7+"%s/>\n\n",
slashQuote,40+j,slashQuote,slashQuote,10+i,slashQuote,slashQuote,slashQuote,slashQuote,
slashQuote,slashQuote,slashQuote);
}
}
}

//Generate a random number.
public static String color(){
Random randomGenerator = new Random();
int r = randomGenerator.nextInt(256);
int g = randomGenerator.nextInt(256);
int b = randomGenerator.nextInt(256);
/*#%02x%02x%02x converts the RGB color to the hexadecimal value. I tried to use RGB color at first
but it doesn't work in svg. Then I found this method in stack overflow. Here is the link:
http://stackoverflow.com/questions/3607858/how-to-convert-a-rgb-color-value-to-an-hexadecimal-value-in-java*/
return String.format("#%02x%02x%02x",r,g,b);
}
}

顺便说一句,这是新的 svg 图片: nhập mô tả hình ảnh ở đây

谢谢@Manuel_Jain!

关于java - 如何循环 8*1 svg 图像(由 java 生成)以获得 8*8 svg 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30093680/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com