java bufferedimage颜色_Java BufferedImage使用不需要的背景颜色进行保存

提前谢谢你的帮助

描述:

该程序绘制,显示和保存图像.它的工作原理如下:对象本身扩展了Frame.在构造函数中,该对象创建一个BufferedImage,并调用一个绘制到该图像上的方法.然后,它将图像显示在帧上.最后,它将图像保存到文件中(我不关心它使用的是什么格式).主程序创建对象,其余部分完成.

问题:

保存的文件始终具有彩色背景!由于显示的图像很好,这尤其奇怪.如果我在ImageIO.write()中使用“jpg”格式,背景会偏红.如果我使用“png”格式,背景为深灰色.

我花了一段时间在这上面,我仍然不知道到底发生了什么!

import java.awt.Frame ;

import java.awt.image.BufferedImage ;

import java.io.IOException ;

import java.awt.event.WindowEvent ;

import java.awt.event.WindowAdapter ;

import java.awt.Toolkit ;

import java.awt.Graphics2D ;

import java.awt.Graphics ;

import java.awt.Color ;

import java.awt.Dimension ;

import javax.imageio.ImageIO ;

import java.io.File ;

import java.awt.geom.Rectangle2D;

public class HGrapher extends Frame{

private BufferedImage img ;

private float colors[][] ; //the colors for every rectangle

private double availWidth ;

private double availHeight ;

public HGrapher(String saveFileName, int numRects) throws IOException {

//*add window closer

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0); }

});

//*get colors to use

setColors( numRects) ;

//*figure out the size of the image and frame

this.availHeight = (3.0/4) * Toolkit.getDefaultToolkit().getScreenSize().height ;

this.availWidth = (3.0/4) * Toolkit.getDefaultToolkit().getScreenSize().width ;

//*create the image

this.img = new BufferedImage( (int)availWidth, (int)availHeight, BufferedImage.TYPE_INT_ARGB);

Graphics2D drawer = img.createGraphics() ;

drawer.setBackground(Color.WHITE);

this.makeImg( drawer) ;

//*display the image

this.setSize( new Dimension( (int)availWidth, (int)availHeight ) ) ;

this.setVisible(true);

//*save the image

ImageIO.write(img, "jpg",new File( (saveFileName +".jpg") ) );

}

//*draws the image by filling rectangles whose color are specified by this.colors

public void makeImg( Graphics2D drawer) {

double rectWidth = this.availWidth / (double)colors.length ;

for(int i = 0 ; i < colors.length ; i ++) {

drawer.setColor( new Color( this.colors[i][0], this.colors[i][1], this.colors[i][2],

this.colors[i][3] ) ) ;

drawer.fill( new Rectangle2D.Double( rectWidth*i, 0, rectWidth, this.availHeight ) ) ;

}

}

//*paint method

public void paint(Graphics g) {

Graphics2D drawer = (Graphics2D)g ;

drawer.drawImage(img, 0, 0, null) ;

}

//*creates an array of the colors rectangles are filled with

public void setColors( int numRects) {

this.colors = new float[ numRects][4] ;

//*make every 1st rect red

for(int i = 0 ; i< colors.length ; i+= 3) {

this.colors[i][0] = (float).8 ; this.colors[i][1] = (float).1 ; this.colors[i][2] = (float).1 ;

this.colors[i][3] = (float).8 ; }

//*make every 2nd rect green

for(int i = 1 ; i< colors.length ; i+= 3) {

this.colors[i][0] = (float).1 ; this.colors[i][1] = (float).8 ; this.colors[i][2] = (float).1 ;

this.colors[i][3] = (float).8 ; }

//*make every 3rd rect

for(int i = 2 ; i< colors.length ; i+= 3) {

this.colors[i][0] = (float).1 ; this.colors[i][1] = (float).1 ; this.colors[i][2] = (float).8 ;

this.colors[i][3] = (float).8 ; }

}

public static void main (String[]args) throws IOException {

HGrapher hg = new HGrapher("saved", 14) ;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值