Question: Please help me out with programming. import images.ImageUtils; public class TileFactory { public final int tileWidth; public final int tileHeight; // TODO: you will NOT
Please help me out with programming.
import images.ImageUtils;
public class TileFactory {
public final int tileWidth;
public final int tileHeight;
// TODO: you will NOT be keeping this array in your final code;
// see assignment description for details
private final int[] hues;
/**
*
* @param colors the palette of RGB colors for this TileFactory
* @param tileWidth width (in pixels) for each tile
* @param tileHeight height (in pixels) for each tile
*/
public TileFactory(int[] colors, int tileWidth, int tileHeight) {
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
// TODO: when you replace the int[] hues, be sure to replace this code, as well
hues = new int[colors.length];
for (int i = 0; i < hues.length; i++) {
hues[i] = Math.round(ImageUtils.hue(colors[i]));
}
}
/**
* Returns the distance between two hues on the circle [0,256).
*
* @param hue1
* @param hue2
* @return the distance between two hues.
*/
static int hueDistance(int hue1, int hue2) {
// TODO
return 0;
}
/**
* Returns the closest hue from the fixed palette this TileFactory contains.
* @param hue
* @return the closest hue from the palette
*/
Integer closestHue(int hue) {
// TODO
return 0;
}
/**
* Adds an image to this TileFactory for later use.
*
* @param image the image to add
*/
public void addImage(BufferedImage image) {
image = ImageUtils.resize(image, tileWidth, tileHeight);
int avgHue = ImageUtils.averageHue(image);
// TODO: add the image to the appropriate place in your map from hues to lists of images
}
/**
* Returns the next tile from the list associated with the hue most closely matching the input hue.
*
* The returned values should cycle through the list. Each time this method is called, the next
* tile in the list will be returned; when the end of the list is reached, the cycle starts over.
*
* @param hue the color to match
* @return a tile matching hue
*/
public BufferedImage getTile(int hue) {
// TODO: return an appropriate image from your map of hues to lists of images;
// see assignment description for details
return null;
}
} ==========================================================
public class ImageUtils {
public static List
List
for (File f: directory.listFiles()) {
BufferedImage image;
try {
image = ImageIO.read(f);
} catch (IOException e) {
continue;
}
if (image == null) continue;
list.add(image);
}
return list;
}
public static int hue(int rgb) {
final ColorModel colorModel = ColorModel.getRGBdefault();
final double DEGREES_TO_RADIANS = (Math.PI / 180.0);
final int R = colorModel.getRed(rgb);
final int G = colorModel.getGreen(rgb);
final int B = colorModel.getBlue(rgb);
final double alpha = (2.0 * R - G - B) / 2.0 / 255.0;
final double beta = (G - B) / (2.0 / Math.sqrt(3.0)) / 255.0;
double hue = Math.atan2(beta, alpha) / DEGREES_TO_RADIANS;
if (hue < 0) {
hue += 360.0;
}
hue = hue * 256.0 / 360.0;
return (int)Math.round(hue);
}
public static int averageHue(BufferedImage image) {
int numPixels = 0;
double totalHue = 0.0;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
final int pixel = image.getRGB(x, y);
totalHue += hue(pixel);
numPixels++;
}
}
return (int)Math.round(totalHue / numPixels);
}
public static BufferedImage resize(BufferedImage image, int width, int height) {
Image scaledImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage targetImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = targetImage.createGraphics();
g2d.drawImage(scaledImage, 0, 0, null);
g2d.dispose();
return targetImage;
}
public static int[] getPixels(BufferedImage image) {
int[] array = new int[image.getWidth() * image.getHeight()];
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
array[y * image.getWidth() + x] = image.getRGB(x, y);
}
}
return array;
}
}
Please translate behaviors into code
Thank you so much.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
