用java代码用for循环画几个画布为黑色,像素为800*480的,三角形为白色,并且保存到自定义的文件夹里
时间: 2023-05-21 15:03:28 浏览: 130
以下是用Java代码使用for循环画黑色画布、白色三角形并保存到自定义文件夹的示例代码:
```java
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class DrawCanvas {
public static void main(String[] args) {
int width = 800;
int height = 480;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.WHITE);
int[] xPoints = {width/2, 0, width};
int[] yPoints = {0, height, height};
Polygon triangle = new Polygon(xPoints, yPoints, 3);
g2d.fill(triangle);
g2d.dispose();
File output = new File("custom_folder/canvas.png");
try {
ImageIO.write(image, "png", output);
System.out.println("Image saved to " + output.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请注意,您需要在代码中将“custom_folder”替换为您要保存图像的实际文件夹路径。
阅读全文