/** This is a class to store information about a picture object. @author Joanne Selinski */ public class Picture { /** Default resolution. */ public static final int PPI = 64; /** Pixels per inch. */ private static int resolution = PPI; /** The width of a picture, in pixels. */ private int width; /** The height of a picture, in pixels. */ private int height; /** A description of the picture. */ private String subject; /** Create a picture object. @param w the width, in pixels @param h the height, in pixels @param s the subject (description) of the picture */ public Picture(int w, int h, String s) { this.width = w; this.height = h; this.subject = s; } /** Create a picture object. @param s contains the subject, width, height in this format Subject Matter [width,height] */ public Picture(String s) { int left = s.indexOf('['); int right = s.indexOf(']'); int comma = s.indexOf(','); this.subject = s.substring(0, left).trim(); this.width = Integer.parseInt(s.substring(left + 1, comma).trim()); this.height = Integer.parseInt(s.substring(comma + 1, right).trim()); } /** Set the pixels/inch for the class. @param r the new value */ public static void setResolution(int r) { resolution = r; } /** Get the area of the picture, in pixels. @return the area */ public int area() { return this.width * this.height; } /** Get the perimeter of the picture, in inches based on the current resolution. @return the perimeter */ public double perimeter() { return 2 * (this.width + this.height) / (double) resolution; } /** Make a string containing all the picture data. @return the string */ public String toString() { return this.subject + " [" + this.width + "," + this.height + "]"; } /** Determine if the picture is in landscape mode. @return true if so, false otherwise */ public boolean isLandscape() { return this.width >= this.height; } /** Determine if the picture is in portrait mode. @return true if so, false otherwise */ public boolean isPortrait() { return this.height >= this.width; } /** Compare pictures based on their perimeters. @param other the picture to compare this to @return negative if this < other, 0 if =, positive if this > other */ public int compareTo(Picture other) { double d = this.perimeter() - other.perimeter(); if (d < 0) { return -1; } else if (d > 0) { return 1; } return 0; } }