java创建图片方法是,如何用Java创建图像

say in my program, i have this paint() method. my wish is to create an image of the rectangles that are drawn (with the for loop). I tried the method below and it did give me those rectangles (blue color), but the background is all black. When I run program without creating image, just drawing the rect on a JFrame, the background is white. How can i fix this. ?

public void paint(Graphics g) {

super.paint(g);

BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

g = Image.getGraphics(); <<

g.setColor(Color.blue);

for ( ..... ) {

g.fillRect(X , Y, width , height);

....

}

try {

ImageIO.write(image, "jpg", new File("CustomImage.jpg"));

}catch (IOException e) {

e.printStackTrace();

}

}

解决方案

The background is black in your image because you are not giving any pixels a value except those in the rectangles. The BufferedImage is starting out with every pixel having RGB of (0, 0, 0), which is black. To give the entire image a white background, simply fill the entire rectangle that is the image with white.

BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

g = image.createGraphics(); // not sure on this line, but this seems more right

g.setColor(Color.white);

g.fillRect(0, 0, 100, 100); // give the whole image a white background

g.setColor(Color.blue);

for( ..... ){

g.fillRect(X , Y, width , height );

....

}

Note that my answer is about writing the image to a file with a white background, not about drawing to the JFrame with a black background. I'm not entirely sure which one you wanted.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用Graphics2D和Java2D API来创建圆角图像。以下是一种常见的方法: 1. 创建一个BufferedImage对象,并获取其Graphics2D上下文。 ```java BufferedImage image = ImageIO.read(new File("input.jpg")); BufferedImage roundedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = roundedImage.createGraphics(); ``` 2. 创建一个圆形的剪切区域,并将该区域设置为当前Graphics2D上下文的剪切区域。 ```java Ellipse2D.Double clip = new Ellipse2D.Double(0, 0, image.getWidth(), image.getHeight()); g.setClip(clip); ``` 3. 将原始图像绘制到圆形剪切区域内。 ```java g.drawImage(image, 0, 0, null); g.dispose(); ``` 4. 将结果保存到文件。 ```java ImageIO.write(roundedImage, "png", new File("output.png")); ``` 完整代码示例: ```java import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class RoundImage { public static void main(String[] args) throws Exception { BufferedImage image = ImageIO.read(new File("input.jpg")); BufferedImage roundedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = roundedImage.createGraphics(); Ellipse2D.Double clip = new Ellipse2D.Double(0, 0, image.getWidth(), image.getHeight()); g.setClip(clip); g.drawImage(image, 0, 0, null); g.dispose(); ImageIO.write(roundedImage, "png", new File("output.png")); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值