Guest User

Untitled

a guest
Dec 10th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.30 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import javax.swing.*;
  5. import javax.swing.border.*;
  6.  
  7. import java.io.File;
  8.  
  9. import java.util.List;
  10. import java.util.ArrayList;
  11. import java.util.Iterator;
  12.  
  13. /**
  14. * Ujian Akhir Semester
  15. * Kelas PBO B
  16. * @author Hendra Ramadani (05111740000055)
  17. * 10 December 2018
  18. */
  19. public class ImageViewer
  20. {
  21. // static fields:
  22. private static final String VERSION = "Version 3.1";
  23. private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
  24.  
  25. // fields:
  26. private JFrame frame;
  27. private ImagePanel imagePanel;
  28. private JLabel filenameLabel;
  29. private JLabel statusLabel;
  30. private JButton smallerButton;
  31. private JButton largerButton;
  32. private JButton cropButton;
  33. private JButton textButton;
  34. private JButton shapeButton;
  35. private JButton rotateleftButton;
  36. private JButton rotaterightButton;
  37. private OFImage currentImage;
  38.  
  39. private List<Filter> filters;
  40.  
  41. /**
  42. * Create an ImageViewer and display its GUI on screen.
  43. */
  44. public ImageViewer()
  45. {
  46. currentImage = null;
  47. filters = createFilters();
  48. makeFrame();
  49. }
  50.  
  51.  
  52. // ---- implementation of menu functions ----
  53.  
  54. /**
  55. * Open function: open a file chooser to select a new image file,
  56. * and then display the chosen image.
  57. */
  58. private void openFile()
  59. {
  60. int returnVal = fileChooser.showOpenDialog(frame);
  61.  
  62. if(returnVal != JFileChooser.APPROVE_OPTION) {
  63. return; // cancelled
  64. }
  65. File selectedFile = fileChooser.getSelectedFile();
  66. currentImage = ImageFileManager.loadImage(selectedFile);
  67.  
  68. if(currentImage == null) { // image file was not a valid image
  69. JOptionPane.showMessageDialog(frame,
  70. "The file was not in a recognized image file format.",
  71. "Image Load Error",
  72. JOptionPane.ERROR_MESSAGE);
  73. return;
  74. }
  75.  
  76. imagePanel.setImage(currentImage);
  77. setButtonsEnabled(true);
  78. showFilename(selectedFile.getPath());
  79. showStatus("File loaded.");
  80. frame.pack();
  81. }
  82.  
  83. /**
  84. * Close function: close the current image.
  85. */
  86. private void close()
  87. {
  88. currentImage = null;
  89. imagePanel.clearImage();
  90. showFilename(null);
  91. setButtonsEnabled(false);
  92. }
  93.  
  94. /**
  95. * Save As function: save the current image to a file.
  96. */
  97. private void saveAs()
  98. {
  99. if(currentImage != null) {
  100. int returnVal = fileChooser.showSaveDialog(frame);
  101.  
  102. if(returnVal != JFileChooser.APPROVE_OPTION) {
  103. return; // cancelled
  104. }
  105. File selectedFile = fileChooser.getSelectedFile();
  106. ImageFileManager.saveImage(currentImage, selectedFile);
  107.  
  108. showFilename(selectedFile.getPath());
  109. }
  110. }
  111.  
  112. /**
  113. * Quit function: quit the application.
  114. */
  115. private void quit()
  116. {
  117. System.exit(0);
  118. }
  119.  
  120. /**
  121. * Apply a given filter to the current image.
  122. *
  123. * @param filter The filter object to be applied.
  124. */
  125. private void applyFilter(Filter filter)
  126. {
  127. if(currentImage != null) {
  128. filter.apply(currentImage);
  129. frame.repaint();
  130. showStatus("Applied: " + filter.getName());
  131. }
  132. else {
  133. showStatus("No image loaded.");
  134. }
  135. }
  136.  
  137. /**
  138. * 'About' function: show the 'about' box.
  139. */
  140. private void showAbout()
  141. {
  142. JOptionPane.showMessageDialog(frame,
  143. "ImageViewer\n" + VERSION,
  144. "About ImageViewer",
  145. JOptionPane.INFORMATION_MESSAGE);
  146. }
  147.  
  148. /**
  149. * Make the current picture larger.
  150. */
  151. private void makeLarger()
  152. {
  153. if(currentImage != null) {
  154. // create new image with double size
  155. int width = currentImage.getWidth();
  156. int height = currentImage.getHeight();
  157. OFImage newImage = new OFImage(width * 2, height * 2);
  158.  
  159. // copy pixel data into new image
  160. for(int y = 0; y < height; y++) {
  161. for(int x = 0; x < width; x++) {
  162. Color col = currentImage.getPixel(x, y);
  163. newImage.setPixel(x * 2, y * 2, col);
  164. newImage.setPixel(x * 2 + 1, y * 2, col);
  165. newImage.setPixel(x * 2, y * 2 + 1, col);
  166. newImage.setPixel(x * 2+1, y * 2 + 1, col);
  167. }
  168. }
  169.  
  170. currentImage = newImage;
  171. imagePanel.setImage(currentImage);
  172. frame.pack();
  173. }
  174. }
  175.  
  176.  
  177. /**
  178. * Make the current picture smaller.
  179. */
  180. private void makeSmaller()
  181. {
  182. if(currentImage != null) {
  183. // create new image with double size
  184. int width = currentImage.getWidth() / 2;
  185. int height = currentImage.getHeight() / 2;
  186. OFImage newImage = new OFImage(width, height);
  187.  
  188. // copy pixel data into new image
  189. for(int y = 0; y < height; y++) {
  190. for(int x = 0; x < width; x++) {
  191. newImage.setPixel(x, y, currentImage.getPixel(x * 2, y * 2));
  192. }
  193. }
  194.  
  195. currentImage = newImage;
  196. imagePanel.setImage(currentImage);
  197. frame.pack();
  198. }
  199. }
  200.  
  201. private void crop()
  202. {
  203. if (currentImage != null)
  204. {
  205. int width = currentImage.getWidth();
  206. int height = currentImage.getWidth();
  207. int xAwal = Integer.parseInt(JOptionPane.showInputDialog("xAwal"));
  208. int yAwal = Integer.parseInt(JOptionPane.showInputDialog("yAwal"));
  209. int xAkhir = Integer.parseInt(JOptionPane.showInputDialog("xAkhir"));
  210. int yAkhir = Integer.parseInt(JOptionPane.showInputDialog("yAkhir"));
  211. OFImage newImage = new OFImage(xAkhir - xAwal, yAkhir - yAwal);
  212.  
  213. for (int y = 0; y < yAkhir - yAwal; y++)
  214. {
  215. for (int x = 0; x < xAkhir - xAwal; x++)
  216. {
  217. newImage.setPixel(x, y, currentImage.getPixel(x + xAwal, y + yAwal));
  218. }
  219. }
  220.  
  221. currentImage = newImage;
  222. imagePanel.setImage(currentImage);
  223. frame.pack();
  224. }
  225. }
  226.  
  227. private void makeText()
  228. {
  229. if(currentImage != null) {
  230. int width = currentImage.getWidth();
  231. int height = currentImage.getHeight();
  232. int xPosition = Integer.parseInt(JOptionPane.showInputDialog("Posisi X"));
  233. int yPosition = Integer.parseInt(JOptionPane.showInputDialog("Posisi Y"));
  234. float fontSize = Float.parseFloat(JOptionPane.showInputDialog("Besar Font"));
  235. String addText = JOptionPane.showInputDialog("Ketik sesuatu..");
  236. OFImage newImage = new OFImage(width, height);
  237.  
  238. // copy pixel data into new image
  239. for(int y = 0; y < height; y++) {
  240. for(int x = 0; x < width; x++) {
  241. Color col = currentImage.getPixel(x, y);
  242. newImage.setPixel(x, y, col);
  243. }
  244. }
  245.  
  246. Graphics g = newImage.getGraphics();
  247. g.setFont(g.getFont().deriveFont(fontSize));
  248. g.drawString(addText, xPosition, yPosition);
  249. g.dispose();
  250.  
  251. currentImage = newImage;
  252. imagePanel.setImage(currentImage);
  253. }
  254. }
  255.  
  256. private void makeShape()
  257. {
  258. if(currentImage != null) {
  259. int width = currentImage.getWidth();
  260. int height = currentImage.getHeight();
  261.  
  262. OFImage newImage = new OFImage(width, height);
  263. int xPosition = Integer.parseInt(JOptionPane.showInputDialog("Posisi X"));
  264. int yPosition = Integer.parseInt(JOptionPane.showInputDialog("Posisi Y"));
  265. int addshape = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Bentuk Shape\n1. Lingkaran\n2. Oval\n3. Persegi\n4.Persegi panjang"));
  266.  
  267. // copy pixel data into new image
  268. for(int y = 0; y < height; y++) {
  269. for(int x = 0; x < width; x++) {
  270. Color col = currentImage.getPixel(x, y);
  271. newImage.setPixel(x, y, col);
  272. }
  273. }
  274.  
  275. Graphics g = newImage.getGraphics();
  276. if(addshape == 1){
  277. int diameter = Integer.parseInt(JOptionPane.showInputDialog("Maukkan Diameter"));
  278. g.drawOval(xPosition, yPosition, diameter, diameter);}
  279. else
  280. if(addshape == 2){
  281. int diameter1 = Integer.parseInt(JOptionPane.showInputDialog("Maukkan Diameter 1"));
  282. int diameter2 = Integer.parseInt(JOptionPane.showInputDialog("Maukkan Diameter 1"));
  283. g.drawRect(xPosition, yPosition, diameter1, diameter2);}
  284. else
  285. if(addshape == 3){
  286. int sisi = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Sisi"));
  287. g.drawRect(xPosition, yPosition, sisi, sisi);}
  288. else
  289. if(addshape == 4){
  290. int panjang = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Panjang"));
  291. int lebar = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Lebar"));
  292. g.drawRect(xPosition, yPosition, panjang, lebar);}
  293.  
  294. currentImage = newImage;
  295. imagePanel.setImage(currentImage);
  296. }
  297. }
  298.  
  299. private void Rotate90left() {
  300. if(currentImage != null) {
  301. // create new image with double size
  302. int width = currentImage.getWidth();
  303. int height = currentImage.getHeight();
  304. OFImage newImage = new OFImage(height, width);
  305.  
  306. //copy pixel data into new image
  307. for(int y = 0; y < height; y++) {
  308. for(int x = 0; x < width; x++) {
  309. Color col = currentImage.getPixel(x, y);
  310. newImage.setPixel(y, width-x-1, col);
  311. }
  312. }
  313.  
  314. currentImage = newImage;
  315. imagePanel.setImage(currentImage);
  316. frame.pack();
  317. }
  318. }
  319.  
  320. private void Rotate90right() {
  321. if(currentImage != null) {
  322. // create new image with double size
  323. int width = currentImage.getWidth();
  324. int height = currentImage.getHeight();
  325. OFImage newImage = new OFImage(height, width);
  326.  
  327. //copy pixel data into new image
  328. for(int y = 0; y < height; y++) {
  329. for(int x = 0; x < width; x++) {
  330. Color col = currentImage.getPixel(x, y);
  331. newImage.setPixel(height-y-1, x, col);
  332. }
  333. }
  334.  
  335. currentImage = newImage;
  336. imagePanel.setImage(currentImage);
  337. frame.pack();
  338. }
  339. }
  340. // ---- support methods ----
  341.  
  342. /**
  343. * Show the file name of the current image in the fils display label.
  344. * 'null' may be used as a parameter if no file is currently loaded.
  345. *
  346. * @param filename The file name to be displayed, or null for 'no file'.
  347. */
  348. private void showFilename(String filename)
  349. {
  350. if(filename == null) {
  351. filenameLabel.setText("No file displayed.");
  352. }
  353. else {
  354. filenameLabel.setText("File: " + filename);
  355. }
  356. }
  357.  
  358.  
  359. /**
  360. * Show a message in the status bar at the bottom of the screen.
  361. * @param text The status message.
  362. */
  363. private void showStatus(String text)
  364. {
  365. statusLabel.setText(text);
  366. }
  367.  
  368.  
  369. /**
  370. * Enable or disable all toolbar buttons.
  371. *
  372. * @param status 'true' to enable the buttons, 'false' to disable.
  373. */
  374. private void setButtonsEnabled(boolean status)
  375. {
  376. smallerButton.setEnabled(status);
  377. largerButton.setEnabled(status);
  378. cropButton.setEnabled(status);
  379. textButton.setEnabled(status);
  380. shapeButton.setEnabled(status);
  381. rotateleftButton.setEnabled(status);
  382. rotaterightButton.setEnabled(status);
  383. }
  384.  
  385.  
  386. /**
  387. * Create a list with all the known filters.
  388. * @return The list of filters.
  389. */
  390. private List<Filter> createFilters()
  391. {
  392. List<Filter> filterList = new ArrayList<Filter>();
  393. filterList.add(new DarkerFilter("Darker"));
  394. filterList.add(new LighterFilter("Lighter"));
  395. filterList.add(new ThresholdFilter("Threshold"));
  396. filterList.add(new InvertFilter("Invert"));
  397. filterList.add(new SolarizeFilter("Solarize"));
  398. filterList.add(new SmoothFilter("Smooth"));
  399. filterList.add(new PixelizeFilter("Pixelize"));
  400. filterList.add(new MirrorFilter("Mirror"));
  401. filterList.add(new GrayScaleFilter("Grayscale"));
  402. filterList.add(new EdgeFilter("Edge Detection"));
  403. filterList.add(new FishEyeFilter("Fish Eye"));
  404.  
  405. return filterList;
  406. }
  407.  
  408. // ---- Swing stuff to build the frame and all its components and menus ----
  409.  
  410. /**
  411. * Create the Swing frame and its content.
  412. */
  413. private void makeFrame()
  414. {
  415. frame = new JFrame("ImageViewer");
  416. JPanel contentPane = (JPanel)frame.getContentPane();
  417. contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
  418.  
  419. makeMenuBar(frame);
  420.  
  421. // Specify the layout manager with nice spacing
  422. contentPane.setLayout(new BorderLayout(6, 6));
  423.  
  424. // Create the image pane in the center
  425. imagePanel = new ImagePanel();
  426. imagePanel.setBorder(new EtchedBorder());
  427. contentPane.add(imagePanel, BorderLayout.CENTER);
  428.  
  429. // Create two labels at top and bottom for the file name and status messages
  430. filenameLabel = new JLabel();
  431. contentPane.add(filenameLabel, BorderLayout.NORTH);
  432.  
  433. statusLabel = new JLabel(VERSION);
  434. contentPane.add(statusLabel, BorderLayout.SOUTH);
  435.  
  436. // Create the toolbar with the buttons
  437. JPanel toolbar = new JPanel();
  438. toolbar.setLayout(new GridLayout(0, 1));
  439.  
  440. smallerButton = new JButton("Smaller");
  441. smallerButton.addActionListener(new ActionListener() {
  442. public void actionPerformed(ActionEvent e) { makeSmaller(); }
  443. });
  444. toolbar.add(smallerButton);
  445.  
  446. largerButton = new JButton("Larger");
  447. largerButton.addActionListener(new ActionListener() {
  448. public void actionPerformed(ActionEvent e) { makeLarger(); }
  449. });
  450. toolbar.add(largerButton);
  451.  
  452. cropButton = new JButton("Crop");
  453. cropButton.addActionListener(new ActionListener() {
  454. public void actionPerformed(ActionEvent e) { crop(); }
  455. });
  456. toolbar.add(cropButton);
  457.  
  458. textButton = new JButton("Add Text");
  459. textButton.addActionListener(new ActionListener() {
  460. public void actionPerformed(ActionEvent e) { makeText();}
  461. });
  462. toolbar.add(textButton);
  463.  
  464. shapeButton = new JButton("Add Shape");
  465. shapeButton.addActionListener(new ActionListener() {
  466. public void actionPerformed(ActionEvent e) { makeShape();}
  467. });
  468. toolbar.add(shapeButton);
  469.  
  470. rotateleftButton = new JButton("Rotate Left");
  471. rotateleftButton.addActionListener(new ActionListener() {
  472. public void actionPerformed(ActionEvent e) { Rotate90left();}
  473. });
  474. toolbar.add(rotateleftButton);
  475.  
  476. rotaterightButton = new JButton("Rotate Right");
  477. rotaterightButton.addActionListener(new ActionListener() {
  478. public void actionPerformed(ActionEvent e) { Rotate90right();}
  479. });
  480. toolbar.add(rotaterightButton);
  481.  
  482. // Add toolbar into panel with flow layout for spacing
  483. JPanel flow = new JPanel();
  484. flow.add(toolbar);
  485.  
  486. contentPane.add(flow, BorderLayout.WEST);
  487.  
  488. // building is done - arrange the components
  489. showFilename(null);
  490. setButtonsEnabled(false);
  491. frame.pack();
  492.  
  493. // place the frame at the center of the screen and show
  494. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  495. frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
  496. frame.setVisible(true);
  497. }
  498.  
  499. /**
  500. * Create the main frame's menu bar.
  501. *
  502. * @param frame The frame that the menu bar should be added to.
  503. */
  504. private void makeMenuBar(JFrame frame)
  505. {
  506. final int SHORTCUT_MASK =
  507. Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  508.  
  509. JMenuBar menubar = new JMenuBar();
  510. frame.setJMenuBar(menubar);
  511.  
  512. JMenu menu;
  513. JMenuItem item;
  514.  
  515. // create the File menu
  516. menu = new JMenu("File");
  517. menubar.add(menu);
  518.  
  519. item = new JMenuItem("Open...");
  520. item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
  521. item.addActionListener(new ActionListener() {
  522. public void actionPerformed(ActionEvent e) { openFile(); }
  523. });
  524. menu.add(item);
  525.  
  526. item = new JMenuItem("Close");
  527. item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
  528. item.addActionListener(new ActionListener() {
  529. public void actionPerformed(ActionEvent e) { close(); }
  530. });
  531. menu.add(item);
  532. menu.addSeparator();
  533.  
  534. item = new JMenuItem("Save As...");
  535. item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, SHORTCUT_MASK));
  536. item.addActionListener(new ActionListener() {
  537. public void actionPerformed(ActionEvent e) { saveAs(); }
  538. });
  539. menu.add(item);
  540. menu.addSeparator();
  541.  
  542. item = new JMenuItem("Quit");
  543. item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
  544. item.addActionListener(new ActionListener() {
  545. public void actionPerformed(ActionEvent e) { quit(); }
  546. });
  547. menu.add(item);
  548.  
  549.  
  550. // create the Filter menu
  551. menu = new JMenu("Filter");
  552. menubar.add(menu);
  553.  
  554. for(final Filter filter : filters) {
  555. item = new JMenuItem(filter.getName());
  556. item.addActionListener(new ActionListener() {
  557. public void actionPerformed(ActionEvent e) {
  558. applyFilter(filter);
  559. }
  560. });
  561. menu.add(item);
  562. }
  563.  
  564. // create the Help menu
  565. menu = new JMenu("Help");
  566. menubar.add(menu);
  567.  
  568. item = new JMenuItem("About ImageViewer...");
  569. item.addActionListener(new ActionListener() {
  570. public void actionPerformed(ActionEvent e) { showAbout(); }
  571. });
  572. menu.add(item);
  573.  
  574. }
  575. }
Add Comment
Please, Sign In to add comment