Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package grafika;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import javax.swing.*;
- import java.io.*;
- import java.net.*;
- import javax.imageio.*;
- public class Main extends JFrame {
- BufferedImage image;
- JLabel promptLabel;
- JTextField prompt;
- JButton promptButton;
- JFileChooser fileChooser;
- JButton loadButton;
- JButton processingButton;
- JButton zad1Button;
- JButton zad2Button;
- JButton zad3Button;
- JButton zad4Button;
- JButton zad5Button;
- JButton zad6Button;
- JScrollPane scrollPane;
- JLabel imgLabel;
- public Main() {
- super("Image processing");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- Container contentPane = getContentPane();
- JPanel inputPanel = new JPanel();
- promptLabel = new JLabel("Filename:");
- inputPanel.add(promptLabel);
- prompt = new JTextField(20);
- inputPanel.add(prompt);
- promptButton = new JButton("Browse");
- inputPanel.add(promptButton);
- contentPane.add(inputPanel, BorderLayout.NORTH);
- fileChooser = new JFileChooser();
- promptButton.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- int returnValue
- = fileChooser.showOpenDialog(null);
- if (returnValue
- == JFileChooser.APPROVE_OPTION) {
- File selectedFile
- = fileChooser.getSelectedFile();
- if (selectedFile != null) {
- prompt.setText(selectedFile.getAbsolutePath());
- }
- }
- }
- }
- );
- imgLabel = new JLabel();
- scrollPane = new JScrollPane(imgLabel);
- scrollPane.setPreferredSize(new Dimension(700, 500));
- contentPane.add(scrollPane, BorderLayout.CENTER);
- JPanel outputPanel = new JPanel();
- loadButton = new JButton("Load");
- outputPanel.add(loadButton);
- loadButton.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- String name = prompt.getText();
- File file = new File(name);
- if (file.exists()) {
- image = ImageIO.read(file.toURL());
- if (image == null) {
- System.err.println("Invalid input file format");
- } else {
- imgLabel.setIcon(new ImageIcon(image));
- }
- } else {
- System.err.println("Bad filename");
- }
- } catch (MalformedURLException mur) {
- System.err.println("Bad filename");
- } catch (IOException ioe) {
- System.err.println("Error reading file");
- }
- }
- }
- );
- processingButton = new JButton("Processing");
- outputPanel.add(processingButton);
- processingButton.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Processing(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- contentPane.add(outputPanel, BorderLayout.SOUTH);
- zad1Button = new JButton("zad1");
- outputPanel.add(zad1Button);
- zad1Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- zad1(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- contentPane.add(outputPanel, BorderLayout.SOUTH);
- zad2Button = new JButton("zad2");
- outputPanel.add(zad2Button);
- zad2Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- zad2(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- contentPane.add(outputPanel, BorderLayout.SOUTH);
- zad3Button = new JButton("zad3");
- outputPanel.add(zad3Button);
- zad3Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- zad3(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- contentPane.add(outputPanel, BorderLayout.SOUTH);
- zad4Button = new JButton("zad4");
- outputPanel.add(zad4Button);
- zad4Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- zad4(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- contentPane.add(outputPanel, BorderLayout.SOUTH);
- zad5Button = new JButton("zad5");
- outputPanel.add(zad5Button);
- zad5Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- zad5(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- contentPane.add(outputPanel, BorderLayout.SOUTH);
- zad6Button = new JButton("zad6");
- outputPanel.add(zad6Button);
- zad6Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- zad6(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- contentPane.add(outputPanel, BorderLayout.SOUTH);
- }
- private static void Processing(BufferedImage img) {
- int w = img.getWidth(null);
- int h = img.getHeight(null);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- int rgb = img.getRGB(x, y);
- int a = (rgb & 0xff000000) >>> 24;
- int r = (rgb & 0x00ff0000) >>> 16;
- int g = (rgb & 0x0000ff00) >>> 8;
- int b = rgb & 0x000000ff;
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = b | (g << 8) | (r << 16) | (a << 24);
- img.setRGB(x, y, RGB);
- }
- }
- }
- private static void zad1(BufferedImage img) {
- int w = img.getWidth(null);
- int h = img.getHeight(null);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- int rgb = img.getRGB(x, y);
- int a = (rgb & 0xff000000) >>> 24;
- int r = (rgb & 0x00ff0000) >>> 16;
- int g = 0;
- int b = 0;
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = b | (g << 8) | (r << 16) | (a << 24);
- img.setRGB(x, y, RGB);
- }
- }
- }
- private static void zad2(BufferedImage img) {
- int w = img.getWidth(null);
- int h = img.getHeight(null);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- int rgb = img.getRGB(x, y);
- int a = (rgb & 0xff000000) >>> 24;
- int r = (rgb & 0x00ff0000) >>> 16;
- int g = (rgb & 0x0000ff00) >>> 8;
- int b = rgb & 0x000000ff;
- int gray = (int) (0.222*r + 0.587*g + 0.113*b);
- r = gray;
- g = gray;
- b = gray;
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = b | (g << 8) | (r << 16) | (a << 24);
- img.setRGB(x, y, RGB);
- }
- }
- }
- private static void zad3(BufferedImage img) {
- int w = img.getWidth(null);
- int h = img.getHeight(null);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- int rgb = img.getRGB(x, y);
- int a = (rgb & 0xff000000) >>> 24;
- int r = (rgb & 0x00ff0000) >>> 16;
- int g = (rgb & 0x0000ff00) >>> 8;
- int b = rgb & 0x000000ff;
- if(r>150 || g>150 || b>150) {
- r = 255;
- g = 255;
- b = 255;
- }
- else {
- r = 0;
- g = 0;
- b = 0;
- }
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = b | (g << 8) | (r << 16) | (a << 24);
- img.setRGB(x, y, RGB);
- }
- }
- }
- private static void zad4(BufferedImage img) {
- int w = img.getWidth(null);
- int h = img.getHeight(null);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- int rgb = img.getRGB(x, y);
- int a = (rgb & 0xff000000) >>> 24;
- int r = (rgb & 0x00ff0000) >>> 16;
- int g = (rgb & 0x0000ff00) >>> 8;
- int b = rgb & 0x000000ff;
- r = 255 - r;
- g = 255 - g;
- b = 255 - b;
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = b | (g << 8) | (r << 16) | (a << 24);
- img.setRGB(x, y, RGB);
- }
- }
- }
- private static void zad5(BufferedImage img) {
- int w = img.getWidth(null);
- int h = img.getHeight(null);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- int rgb = img.getRGB(x, y);
- int a = (rgb & 0xff000000) >>> 24;
- int r = (rgb & 0x00ff0000) >>> 16;
- int g = (rgb & 0x0000ff00) >>> 8;
- int b = rgb & 0x000000ff;
- r = r - 128;
- g = g - 128;
- b = b - 128;
- r = (int) (1.5*r);
- g = (int) (1.5*g);
- b = (int) (1.5*b);
- r = r + 128;
- g = g + 128;
- b = b + 128;
- if (r<0) {
- r=0;
- }
- if (r>255) {
- r=255;
- }
- if (g<0) {
- g=0;
- }
- if (g>255) {
- g=255;
- }
- if (b<0) {
- b=0;
- }
- if (b>255) {
- b=255;
- }
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = b | (g << 8) | (r << 16) | (a << 24);
- img.setRGB(x, y, RGB);
- }
- }
- }
- private static void zad6(BufferedImage img) {
- int w = img.getWidth(null);
- int h = img.getHeight(null);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- int rgb = img.getRGB(x, y);
- int a = (rgb & 0xff000000) >>> 24;
- int r = (rgb & 0x00ff0000) >>> 16;
- int g = (rgb & 0x0000ff00) >>> 8;
- int b = rgb & 0x000000ff;
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = b | (g << 8) | (r << 16) | (a << 24);
- img.setRGB(x, y, RGB);
- }
- }
- }
- public static void main(String args[]) {
- JFrame frame = new Main();
- frame.pack();
- frame.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement