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 :(得分: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
。在不同的OS上,Graphics
对象的行为可能会有所不同,这意味着有时候,在图形对象dispose
之前,它可能实际上没有绘制任何东西......