2013-07-12 101 views
0

你能告诉我为什么像素不会设置为红色setRGB()似乎并没有改变颜色

Color myColor = new Color(255, 0, 0); 
    int rgb = myColor.getRGB(); 
    String fileName = Config.IMAGEFILEPATH + "first_nodal_domain " 
       + "full.png"; 

     BufferedImage bi = new BufferedImage(AZIMUTH_RES, ELEVATION_RES, BufferedImage.TYPE_USHORT_GRAY); 
     for (int i = 0; i < AZIMUTH_RES; i++){ 
      for (int j = 0; j < ELEVATION_RES; j++){ 
       bi.setRGB(i,j,(255 << 16) + (255 << 8) + 255); 
      } 
     } 
     for (Point draw: shadedPoints){ 
      bi.setRGB(draw.x, draw.y, rgb); 
     } 
     BufferedImage scaledImage = new BufferedImage(
      1000, 1000, BufferedImage.TYPE_USHORT_GRAY); 

    // Paint scaled version of image to new image 
    Graphics2D graphics2D = scaledImage.createGraphics(); 
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
      RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
    graphics2D.drawImage(bi, 0, 0, 1000, 1000, null); 
     try { 
      // write out image to file as .png 
      ImageIO.write(scaledImage, "png", new File(fileName)); 
     } catch (IOException ex) { 
      Logger.getLogger(NodalDomainsDrawing.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     bi.flush(); 

在此先感谢。

+0

尝试调用'graphics2D.dispose()',然后再保存图片 – MadProgrammer

回答

2

恕我直言,你似乎是要对这个很奇怪......

甚则试图直接绘制到像素级,你应该利用的Graphics API功能。

例如,使用Graphics#fillRect然后遍历并设置每个像素,清除图像将显着加快。

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.RenderingHints; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import javax.imageio.ImageIO; 

public class TestImage02 { 

    public static void main(String[] args) { 
     Color myColor = new Color(255, 0, 0); 
//  int rgb = myColor.getRGB(); 

     List<Point> shadedPoints = new ArrayList<>(25); 
     for (int index = 0; index < 100; index++) { 
      shadedPoints.add(new Point(index, index)); 
     } 

     BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_USHORT_GRAY); 
     Graphics2D g2d = bi.createGraphics(); 
     g2d.setColor(Color.WHITE); 
     g2d.fillRect(0, 0, 100, 100); 
//  for (int i = 0; i < 100; i++) { 
//   for (int j = 0; j < 100; j++) { 
//    bi.setRGB(i, j, (255 << 16) + (255 << 8) + 255); 
//   } 
//  } 
     g2d.setColor(myColor); 
     for (Point draw : shadedPoints) { 
//   bi.setRGB(draw.x, draw.y, rgb); 
      g2d.drawLine(draw.x, draw.y, 1, 1); 
     } 
     g2d.dispose(); 
     BufferedImage scaledImage = new BufferedImage(
       1000, 1000, BufferedImage.TYPE_USHORT_GRAY); 

     // Paint scaled version of image to new image 
     Graphics2D graphics2D = scaledImage.createGraphics(); 
     graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
       RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     graphics2D.drawImage(bi, 0, 0, 1000, 1000, null); 
     graphics2D.dispose(); 
     try { 
      // write out image to file as .png 
      ImageIO.write(scaledImage, "png", new File("Test.png")); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 

     bi.flush(); 
    } 
} 

我跑你的原代码(经修改,使其工作),它工作得很好,但我已经张贴使用Graphics,而不是一些额外的代码。

您应该确保您致电Graphics#dispose。在不同的操作系统上,Graphics对象的行为可能会有所不同,这意味着有时候,直到您的图形对象的dispose,它可能实际上不会绘制任何东西...