Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

@SuppressWarnings(\"serial\") public class MosaicDriver extends JFrame { private

ID: 3602257 • Letter: #

Question

@SuppressWarnings("serial")

public class MosaicDriver extends JFrame {

   private static final int WIDTH = 10;

   private static final int HEIGHT = 10;

   private static final String RESOURCE_DIR = "test" + File.separator + "resources" + File.separator;

  

   public MosaicDriver() throws IOException {

       add(new ImagePanel(makeMosaic()));

       pack();

       setTitle("Image Mosaic");

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       setLocationRelativeTo(null);

   }

  

   public static BufferedImage makeMosaic() throws IOException {

       // first, choose the set of colors (the "palette") for the mosaic

       int[] palette = {0xFF0000, 0x00FF00, 0x0000FF};

      

       // then instantiate a TileFactor with this palette

       TileFactory tileFactory = new TileFactory(palette, WIDTH, HEIGHT);

      

       // load the test images

       for (String color: new String[] {"red", "green", "blue"}) {

           for (BufferedImage image: ImageUtils.loadFromDirectory(new File(RESOURCE_DIR + color))) {              

               tileFactory.addImage(image);

           }

       }

      

       // load the target image

       File file = new File(RESOURCE_DIR + "nature.jpg");

       BufferedImage image = ImageIO.read(file);

       image = ImageUtils.resize(image, 1152, 720);

      

       // create a Mosaic with this target image and the previously initialized TileFactory

       Mosaic mosaic = new Mosaic(image, tileFactory);

       BufferedImage imageMosaic = mosaic.buildMosaic();

       return imageMosaic;

   }

  

   public static void main(String[] args) throws IOException {

       EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

MosaicDriver m;

               try {

                   m = new MosaicDriver();

               } catch (IOException e) {

                   e.printStackTrace();

                   return;

               }

m.setVisible(true);

}

});

   }

}

==================================================================

import java.awt.image.BufferedImage;

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;

}

}

Shuold change the TileFactory to instead use a Map, declare your map and initialize its key-value pairs appropriately.
You’ll need to keep the use of ImageUtils.hue() to convert the input color (an integer) into a hue (also an integer).
Use the 12 hour-clock analogy to figure out the right mathematical expression in addImage().
Translate written descriptions of behavior into code. Thank you so mcuh.

Explanation / Answer

public class MosaicDriver extends JFrame {

   private static final int WIDTH = 10;

   private static final int HEIGHT = 10;

   private static final String RESOURCE_DIR = "test" + File.separator + "resources" + File.separator;

  

   public MosaicDriver() throws IOException {

       add(new ImagePanel(makeMosaic()));

       pack();

       setTitle("Image Mosaic");

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       setLocationRelativeTo(null);

   }

  

   public static BufferedImage makeMosaic() throws IOException {

       // first, choose the set of colors (the "palette") for the mosaic

       int[] palette = {0xFF0000, 0x00FF00, 0x0000FF};

      

       // then instantiate a TileFactor with this palette

       TileFactory tileFactory = new TileFactory(palette, WIDTH, HEIGHT);

      

       // load the test images

       for (String color: new String[] {"red", "green", "blue"}) {

           for (BufferedImage image: ImageUtils.loadFromDirectory(new File(RESOURCE_DIR + color))) {              

               tileFactory.addImage(image);

           }

       }

      

       // load the target image

       File file = new File(RESOURCE_DIR + "nature.jpg");

       BufferedImage image = ImageIO.read(file);

       image = ImageUtils.resize(image, 1152, 720);

      

       // create a Mosaic with this target image and the previously initialized TileFactory

       Mosaic mosaic = new Mosaic(image, tileFactory);

       BufferedImage imageMosaic = mosaic.buildMosaic();

       return imageMosaic;

   }

  

   public static void main(String[] args) throws IOException {

       EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

MosaicDriver m;

               try {

                   m = new MosaicDriver();

               } catch (IOException e) {

                   e.printStackTrace();

                   return;

               }

m.setVisible(true);

}

});

   }

import java.awt.image.BufferedImage;

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;

for (int i = 0; i < hues.length; i++) {

hues[i] = Math.round(ImageUtils.hue(colors[i]));

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote