thanks for the help
Description: The program draws, displays and saves the image. It works as follows: the object itself extends Frame. In the constructor, the object creates a BufferedImage and calls the method that draws on this image. Then it displays the image on the frame. Finally, it saves the image to a file (I don't care what format it uses). The main program creates an object that does the rest.
Problem: A saved file always has a colored background! This is especially strange since the displayed image is beautiful. If I use the "jpg" format with ImageIO.write (), the background will be reddish. If I use the format "png", the background is dark gray.
I spent some time on this and I still don't know what the hell is going on!
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[][] ;
private double availWidth ;
private double availHeight ;
public HGrapher(String saveFileName, int numRects) throws IOException {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); }
});
setColors( numRects) ;
this.availHeight = (3.0/4) * Toolkit.getDefaultToolkit().getScreenSize().height ;
this.availWidth = (3.0/4) * Toolkit.getDefaultToolkit().getScreenSize().width ;
this.img = new BufferedImage( (int)availWidth, (int)availHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D drawer = img.createGraphics() ;
drawer.setBackground(Color.WHITE);
this.makeImg( drawer) ;
this.setSize( new Dimension( (int)availWidth, (int)availHeight ) ) ;
this.setVisible(true);
ImageIO.write(img, "jpg",new File( (saveFileName +".jpg") ) );
}
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 ) ) ;
}
}
public void paint(Graphics g) {
Graphics2D drawer = (Graphics2D)g ;
drawer.drawImage(img, 0, 0, null) ;
}
public void setColors( int numRects) {
this.colors = new float[ numRects][4] ;
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 ; }
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 ; }
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) ;
}
}
source
share