0

could you please tell me why the pixels won't set to red

    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();

thanks in advance.

user96454
  • 123
  • 1
  • 1
  • 9

1 Answers1

2

With respect, you seem to be going about this very strangely...

Rather then trying to draw directly to the pixel level, you should be making use of the Graphics API capabilities.

For example, clearing the image is going to be significantly faster using Graphics#fillRect then looping through and setting each pixel.

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();
    }
}

I ran your original code (with modifications to make it work) and it worked fine, but I've post some additional code that use Graphics instead.

You should make sure that you are calling Graphics#dispose. On different OS'es the Graphics object can behave differently, meaning that some times, until you dispose of the graphics object, it may not actually paint anything...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366