Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab01;
- 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;
- JScrollPane scrollPane;
- JLabel imgLabel;
- JButton zad1Button;
- JButton zad2Button;
- JButton zad3Button;
- JButton zad4Button;
- JButton zad5Button;
- JButton zad6Button;
- 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));
- }
- });
- zad1Button = new JButton("Zadanie 1");
- outputPanel.add(zad1Button);
- zad1Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Zad1(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- zad2Button = new JButton("Zadanie 2");
- outputPanel.add(zad2Button);
- zad2Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Zad2(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- zad3Button = new JButton("Zadanie 3");
- outputPanel.add(zad3Button);
- zad3Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Zad3(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- zad4Button = new JButton("Zadanie 4");
- outputPanel.add(zad4Button);
- zad4Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Zad4(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- zad5Button = new JButton("Zadanie 5");
- outputPanel.add(zad5Button);
- zad5Button.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Zad5(image);
- imgLabel.setIcon(new ImageIcon(image));
- }
- });
- zad6Button = new JButton("Zadanie 6");
- 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 >>> 8;
- 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 grey = (int) (0.299*r + 0.587*g + 0.114*b);
- r = g = b = grey;
- //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;
- int t = 100;
- if(r>t || g>t || b>t){
- r = g = b = 255;
- }
- else{
- r = g = 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;
- int newA = 255-a;
- int newR = 255-r;
- int newG = 255-g;
- int newB = 255-b;
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = newB | (newG << 8) | (newR << 16) | (newA << 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 N = 2;
- 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 newA = a - 128;
- int newR = r - 128;
- int newG = g - 128;
- int newB = b - 128;
- int A = (newA*N)+128;
- int R = (newR*N)+128;
- int G = (newG*N)+128;
- int B = (newB*N)+128;
- if(A < 0){
- A = 0;
- }
- if(R < 0){
- R = 0;
- }
- if(G < 0){
- G = 0;
- }
- if(B < 0){
- B = 0;
- }
- if(A > 255){
- A = 255;
- }
- if(R > 255){
- R = 255;
- }
- if(G > 255){
- G = 255;
- }
- if(B > 255){
- B = 255;
- }
- //tu można modyfikować wartość kanałów
- //zapis kanałów
- int RGB = B | (G << 8) | (R << 16) | (R << 24);
- img.setRGB(x, y, RGB);
- }
- }
- }
- private static void Zad6(BufferedImage img){
- }
- public static void main(String args[]) {
- JFrame frame = new Main();
- frame.pack();
- frame.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement