Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

---------------------------------------------------------------------------------------------------------------- package mosaic; import images.ImagePanel; import images.ImageUtils; import images.Mosaic; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @SuppressWarnings(serial) public class MosaicDriver

image text in transcribed

----------------------------------------------------------------------------------------------------------------

package mosaic;

import images.ImagePanel;

import images.ImageUtils;

import images.Mosaic;

import javax.imageio.ImageIO;

import javax.swing.*;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

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

}

});

}

}

------------------------------------------------------------------------------------------------------

package mosaic;

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[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;

}

}

--------------------------------------------------------------------------------------

Running the MosaicDriver onre you've finished should turn this: into this Running the MosaicDriver onre you've finished should turn this: into this

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Case Studies In Business Data Bases

Authors: James Bradley

1st Edition

0030141346, 978-0030141348

More Books

Students also viewed these Databases questions

Question

4. Who should be invited to attend?

Answered: 1 week ago

Question

7. How will you encourage her to report back on the findings?

Answered: 1 week ago

Question

Were the decisions based on appropriate facts?

Answered: 1 week ago