おかげで先に助けのJavaのBufferedImageは
説明のための時間の不要な背景色で保存されます。次のように動作します。オブジェクト自体がフレームを拡張します。コンストラクタでは、オブジェクトは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) ;
}
}
ありがとう、バッチピーター!この方法は.jpg形式では動作しません(純粋な黒い画像が得られます)が、.png形式で動作します。私は部分的になぜそれが1つのフォーマットで動作し、別のものではないかを尋ねる傾向がある...とにかく、感謝のピーター! – whearyou
'clearRect()'はおそらく0のアルファ値を残すでしょう。JPGは透明度をサポートしておらず、何かアルファが0の場合はおそらく黒になるでしょう。 'setBackground 'の代わりに' setColor() 'と' fillRect() () 'と' clearRect() 'を使ってJPGと動作させる – PeterT