0

I have a txt file with nrows, ncols etc and ex elevation values as numbers which we are supposed to make a map image of.

I have code (that works) but my problem is that the pixels are exactly one pixel big each, they actually have a cellsize also that already is defined(10m). When the code runs I get the smallest greyscale map ever, I want it to be atleast 10x10 cm big imageicon so I can se what's going on but I don't know where to set it please help. Already searched for ways to resize etc but noone really fit me, what about a setpixels solution?

public void mapColor()
{
    int height = nRows; int width = nCols;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();
    for (int i = 0; i < nRows; i++) { 
        for (int j = 0; j < nCols; j++) {
            double value = values[i][j];
            int[] color = new int[3];
            int colValue = getColorValue(value); //In this case Green to Red strength
            int percentage = ((100*colValue)/255);
            int R; int G; int B = 0;
            //Formula: Red= 255*percentage /100 G= (255*(100-percentage))/100
            R = (255*percentage)/100; G=(255*(100-percentage))/100;
            R = Math.round(R); G= Math.round(G);
            color[0]= R; color[1] = G; color[2] = B; //100% will be bright red and 50% yellow
            raster.setPixel(j, i, color);
            //System.out.println("Value " + value + " Red is " + R + " Green is " + G);
        }
    }

    JFrame jf = new JFrame();
    JLabel jl = new JLabel();
    ImageIcon ii = new ImageIcon(image);
    jl.setIcon(ii);
    jf.add(jl);
    jf.setSize(200, 200);
    jf.setVisible(true);
}

This returns an image that is 2x2mm big, values is a double array with all elevation data. It's my first java course ever I want easy solutions.

asherbret
  • 5,439
  • 4
  • 38
  • 58
user3514461
  • 9
  • 1
  • 5

1 Answers1

0

The most simple solution would be to use the getScaledInstance method and use the resulting image to create the ImageIcon:

Image scaledImage = image.getScaledInstance(200 * nRows, 200 * nCols, Image.SCALE_DEFAULT);
ImageIcon ii = new ImageIcon(scaledImage);
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49