Advertisement
Guest User

Untitled

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