Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.61 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.font.*;
  3. import java.awt.geom.*;
  4. import java.awt.image.BufferedImage;
  5. import java.text.*;
  6. import java.util.*;
  7. import java.util.List; // resolves problem with java.awt.List and java.util.List
  8.  
  9. /**
  10. * A class that represents a picture. This class inherits from
  11. * SimplePicture and allows the student to add functionality to
  12. * the Picture class.
  13. *
  14. * @author Barbara Ericson ericson@cc.gatech.edu
  15. */
  16. public class Picture extends SimplePicture
  17. {
  18. ///////////////////// constructors //////////////////////////////////
  19.  
  20. /**
  21. * Constructor that takes no arguments
  22. */
  23. public Picture ()
  24. {
  25. /* not needed but use it to show students the implicit call to super()
  26. * child constructors always call a parent constructor
  27. */
  28. super();
  29. }
  30.  
  31. /**
  32. * Constructor that takes a file name and creates the picture
  33. * @param fileName the name of the file to create the picture from
  34. */
  35. public Picture(String fileName)
  36. {
  37. // let the parent class handle this fileName
  38. super(fileName);
  39. }
  40.  
  41. /**
  42. * Constructor that takes the width and height
  43. * @param height the height of the desired picture
  44. * @param width the width of the desired picture
  45. */
  46. public Picture(int height, int width)
  47. {
  48. // let the parent class handle this width and height
  49. super(width,height);
  50. }
  51.  
  52. /**
  53. * Constructor that takes a picture and creates a
  54. * copy of that picture
  55. * @param copyPicture the picture to copy
  56. */
  57. public Picture(Picture copyPicture)
  58. {
  59. // let the parent class do the copy
  60. super(copyPicture);
  61. }
  62.  
  63. /**
  64. * Constructor that takes a buffered image
  65. * @param image the buffered image to use
  66. */
  67. public Picture(BufferedImage image)
  68. {
  69. super(image);
  70. }
  71.  
  72. ////////////////////// methods ///////////////////////////////////////
  73.  
  74. /**
  75. * Method to return a string with information about this picture.
  76. * @return a string with information about the picture such as fileName,
  77. * height and width.
  78. */
  79. public String toString()
  80. {
  81. String output = "Picture, filename " + getFileName() +
  82. " height " + getHeight()
  83. + " width " + getWidth();
  84. return output;
  85.  
  86. }
  87. /** Method to set the blue to 0 */
  88. public void zeroBlue()
  89. {
  90. Pixel[][] pixels = this.getPixels2D();
  91. for (Pixel[] rowArray : pixels)
  92. {
  93. for (Pixel pixelObj : rowArray)
  94. {
  95. pixelObj.setBlue(0);
  96. }
  97. }
  98. }
  99. public void keepOnlyBlue()
  100. {
  101. Pixel[][] pixels = this.getPixels2D();
  102. for(Pixel[] rowArray : pixels)
  103. {
  104. for(Pixel pixelObj : rowArray)
  105. {
  106. pixelObj.setRed(0);
  107. pixelObj.setGreen(0);
  108. }
  109. }
  110. }
  111. public void negate()
  112. {
  113. Pixel[][] pixels = this.getPixels2D();
  114. for(Pixel[] rowArray : pixels)
  115. {
  116. for(Pixel pixelObj : rowArray)
  117. {
  118. int red = pixelObj.getRed();
  119. int blue = pixelObj.getBlue();
  120. int green = pixelObj.getGreen();
  121. pixelObj.setRed(red-255);
  122. pixelObj.setGreen(green-255);
  123. pixelObj.setBlue(blue-255);
  124. }
  125. }
  126. }
  127. public void grayscale()
  128. {
  129. Pixel[][] pixels = this.getPixels2D();
  130. for(Pixel[] rowArray : pixels)
  131. {
  132. for(Pixel pixelObj : rowArray)
  133. {
  134. int red = pixelObj.getRed();
  135. int blue = pixelObj.getBlue();
  136. int green = pixelObj.getGreen();
  137. pixelObj.setRed((red+blue+green)/3);
  138. pixelObj.setBlue((red+blue+green)/3);
  139. pixelObj.setGreen((red+blue+green)/3);
  140. }
  141. }
  142. }
  143. public void fixUnderWater()
  144. {
  145. Pixel[][] pixels = this.getPixels2D();
  146. for(Pixel[] rowArray : pixels)
  147. {
  148. for(Pixel pixelObj : rowArray)
  149. {
  150. int red = pixelObj.getRed();
  151. int blue = pixelObj.getBlue();
  152. int green = pixelObj.getGreen();
  153. pixelObj.setRed((red*4));
  154. pixelObj.setGreen((green*2));
  155. pixelObj.setBlue(blue*2);
  156. }
  157. }
  158. }
  159. //right to left
  160. public void mirrorVerticalRightToLeft()
  161. {
  162. Pixel[][] pixels = this.getPixels2D();
  163. Pixel topPixel = null;
  164. Pixel bottPixel = null;
  165. int height = pixels[0].length;
  166. for (int row = 0; row < pixels.length; row++)
  167. {
  168. for (int col = 0; col < height / 2; col++)
  169. {
  170. topPixel = pixels[row][col];
  171. bottPixel = pixels[height - 1 - row][col];
  172. bottPixel.setColor(topPixel.getColor());
  173. }
  174. }
  175. }
  176. public void primaries()
  177. {
  178. Pixel[][] pixels = this.getPixels2D();
  179. int n = 2;
  180. for(Pixel[] rowArray : pixels)
  181. {
  182. for(Pixel pixelObj : rowArray)
  183. {
  184. int blue = pixelObj.getBlue();
  185. int green = pixelObj.getGreen();
  186. int red = pixelObj.getRed();
  187. if(blue < 50 && red < 50 && green < 50)
  188. {
  189. blue = 0;
  190. red = 0;
  191. green = 0;
  192. }
  193. else if(blue > green && blue > red)
  194. {
  195. blue *= n;
  196. red = 0;
  197. green = 0;
  198. }
  199. else if (green > blue && green > red)
  200. {
  201. blue = 0;
  202. red = 0;
  203. green *= n;
  204. }
  205. else
  206. {
  207. blue = 0;
  208. red *= n;
  209. green = 0;
  210. }
  211. pixelObj.setBlue(blue);
  212. pixelObj.setRed(red);
  213. pixelObj.setGreen(green);
  214. }
  215. }
  216. }
  217. /** Method that mirrors the picture around a
  218. * vertical mirror in the center of the picture
  219. * from left to right */
  220. public void mirrorVertical()
  221. {
  222. Pixel[][] pixels = this.getPixels2D();
  223. Pixel leftPixel = null;
  224. Pixel rightPixel = null;
  225. int width = pixels[0].length;
  226. for (int row = 0; row < pixels.length; row++)
  227. {
  228. for (int col = 0; col < width / 2; col++)
  229. {
  230. leftPixel = pixels[row][col];
  231. rightPixel = pixels[row][width - 1 - col];
  232. rightPixel.setColor(leftPixel.getColor());
  233. }
  234. }
  235. }
  236.  
  237. /** Mirror just part of a picture of a temple */
  238. public void mirrorTemple()
  239. {
  240. int mirrorPoint = 276;
  241. Pixel leftPixel = null;
  242. Pixel rightPixel = null;
  243. int count = 0;
  244. Pixel[][] pixels = this.getPixels2D();
  245.  
  246. // loop through the rows
  247. for (int row = 27; row < 97; row++)
  248. {
  249. // loop from 13 to just before the mirror point
  250. for (int col = 13; col < mirrorPoint; col++)
  251. {
  252.  
  253. leftPixel = pixels[row][col];
  254. rightPixel = pixels[row]
  255. [mirrorPoint - col + mirrorPoint];
  256. rightPixel.setColor(leftPixel.getColor());
  257. }
  258. }
  259. }
  260.  
  261. /** copy from the passed fromPic to the
  262. * specified startRow and startCol in the
  263. * current picture
  264. * @param fromPic the picture to copy from
  265. * @param startRow the start row to copy to
  266. * @param startCol the start col to copy to
  267. */
  268. public void copy(Picture fromPic,
  269. int startRow, int startCol)
  270. {
  271. Pixel fromPixel = null;
  272. Pixel toPixel = null;
  273. Pixel[][] toPixels = this.getPixels2D();
  274. Pixel[][] fromPixels = fromPic.getPixels2D();
  275. for (int fromRow = 0, toRow = startRow;
  276. fromRow < fromPixels.length &&
  277. toRow < toPixels.length;
  278. fromRow++, toRow++)
  279. {
  280. for (int fromCol = 0, toCol = startCol;
  281. fromCol < fromPixels[0].length &&
  282. toCol < toPixels[0].length;
  283. fromCol++, toCol++)
  284. {
  285. fromPixel = fromPixels[fromRow][fromCol];
  286. toPixel = toPixels[toRow][toCol];
  287. toPixel.setColor(fromPixel.getColor());
  288. }
  289. }
  290. }
  291.  
  292. /** Method to create a collage of several pictures */
  293. public void createCollage()
  294. {
  295. Picture flower1 = new Picture("flower1.jpg");
  296. Picture flower2 = new Picture("flower2.jpg");
  297. this.copy(flower1,0,0);
  298. this.copy(flower2,100,0);
  299. this.copy(flower1,200,0);
  300. Picture flowerNoBlue = new Picture(flower2);
  301. flowerNoBlue.zeroBlue();
  302. this.copy(flowerNoBlue,300,0);
  303. this.copy(flower1,400,0);
  304. this.copy(flower2,500,0);
  305. this.mirrorVertical();
  306. this.write("collage.jpg");
  307. }
  308.  
  309. /** Blur */
  310. public void blur(int val)
  311. {
  312. Picture from = this;
  313. Picture to = new Picture(from.getHeight(), from.getWidth());
  314. Pixel[][] fromPixels = from.getPixels2D();
  315. Pixel[][] toPixels = to.getPixels2D();
  316. for(int row = 0; row < fromPixels.length; row++)
  317. {
  318. for(int col = 0; col < fromPixels[0].length; col++)
  319. {
  320. int sumR = 0, sumB = 0, sumG = 0;
  321. int count = 0;
  322. for(int i = -val; i <= val; i++)
  323. {
  324. for(int j = -val; j <= val; j++)
  325. {
  326. if(( (row + i) >= 0 && (row + i) < fromPixels.length ) && ((col + j) >= 0 && (col + j) < fromPixels[0].length ))
  327. {
  328. sumR += fromPixels[row + i][col + j].getRed();
  329. sumG += fromPixels[row + i][col + j].getGreen();
  330. sumB += fromPixels[row + i][col + j].getBlue();
  331. count++;
  332. }
  333. }
  334. }
  335. toPixels[row][col].updatePicture(0, sumR / count, sumG / count, sumB / count);
  336. }
  337. }
  338. to.write("blur.jpg");
  339. }
  340.  
  341. /** Method to show large changes in color
  342. * @param edgeDist the distance for finding edges
  343. */
  344. public void edgeDetection(int edgeDist)
  345. {
  346. Pixel leftPixel = null;
  347. Pixel rightPixel = null;
  348. Pixel[][] pixels = this.getPixels2D();
  349. Color rightColor = null;
  350. for (int row = 0; row < pixels.length; row++)
  351. {
  352. for (int col = 0;
  353. col < pixels[0].length-1; col++)
  354. {
  355. leftPixel = pixels[row][col];
  356. rightPixel = pixels[row][col+1];
  357. rightColor = rightPixel.getColor();
  358. if (leftPixel.colorDistance(rightColor) >
  359. edgeDist)
  360. leftPixel.setColor(Color.BLACK);
  361. else
  362. leftPixel.setColor(Color.WHITE);
  363. }
  364. }
  365. }
  366.  
  367. public void myEdgeDetection(int edgeDist)
  368. {
  369. Pixel left = null;
  370. Pixel right = null;
  371. Pixel[][] pixels = this.getPixels2D();
  372. Picture to = new Picture(this.getHeight(), this.getWidth());
  373. Pixel[][] toPixels = to.getPixels2D();
  374. for (int row = 0; row < pixels.length; row++)
  375. {
  376. for (int col = 0; col < pixels[0].length-1; col++)
  377. {
  378. left = pixels[row][col];
  379. right = pixels[row][col+1];
  380. if(Math.abs(left.getRed() - right.getBlue()) > edgeDist
  381. || Math.abs(left.getGreen() - right.getBlue()) > edgeDist)
  382. toPixels[row][col].setColor(Color.BLACK);
  383. else
  384. toPixels[row][col].setColor(Color.WHITE);
  385. }
  386. }
  387. for(int row = 0; row < toPixels.length; row++)
  388. {
  389. for(int col = 0; col < toPixels[0].length; col++)
  390. {
  391.  
  392. }
  393. }
  394. to.write("edge.jpg");
  395. }
  396.  
  397. public void morph(int i, int total)
  398. {
  399. Picture from = new Picture("beach.jpg");
  400. Picture to = new Picture("seagull.jpg");
  401. Picture middle = new Picture(480, 640);
  402. Pixel[][] fromPixels = from.getPixels2D();
  403. Pixel[][] toPixels = to.getPixels2D();
  404. Pixel[][] interpolate = middle.getPixels2D();
  405. for(int row = 0; row < fromPixels.length; row++)
  406. {
  407. for(int col = 0; col < fromPixels[0].length; col++)
  408. {
  409. int fr = fromPixels[row][col].getRed();
  410. int fb = fromPixels[row][col].getBlue();
  411. int fg = fromPixels[row][col].getGreen();
  412. int tr = toPixels[row][col].getRed();
  413. int tb = toPixels[row][col].getBlue();
  414. int tg = toPixels[row][col].getGreen();
  415. interpolate[row][col].setRed(fr + i * (tr - fr) / total);
  416. interpolate[row][col].setBlue(fb + i * (tb - fb) / total);
  417. interpolate[row][col].setGreen(fg + i * (tg - fg) / total);
  418. }
  419. }
  420. middle.write("test" + i + ".jpg");
  421. }
  422.  
  423. /* Main method for testing - each class in Java can have a main
  424. * method
  425. */
  426. public static void main(String[] args)
  427. {
  428. Picture beach = new Picture("beach.jpg");
  429. beach.explore();
  430. beach.mirrorVerticalRightToLeft();
  431. beach.explore();
  432. }
  433.  
  434. } // this } is the end of class Picture, put all new methods before this
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement