Advertisement
Guest User

Grafika

a guest
Feb 25th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.33 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package grafika;
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.awt.image.*;
  11. import javax.swing.*;
  12. import java.io.*;
  13. import java.net.*;
  14. import javax.imageio.*;
  15.  
  16. public class Main extends JFrame {
  17.  
  18. BufferedImage image;
  19. JLabel promptLabel;
  20. JTextField prompt;
  21. JButton promptButton;
  22. JFileChooser fileChooser;
  23. JButton loadButton;
  24. JButton processingButton;
  25. JButton zad1Button;
  26. JButton zad2Button;
  27. JButton zad3Button;
  28. JButton zad4Button;
  29. JButton zad5Button;
  30. JButton zad6Button;
  31.  
  32. JScrollPane scrollPane;
  33. JLabel imgLabel;
  34.  
  35. public Main() {
  36. super("Image processing");
  37. setDefaultCloseOperation(EXIT_ON_CLOSE);
  38. Container contentPane = getContentPane();
  39. JPanel inputPanel = new JPanel();
  40. promptLabel = new JLabel("Filename:");
  41. inputPanel.add(promptLabel);
  42. prompt = new JTextField(20);
  43. inputPanel.add(prompt);
  44. promptButton = new JButton("Browse");
  45. inputPanel.add(promptButton);
  46. contentPane.add(inputPanel, BorderLayout.NORTH);
  47. fileChooser = new JFileChooser();
  48. promptButton.addActionListener(
  49. new ActionListener() {
  50. public void actionPerformed(ActionEvent e) {
  51. int returnValue
  52. = fileChooser.showOpenDialog(null);
  53. if (returnValue
  54. == JFileChooser.APPROVE_OPTION) {
  55. File selectedFile
  56. = fileChooser.getSelectedFile();
  57. if (selectedFile != null) {
  58. prompt.setText(selectedFile.getAbsolutePath());
  59. }
  60. }
  61. }
  62. }
  63. );
  64.  
  65. imgLabel = new JLabel();
  66. scrollPane = new JScrollPane(imgLabel);
  67. scrollPane.setPreferredSize(new Dimension(700, 500));
  68. contentPane.add(scrollPane, BorderLayout.CENTER);
  69.  
  70. JPanel outputPanel = new JPanel();
  71. loadButton = new JButton("Load");
  72. outputPanel.add(loadButton);
  73. loadButton.addActionListener(
  74. new ActionListener() {
  75. public void actionPerformed(ActionEvent e) {
  76. try {
  77. String name = prompt.getText();
  78. File file = new File(name);
  79. if (file.exists()) {
  80. image = ImageIO.read(file.toURL());
  81. if (image == null) {
  82. System.err.println("Invalid input file format");
  83. } else {
  84. imgLabel.setIcon(new ImageIcon(image));
  85. }
  86. } else {
  87. System.err.println("Bad filename");
  88. }
  89. } catch (MalformedURLException mur) {
  90. System.err.println("Bad filename");
  91. } catch (IOException ioe) {
  92. System.err.println("Error reading file");
  93. }
  94. }
  95. }
  96. );
  97.  
  98. processingButton = new JButton("Processing");
  99. outputPanel.add(processingButton);
  100. processingButton.addActionListener(
  101. new ActionListener() {
  102. public void actionPerformed(ActionEvent e) {
  103. Processing(image);
  104. imgLabel.setIcon(new ImageIcon(image));
  105. }
  106. });
  107.  
  108. contentPane.add(outputPanel, BorderLayout.SOUTH);
  109.  
  110. zad1Button = new JButton("zad1");
  111. outputPanel.add(zad1Button);
  112. zad1Button.addActionListener(
  113. new ActionListener() {
  114. public void actionPerformed(ActionEvent e) {
  115. zad1(image);
  116. imgLabel.setIcon(new ImageIcon(image));
  117. }
  118. });
  119.  
  120. contentPane.add(outputPanel, BorderLayout.SOUTH);
  121.  
  122. zad2Button = new JButton("zad2");
  123. outputPanel.add(zad2Button);
  124. zad2Button.addActionListener(
  125. new ActionListener() {
  126. public void actionPerformed(ActionEvent e) {
  127. zad2(image);
  128. imgLabel.setIcon(new ImageIcon(image));
  129. }
  130. });
  131.  
  132. contentPane.add(outputPanel, BorderLayout.SOUTH);
  133.  
  134. zad3Button = new JButton("zad3");
  135. outputPanel.add(zad3Button);
  136. zad3Button.addActionListener(
  137. new ActionListener() {
  138. public void actionPerformed(ActionEvent e) {
  139. zad3(image);
  140. imgLabel.setIcon(new ImageIcon(image));
  141. }
  142. });
  143.  
  144. contentPane.add(outputPanel, BorderLayout.SOUTH);
  145.  
  146. zad4Button = new JButton("zad4");
  147. outputPanel.add(zad4Button);
  148. zad4Button.addActionListener(
  149. new ActionListener() {
  150. public void actionPerformed(ActionEvent e) {
  151. zad4(image);
  152. imgLabel.setIcon(new ImageIcon(image));
  153. }
  154. });
  155.  
  156. contentPane.add(outputPanel, BorderLayout.SOUTH);
  157.  
  158. zad5Button = new JButton("zad5");
  159. outputPanel.add(zad5Button);
  160. zad5Button.addActionListener(
  161. new ActionListener() {
  162. public void actionPerformed(ActionEvent e) {
  163. zad5(image);
  164. imgLabel.setIcon(new ImageIcon(image));
  165. }
  166. });
  167.  
  168. contentPane.add(outputPanel, BorderLayout.SOUTH);
  169.  
  170. zad6Button = new JButton("zad6");
  171. outputPanel.add(zad6Button);
  172. zad6Button.addActionListener(
  173. new ActionListener() {
  174. public void actionPerformed(ActionEvent e) {
  175. zad6(image);
  176. imgLabel.setIcon(new ImageIcon(image));
  177. }
  178. });
  179.  
  180. contentPane.add(outputPanel, BorderLayout.SOUTH);
  181. }
  182.  
  183. private static void Processing(BufferedImage img) {
  184. int w = img.getWidth(null);
  185. int h = img.getHeight(null);
  186. for (int x = 0; x < w; x++) {
  187. for (int y = 0; y < h; y++) {
  188. int rgb = img.getRGB(x, y);
  189. int a = (rgb & 0xff000000) >>> 24;
  190. int r = (rgb & 0x00ff0000) >>> 16;
  191. int g = (rgb & 0x0000ff00) >>> 8;
  192. int b = rgb & 0x000000ff;
  193.  
  194. //tu można modyfikować wartość kanałów
  195. //zapis kanałów
  196. int RGB = b | (g << 8) | (r << 16) | (a << 24);
  197. img.setRGB(x, y, RGB);
  198. }
  199. }
  200. }
  201.  
  202. private static void zad1(BufferedImage img) {
  203. int w = img.getWidth(null);
  204. int h = img.getHeight(null);
  205. for (int x = 0; x < w; x++) {
  206. for (int y = 0; y < h; y++) {
  207. int rgb = img.getRGB(x, y);
  208. int a = (rgb & 0xff000000) >>> 24;
  209. int r = (rgb & 0x00ff0000) >>> 16;
  210. int g = 0;
  211. int b = 0;
  212.  
  213. //tu można modyfikować wartość kanałów
  214. //zapis kanałów
  215. int RGB = b | (g << 8) | (r << 16) | (a << 24);
  216. img.setRGB(x, y, RGB);
  217. }
  218. }
  219. }
  220.  
  221. private static void zad2(BufferedImage img) {
  222. int w = img.getWidth(null);
  223. int h = img.getHeight(null);
  224. for (int x = 0; x < w; x++) {
  225. for (int y = 0; y < h; y++) {
  226. int rgb = img.getRGB(x, y);
  227. int a = (rgb & 0xff000000) >>> 24;
  228. int r = (rgb & 0x00ff0000) >>> 16;
  229. int g = (rgb & 0x0000ff00) >>> 8;
  230. int b = rgb & 0x000000ff;
  231. int gray = (int) (0.222*r + 0.587*g + 0.113*b);
  232.  
  233. r = gray;
  234. g = gray;
  235. b = gray;
  236.  
  237. //tu można modyfikować wartość kanałów
  238. //zapis kanałów
  239. int RGB = b | (g << 8) | (r << 16) | (a << 24);
  240. img.setRGB(x, y, RGB);
  241. }
  242. }
  243. }
  244.  
  245. private static void zad3(BufferedImage img) {
  246. int w = img.getWidth(null);
  247. int h = img.getHeight(null);
  248. for (int x = 0; x < w; x++) {
  249. for (int y = 0; y < h; y++) {
  250. int rgb = img.getRGB(x, y);
  251. int a = (rgb & 0xff000000) >>> 24;
  252. int r = (rgb & 0x00ff0000) >>> 16;
  253. int g = (rgb & 0x0000ff00) >>> 8;
  254. int b = rgb & 0x000000ff;
  255.  
  256. if(r>150 || g>150 || b>150) {
  257. r = 255;
  258. g = 255;
  259. b = 255;
  260.  
  261. }
  262.  
  263. else {
  264. r = 0;
  265. g = 0;
  266. b = 0;
  267.  
  268. }
  269.  
  270. //tu można modyfikować wartość kanałów
  271. //zapis kanałów
  272. int RGB = b | (g << 8) | (r << 16) | (a << 24);
  273. img.setRGB(x, y, RGB);
  274. }
  275. }
  276. }
  277.  
  278. private static void zad4(BufferedImage img) {
  279. int w = img.getWidth(null);
  280. int h = img.getHeight(null);
  281. for (int x = 0; x < w; x++) {
  282. for (int y = 0; y < h; y++) {
  283. int rgb = img.getRGB(x, y);
  284. int a = (rgb & 0xff000000) >>> 24;
  285. int r = (rgb & 0x00ff0000) >>> 16;
  286. int g = (rgb & 0x0000ff00) >>> 8;
  287. int b = rgb & 0x000000ff;
  288.  
  289. r = 255 - r;
  290. g = 255 - g;
  291. b = 255 - b;
  292.  
  293.  
  294. //tu można modyfikować wartość kanałów
  295. //zapis kanałów
  296. int RGB = b | (g << 8) | (r << 16) | (a << 24);
  297. img.setRGB(x, y, RGB);
  298. }
  299. }
  300. }
  301.  
  302. private static void zad5(BufferedImage img) {
  303. int w = img.getWidth(null);
  304. int h = img.getHeight(null);
  305. for (int x = 0; x < w; x++) {
  306. for (int y = 0; y < h; y++) {
  307. int rgb = img.getRGB(x, y);
  308. int a = (rgb & 0xff000000) >>> 24;
  309. int r = (rgb & 0x00ff0000) >>> 16;
  310. int g = (rgb & 0x0000ff00) >>> 8;
  311. int b = rgb & 0x000000ff;
  312.  
  313. r = r - 128;
  314. g = g - 128;
  315. b = b - 128;
  316.  
  317. r = (int) (1.5*r);
  318. g = (int) (1.5*g);
  319. b = (int) (1.5*b);
  320.  
  321. r = r + 128;
  322. g = g + 128;
  323. b = b + 128;
  324.  
  325. if (r<0) {
  326. r=0;
  327. }
  328. if (r>255) {
  329. r=255;
  330. }
  331. if (g<0) {
  332. g=0;
  333. }
  334. if (g>255) {
  335. g=255;
  336. }
  337. if (b<0) {
  338. b=0;
  339. }
  340. if (b>255) {
  341. b=255;
  342. }
  343.  
  344.  
  345. //tu można modyfikować wartość kanałów
  346. //zapis kanałów
  347. int RGB = b | (g << 8) | (r << 16) | (a << 24);
  348. img.setRGB(x, y, RGB);
  349. }
  350. }
  351. }
  352.  
  353. private static void zad6(BufferedImage img) {
  354. int w = img.getWidth(null);
  355. int h = img.getHeight(null);
  356. for (int x = 0; x < w; x++) {
  357. for (int y = 0; y < h; y++) {
  358. int rgb = img.getRGB(x, y);
  359. int a = (rgb & 0xff000000) >>> 24;
  360. int r = (rgb & 0x00ff0000) >>> 16;
  361. int g = (rgb & 0x0000ff00) >>> 8;
  362. int b = rgb & 0x000000ff;
  363.  
  364. //tu można modyfikować wartość kanałów
  365. //zapis kanałów
  366. int RGB = b | (g << 8) | (r << 16) | (a << 24);
  367. img.setRGB(x, y, RGB);
  368. }
  369. }
  370. }
  371.  
  372. public static void main(String args[]) {
  373. JFrame frame = new Main();
  374. frame.pack();
  375. frame.show();
  376. }
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement