Window Frame

Solve It

In this example we are going to take the idea of making a frame for an image a step further. Instead of just drawing a frame around the outside of the image, we will add a window pane effect by drawing a horizontal line through the middle of the image and three vertical lines that divide the image into 8 equally sized panes. The outside frame will be thicker (15 pixels thick) and the inner frames will be a bit thinner (9 pixels).

To do this, think about dividing the image into regions. One region of interest is the top 15 rows of pixels, which should be colored the frame color. We know a pixel is in that region if it has an x value of less than 15. We can similarly define conditions that tell us if a pixel is in the bottom horizontal strip, leftmost vertical strip, and rightmost vertical strip. If the pixel is in any of these four regions it should get red, green, and blue values of our frame color.

For the middle frame line, we need to calculate 1/2 the height for the middle horizontal line, and 1/4, 1/2, and 3/4 of the width for the vertical lines. We can use if statements similar to the ones we wrote for the outer frame to check if a pixel has an x value within 5 pixels of the vertical pane lines or a y value within 5 pixels of the horizontal pane line. If so, we change the red, green, and blue values to the frame color. If the pixel does not fall on a frame region we copy its original values.

Code It

// draw a window frame over the image var image = new SimpleImage("islandSmall.jpg"); var w = image.getWidth(); var h = image.getHeight(); var output = new SimpleImage(w,h); for (var pixel of image.values()) { // put a thick border around it var x = pixel.getX(); var y = pixel.getY(); var pout = output.getPixel(x,y); // calculate where bars go oneFourthX = Math.floor(w/4); oneHalfX = Math.floor(w/2); threeFourthsX = Math.floor(w/4 * 3) oneHalfy = Math.floor(h/2); if (pixel.getX()<15 || pixel.getX() > w-15 || pixel.getY()<15 || pixel.getY() > h-15) { // RGB color below is a yellow-gold color pout.setRed(227); pout.setGreen(212); pout.setBlue(116); } // add three thinner vertical bars for a window pane else { if (Math.abs(oneFourthX - pixel.getX())< 5 || Math.abs(oneHalfX - pixel.getX())< 5 || Math.abs(threeFourthsX - pixel.getX())< 5) { // add thinner vertical bars for a window pane pout.setRed(227); pout.setGreen(212); pout.setBlue(116); } // add one thin horizontal bar else { if (Math.abs(oneHalfy - pixel.getY()) < 5) { pout.setRed(227); pout.setGreen(212); pout.setBlue(116); } else { // just use the pixel from the original image pout.setRed(pixel.getRed()); pout.setGreen(pixel.getGreen()); pout.setBlue(pixel.getBlue()); } } } } print(output);

See It

 
 

Available Images


islandSmall.jpg
[320x239]

Drop your images onto the area above to make it available within your code editor on this page. Note: your images will not be uploaded anywhere, they will stay on your computer.