Implementation of glass filter for images This is exercise 3.1.39. from the book Computer Science An Interdisciplinary Approach by Sedgewick &amp; Wayne:

Write a program that takes the name of an image file as a command-line
argument and applies a glass filter: set each pixel p to the color of
a random neighboring pixel (whose pixel coordinates both differ from
p’s coordinates by at most 5).

Here is my program:
import java.awt.Color;

public class test 
{
    public static int[] chooseRandomNeighbor(int i, int j)
    {
        int[] chosenNeighbor = new int[2];
        double r = Math.random();
        if      (r &lt; 1.0/8.0)
        {
            chosenNeighbor[0] = i-1;
            chosenNeighbor[1] = j-1;
        }
        else if (r &lt; 2.0/8.0)
        {
            chosenNeighbor[0] = i-1;
            chosenNeighbor[1] = j; 
        }
        else if (r &lt; 3.0/8.0)
        {
            chosenNeighbor[0] = i-1;
            chosenNeighbor[1] = j+1; 
        }
        else if (r &lt; 4.0/8.0)
        {
            chosenNeighbor[0] = i;
            chosenNeighbor[1] = j+1; 
        }
        else if (r &lt; 5.0/8.0)
        {
            chosenNeighbor[0] = i+1;
            chosenNeighbor[1] = j+1; 
        }
        else if (r &lt; 6.0/8.0)
        {
            chosenNeighbor[0] = i+1;
            chosenNeighbor[1] = j; 
        }
        else if (r &lt; 7.0/8.0)
        {
            chosenNeighbor[0] = i+1;
            chosenNeighbor[1] = j-1; 
        }
        else if (r &lt; 8.0/8.0)
        {
            chosenNeighbor[0] = i;
            chosenNeighbor[1] = j-1; 
        }
        return chosenNeighbor;
    }
    public static Picture filter(Picture picture)
    {
        int width = picture.width();
        int height = picture.height();
        Picture filteredPicture = new Picture(width,height);
        // the following four for-loops make the dead frame
        for (int row = 0; row &lt; height; row++)
        {
            Color color = picture.get(0,row);
            filteredPicture.set(0,row,color);
        }
        for (int row = 0; row &lt; height; row++)
        {
            Color color = picture.get(width-1,row);
            filteredPicture.set(width-1,row,color);
        }
        for (int col = 0; col &lt; width; col++)
        {
            Color color = picture.get(col,0);
            filteredPicture.set(col,0,color);
        }
        for (int col = 0; col &lt; width; col++)
        {
            Color color = picture.get(col,height-1);
            filteredPicture.set(col,height-1,color);
        }
        // the real filtering takes place here
        for (int col = 1; col &lt; width-1; col++)
        {
            for (int row = 1; row &lt; height-1; row++)
            {
                int[] chosenNeighbor = chooseRandomNeighbor(row,col);
                Color color = picture.get(chosenNeighbor[1],chosenNeighbor[0]);
                filteredPicture.set(col,row,color);
            }
        }
        return filteredPicture;
    }
    public static void main(String[] args)
    {
        Picture picture = new Picture(args[0]);
        Picture filteredPicture = filter(filter(filter(filter(picture))));
        filteredPicture.show();
    }
}

Picture is a simple API written by the authors of the book. I checked my program and it works. Here is one instance of it:
Input (picture of James McAvoy taken from Wikipedia who played in the movie Glass):

Output:

Is there any way that I can improve my program?
Thanks for your attention.
Interesting exercise and nice implementation, few suggestions on my side:

The method chooseRandomNeighbor is only used by filter, so it can be set to private
The name filter is too general for a method, a better name might be applyGlassFilter
In Java the name of a class starts with a capital letter

Optimization

random neighboring pixel (whose pixel coordinates both differ from p’s
coordinates by at most 5).

The method chooseRandomNeighbor picks an adjacent random neighbor (which differ by at most 1) and then gets called 4 times. That implies creating 4 full images in memory. Wouldn't be better to directly pick a neighbor with maximum distance 5?
To do that the method chooseRandomNeighbor needs to accept the width or height:
int neighborColIndex = chooseRandomNeighbor(col,width);
int neighborRowIndex = chooseRandomNeighbor(row,height);

And this is chooseRandomNeighbor refactored:
private static int chooseRandomNeighbor(int index, int max) {
    // Random delta between -5 and +5
    int randomDelta = (int) ((Math.random() * (10)) - 5);
    // Add delta to index without overflowing the limit
    int neighborIndex = (index + randomDelta) % max;
    // If index is negative return 0
    return neighborIndex &lt; 0 ? 0 : neighborIndex;
}

Now the method filter can be called only once and only the new image will be created in memory.
Side note: you are getting better and better, keep up the good work!
Code Refactored
public class Test {
    
    public static Picture applyGlassFilter(Picture inputPicture) {
        int width = inputPicture.width();
        int height = inputPicture.height();
        Picture outputPicture = new Picture(width, height);
        for (int col = 0; col &lt; width; col++) {
            for (int row = 0; row &lt; height; row++) {
                int neighborColIndex = chooseRandomNeighbor(col,width);
                int neighborRowIndex = chooseRandomNeighbor(row,height);
                Color c = inputPicture.get(neighborColIndex,neighborRowIndex);
                outputPicture.set(col, row, c);
            }
        }
        return outputPicture;
    }
    
    private static int chooseRandomNeighbor(int index, int max) {
        // Random delta between -5 and +5
        int randomDelta = (int) ((Math.random() * (10)) - 5);
        // Add delta to index without overflowing the limit
        int neighborIndex = (index + randomDelta) % max;
        // If index is negative return 0
        return neighborIndex &lt; 0 ? 0 : neighborIndex;
    }
    
    public static void main(String[] args) {
        Picture inputPicture = new Picture(args[0]);
        Picture outputPicture = applyGlassFilter(inputPicture);
        outputPicture.show();
    }
}