显示和操作更新的图像

发布于 2024-12-10 06:57:31 字数 9071 浏览 0 评论 0原文

该程序应该首先打开一个图像,然后允许通过灰度、缩放和旋转方法对其进行操作(目前不起作用;忽略它)。但是,我不确定如何在每次执行方法时调用更新的图像。例如,如果我对图像进行灰度化,它就会变成灰度化。但是,如果我随后将其缩放到不同的尺寸,则生成的图像是原始图像的缩放版本,而不是缩放的灰度图像。我尝试输入“image2 = image;”无济于事。我该如何解决这个问题? 谢谢。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class Picture{
    JFileChooser fileChooser = new JFileChooser();    
    final JFrame frame = new JFrame("Edit Image");    
    Container content;
    static BufferedImage image;
    BufferedImage image2;
    JLabel imageLabel;

    public Picture() {
        //asks for image file as input
        fileChooser.setDialogTitle("Choose an image file to begin:");
        fileChooser.showOpenDialog(frame);
        File selectedFile = fileChooser.getSelectedFile();
        if (fileChooser.getSelectedFile() != null) {
            try {
                //reads File as image
                image = ImageIO.read(selectedFile);
            } 
            catch (IOException e) {
                System.out.println("Invalid image file: " + selectedFile);
                System.exit(0);
            }
        }
        else {
            System.out.println("No File Selected!");
        }
    }

    public int width() {
        //returns width of present image
        int width = image.getWidth();
        return width;
    }

    public int height() {
        //returns height of present image
        int height = image.getHeight();
        return height;
    }

    public void getImage() {
        this.image2 = image;
    }

    public void saveImage() {
        //saves current image as JPEG
        fileChooser.setDialogTitle("Save this image?");
        fileChooser.showSaveDialog(frame);
        try {
            //writes new file
            ImageIO.write(this.image, "JPG", fileChooser.getSelectedFile());
        }
        catch (IOException f) {
            System.out.println("Saving failed! Could not save image.");
        }
    }

    public void show() {
        //set frame title, set it visible, etc
        content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        //add the image to the frame
        ImageIcon icon = new ImageIcon(image);
        imageLabel = new JLabel(icon);
        frame.setContentPane(imageLabel);

        //add a menubar on the frame with a single option: saving the image
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu progName = new JMenu("Edit Image");
        progName.setBackground(Color.RED);
        menuBar.add(progName);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        JMenu editMenu = new JMenu("Edit");
        menuBar.add(editMenu);

        ImageIcon exitIcon = new ImageIcon("app-exit.png");
        JMenuItem exitAction = new JMenuItem("Exit", exitIcon);
        progName.add(exitAction);
        exitAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then exits
                        saveImage();
                        System.exit(0);
                    }
                    else {
                        //exits without saving
                        System.exit(0);
                    }
                }
            });

        ImageIcon newIcon = new ImageIcon("new-image.png");
        JMenuItem newAction = new JMenuItem("Open Image", newIcon);
        fileMenu.add(newAction);
        newAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then asks asks for new image file
                        saveImage();
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                    else {
                        //asks for new image file since user did not want to save original
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                }
            });

        ImageIcon saveIcon = new ImageIcon("save-image.png");
        JMenuItem saveAction = new JMenuItem("Save Image As...", saveIcon);
        fileMenu.add(saveAction);
        saveAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //opens save image method
                    saveImage();
                }
            });
        ImageIcon gsIcon = new ImageIcon("grayscale-image.png");
        JMenuItem grayScale = new JMenuItem("Grayscale", gsIcon);
        editMenu.add(grayScale);
        grayScale.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //grabs height and width of image, then grayscales it
                    grayscale(width(), height());
                }
            });

        ImageIcon scaleIcon = new ImageIcon("scale-image.png");
        JMenuItem scaleImg = new JMenuItem("Scale Image", scaleIcon);
        editMenu.add(scaleImg);
        scaleImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //asks for height and width to create new image
                    ImageIcon widthIcon = new ImageIcon("LR-arrows.png");
                    String scaleWidth = (String)JOptionPane.showInputDialog(null,"What should the new width be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    ImageIcon heightIcon = new ImageIcon("UD-arrows.png");
                    String scaleHeight = (String)JOptionPane.showInputDialog(null,"What should the new height be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    //turns user input strings into doubles
                    double x = Double.parseDouble(scaleWidth);
                    double y = Double.parseDouble(scaleHeight);
                    //casts doubles as ints
                    int newWidth = (int)x;
                    int newHeight = (int)y;
                    //resizes frame to fit new image dimensions
                    frame.setSize(newWidth, newHeight);
                    //calls scale method to resize image using given dimensions
                    scale(newWidth, newHeight);
                }
            });
        ImageIcon rotateIcon = new ImageIcon("rotate-image.png");
        JMenuItem rotateImg = new JMenuItem("Rotate Image", rotateIcon);
        editMenu.add(rotateImg);
        rotateImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                }
            });

        //paint the frame
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }

    // convert to grayscale
    public void grayscale(int width, int height) {
        // create a grayscale image with original dimensions
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

        // convert colored image to grayscale
        ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
        grayScale.filter(image,image2);
        imageLabel.setIcon(new ImageIcon(image2));
        getImage();
    }

    //scales image by a given factor
    public void scale(int width, int height){
        //uses user-input dimensions to create new image
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image2.createGraphics();
        //gets new dimensions and resizes image
        g.drawImage(image, 0, 0, image2.getWidth(), image2.getHeight(), 0, 0, width(), height(), null);  
        imageLabel.setIcon(new ImageIcon(image2));
        getImage();
    }

    //rotates the image
    public void rotate(int width, int height, int theta) {

    }

    public static void main(String[] args) {
        Picture p = new Picture();
        p.show();
    }
}

This program should first open an image, and then allow it to be manipulated via grayscale, scale, and rotate methods (not functional at the moment; disregard it). However, I'm not sure how I can call upon the updated image every time a method is performed. For example, if I grayscale an image, it goes to grayscale. But if I then scale it to a different size, the resulting image is a scaled version of the original image, not a scaled grayscale image. I tried putting in "image2 = image;" to no avail. How can I fix this?
Thanks.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class Picture{
    JFileChooser fileChooser = new JFileChooser();    
    final JFrame frame = new JFrame("Edit Image");    
    Container content;
    static BufferedImage image;
    BufferedImage image2;
    JLabel imageLabel;

    public Picture() {
        //asks for image file as input
        fileChooser.setDialogTitle("Choose an image file to begin:");
        fileChooser.showOpenDialog(frame);
        File selectedFile = fileChooser.getSelectedFile();
        if (fileChooser.getSelectedFile() != null) {
            try {
                //reads File as image
                image = ImageIO.read(selectedFile);
            } 
            catch (IOException e) {
                System.out.println("Invalid image file: " + selectedFile);
                System.exit(0);
            }
        }
        else {
            System.out.println("No File Selected!");
        }
    }

    public int width() {
        //returns width of present image
        int width = image.getWidth();
        return width;
    }

    public int height() {
        //returns height of present image
        int height = image.getHeight();
        return height;
    }

    public void getImage() {
        this.image2 = image;
    }

    public void saveImage() {
        //saves current image as JPEG
        fileChooser.setDialogTitle("Save this image?");
        fileChooser.showSaveDialog(frame);
        try {
            //writes new file
            ImageIO.write(this.image, "JPG", fileChooser.getSelectedFile());
        }
        catch (IOException f) {
            System.out.println("Saving failed! Could not save image.");
        }
    }

    public void show() {
        //set frame title, set it visible, etc
        content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        //add the image to the frame
        ImageIcon icon = new ImageIcon(image);
        imageLabel = new JLabel(icon);
        frame.setContentPane(imageLabel);

        //add a menubar on the frame with a single option: saving the image
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu progName = new JMenu("Edit Image");
        progName.setBackground(Color.RED);
        menuBar.add(progName);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        JMenu editMenu = new JMenu("Edit");
        menuBar.add(editMenu);

        ImageIcon exitIcon = new ImageIcon("app-exit.png");
        JMenuItem exitAction = new JMenuItem("Exit", exitIcon);
        progName.add(exitAction);
        exitAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then exits
                        saveImage();
                        System.exit(0);
                    }
                    else {
                        //exits without saving
                        System.exit(0);
                    }
                }
            });

        ImageIcon newIcon = new ImageIcon("new-image.png");
        JMenuItem newAction = new JMenuItem("Open Image", newIcon);
        fileMenu.add(newAction);
        newAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then asks asks for new image file
                        saveImage();
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                    else {
                        //asks for new image file since user did not want to save original
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                }
            });

        ImageIcon saveIcon = new ImageIcon("save-image.png");
        JMenuItem saveAction = new JMenuItem("Save Image As...", saveIcon);
        fileMenu.add(saveAction);
        saveAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //opens save image method
                    saveImage();
                }
            });
        ImageIcon gsIcon = new ImageIcon("grayscale-image.png");
        JMenuItem grayScale = new JMenuItem("Grayscale", gsIcon);
        editMenu.add(grayScale);
        grayScale.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //grabs height and width of image, then grayscales it
                    grayscale(width(), height());
                }
            });

        ImageIcon scaleIcon = new ImageIcon("scale-image.png");
        JMenuItem scaleImg = new JMenuItem("Scale Image", scaleIcon);
        editMenu.add(scaleImg);
        scaleImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //asks for height and width to create new image
                    ImageIcon widthIcon = new ImageIcon("LR-arrows.png");
                    String scaleWidth = (String)JOptionPane.showInputDialog(null,"What should the new width be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    ImageIcon heightIcon = new ImageIcon("UD-arrows.png");
                    String scaleHeight = (String)JOptionPane.showInputDialog(null,"What should the new height be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    //turns user input strings into doubles
                    double x = Double.parseDouble(scaleWidth);
                    double y = Double.parseDouble(scaleHeight);
                    //casts doubles as ints
                    int newWidth = (int)x;
                    int newHeight = (int)y;
                    //resizes frame to fit new image dimensions
                    frame.setSize(newWidth, newHeight);
                    //calls scale method to resize image using given dimensions
                    scale(newWidth, newHeight);
                }
            });
        ImageIcon rotateIcon = new ImageIcon("rotate-image.png");
        JMenuItem rotateImg = new JMenuItem("Rotate Image", rotateIcon);
        editMenu.add(rotateImg);
        rotateImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                }
            });

        //paint the frame
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }

    // convert to grayscale
    public void grayscale(int width, int height) {
        // create a grayscale image with original dimensions
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

        // convert colored image to grayscale
        ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
        grayScale.filter(image,image2);
        imageLabel.setIcon(new ImageIcon(image2));
        getImage();
    }

    //scales image by a given factor
    public void scale(int width, int height){
        //uses user-input dimensions to create new image
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image2.createGraphics();
        //gets new dimensions and resizes image
        g.drawImage(image, 0, 0, image2.getWidth(), image2.getHeight(), 0, 0, width(), height(), null);  
        imageLabel.setIcon(new ImageIcon(image2));
        getImage();
    }

    //rotates the image
    public void rotate(int width, int height, int theta) {

    }

    public static void main(String[] args) {
        Picture p = new Picture();
        p.show();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-12-17 06:57:31

图片

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class Picture{
    JFileChooser fileChooser = new JFileChooser();
    final JFrame frame = new JFrame("Edit Image");
    Container content;
    static BufferedImage image;
    BufferedImage image2;
    JLabel imageLabel;

    public Picture() {
        //asks for image file as input
        fileChooser.setDialogTitle("Choose an image file to begin:");
        fileChooser.showOpenDialog(frame);
        File selectedFile = fileChooser.getSelectedFile();
        if (fileChooser.getSelectedFile() != null) {
            try {
                //reads File as image
                image = ImageIO.read(selectedFile);
            }
            catch (IOException e) {
                System.out.println("Invalid image file: " + selectedFile);
                System.exit(0);
            }
        }
        else {
            System.out.println("No File Selected!");
        }
    }

    public int width() {
        //returns width of present image
        int width = image.getWidth();
        return width;
    }

    public int height() {
        //returns height of present image
        int height = image.getHeight();
        return height;
    }
/*
    public void getImage() {
        this.image2 = image;
    }
*/
    public void saveImage() {
        //saves current image as JPEG
        fileChooser.setDialogTitle("Save this image?");
        fileChooser.showSaveDialog(frame);
        try {
            //writes new file
            ImageIO.write(this.image, "JPG", fileChooser.getSelectedFile());
        }
        catch (IOException f) {
            System.out.println("Saving failed! Could not save image.");
        }
    }

    public void show() {
        //set frame title, set it visible, etc
        content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        //add the image to the frame
        ImageIcon icon = new ImageIcon(image);
        imageLabel = new JLabel(icon);
        frame.setContentPane(imageLabel);

        //add a menubar on the frame with a single option: saving the image
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu progName = new JMenu("Edit Image");
        progName.setBackground(Color.RED);
        menuBar.add(progName);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        JMenu editMenu = new JMenu("Edit");
        menuBar.add(editMenu);

        ImageIcon exitIcon = new ImageIcon("app-exit.png");
        JMenuItem exitAction = new JMenuItem("Exit", exitIcon);
        progName.add(exitAction);
        exitAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then exits
                        saveImage();
                        System.exit(0);
                    }
                    else {
                        //exits without saving
                        System.exit(0);
                    }
                }
            });

        ImageIcon newIcon = new ImageIcon("new-image.png");
        JMenuItem newAction = new JMenuItem("Open Image", newIcon);
        fileMenu.add(newAction);
        newAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then asks asks for new image file
                        saveImage();
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                    else {
                        //asks for new image file since user did not want to save original
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                }
            });

        ImageIcon saveIcon = new ImageIcon("save-image.png");
        JMenuItem saveAction = new JMenuItem("Save Image As...", saveIcon);
        fileMenu.add(saveAction);
        saveAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //opens save image method
                    saveImage();
                }
            });
        ImageIcon gsIcon = new ImageIcon("grayscale-image.png");
        JMenuItem grayScale = new JMenuItem("Grayscale", gsIcon);
        editMenu.add(grayScale);
        grayScale.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //grabs height and width of image, then grayscales it
                    grayscale(width(), height());
                }
            });

        ImageIcon scaleIcon = new ImageIcon("scale-image.png");
        JMenuItem scaleImg = new JMenuItem("Scale Image", scaleIcon);
        editMenu.add(scaleImg);
        scaleImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //asks for height and width to create new image
                    ImageIcon widthIcon = new ImageIcon("LR-arrows.png");
                    String scaleWidth = (String)JOptionPane.showInputDialog(null,"What should the new width be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    ImageIcon heightIcon = new ImageIcon("UD-arrows.png");
                    String scaleHeight = (String)JOptionPane.showInputDialog(null,"What should the new height be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    //turns user input strings into doubles
                    double x = Double.parseDouble(scaleWidth);
                    double y = Double.parseDouble(scaleHeight);
                    //casts doubles as ints
                    int newWidth = (int)x;
                    int newHeight = (int)y;
                    //resizes frame to fit new image dimensions
                    frame.setSize(newWidth, newHeight);
                    //calls scale method to resize image using given dimensions
                    scale(newWidth, newHeight);
                }
            });
        ImageIcon rotateIcon = new ImageIcon("rotate-image.png");
        JMenuItem rotateImg = new JMenuItem("Rotate Image", rotateIcon);
        editMenu.add(rotateImg);
        rotateImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                }
            });

        //paint the frame
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }

    // convert to grayscale
    public void grayscale(int width, int height) {
        // create a grayscale image with original dimensions
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

        // convert colored image to grayscale
        ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
        grayScale.filter(image,image2);
        imageLabel.setIcon(new ImageIcon(image2));
        //getImage();
        image = image2;
    }

    //scales image by a given factor
    public void scale(int width, int height){
        //uses user-input dimensions to create new image
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image2.createGraphics();
        //gets new dimensions and resizes image
        g.drawImage(image, 0, 0, image2.getWidth(), image2.getHeight(), 0, 0, width(), height(), null);
        imageLabel.setIcon(new ImageIcon(image2));
        //getImage();
        image = image2;
    }

    //rotates the image
    public void rotate(int width, int height, int theta) {

    }

    public static void main(String[] args) {
        Picture p = new Picture();
        p.show();
    }
}

Picture

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class Picture{
    JFileChooser fileChooser = new JFileChooser();
    final JFrame frame = new JFrame("Edit Image");
    Container content;
    static BufferedImage image;
    BufferedImage image2;
    JLabel imageLabel;

    public Picture() {
        //asks for image file as input
        fileChooser.setDialogTitle("Choose an image file to begin:");
        fileChooser.showOpenDialog(frame);
        File selectedFile = fileChooser.getSelectedFile();
        if (fileChooser.getSelectedFile() != null) {
            try {
                //reads File as image
                image = ImageIO.read(selectedFile);
            }
            catch (IOException e) {
                System.out.println("Invalid image file: " + selectedFile);
                System.exit(0);
            }
        }
        else {
            System.out.println("No File Selected!");
        }
    }

    public int width() {
        //returns width of present image
        int width = image.getWidth();
        return width;
    }

    public int height() {
        //returns height of present image
        int height = image.getHeight();
        return height;
    }
/*
    public void getImage() {
        this.image2 = image;
    }
*/
    public void saveImage() {
        //saves current image as JPEG
        fileChooser.setDialogTitle("Save this image?");
        fileChooser.showSaveDialog(frame);
        try {
            //writes new file
            ImageIO.write(this.image, "JPG", fileChooser.getSelectedFile());
        }
        catch (IOException f) {
            System.out.println("Saving failed! Could not save image.");
        }
    }

    public void show() {
        //set frame title, set it visible, etc
        content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        //add the image to the frame
        ImageIcon icon = new ImageIcon(image);
        imageLabel = new JLabel(icon);
        frame.setContentPane(imageLabel);

        //add a menubar on the frame with a single option: saving the image
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu progName = new JMenu("Edit Image");
        progName.setBackground(Color.RED);
        menuBar.add(progName);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        JMenu editMenu = new JMenu("Edit");
        menuBar.add(editMenu);

        ImageIcon exitIcon = new ImageIcon("app-exit.png");
        JMenuItem exitAction = new JMenuItem("Exit", exitIcon);
        progName.add(exitAction);
        exitAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then exits
                        saveImage();
                        System.exit(0);
                    }
                    else {
                        //exits without saving
                        System.exit(0);
                    }
                }
            });

        ImageIcon newIcon = new ImageIcon("new-image.png");
        JMenuItem newAction = new JMenuItem("Open Image", newIcon);
        fileMenu.add(newAction);
        newAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then asks asks for new image file
                        saveImage();
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                    else {
                        //asks for new image file since user did not want to save original
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                }
            });

        ImageIcon saveIcon = new ImageIcon("save-image.png");
        JMenuItem saveAction = new JMenuItem("Save Image As...", saveIcon);
        fileMenu.add(saveAction);
        saveAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //opens save image method
                    saveImage();
                }
            });
        ImageIcon gsIcon = new ImageIcon("grayscale-image.png");
        JMenuItem grayScale = new JMenuItem("Grayscale", gsIcon);
        editMenu.add(grayScale);
        grayScale.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //grabs height and width of image, then grayscales it
                    grayscale(width(), height());
                }
            });

        ImageIcon scaleIcon = new ImageIcon("scale-image.png");
        JMenuItem scaleImg = new JMenuItem("Scale Image", scaleIcon);
        editMenu.add(scaleImg);
        scaleImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //asks for height and width to create new image
                    ImageIcon widthIcon = new ImageIcon("LR-arrows.png");
                    String scaleWidth = (String)JOptionPane.showInputDialog(null,"What should the new width be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    ImageIcon heightIcon = new ImageIcon("UD-arrows.png");
                    String scaleHeight = (String)JOptionPane.showInputDialog(null,"What should the new height be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    //turns user input strings into doubles
                    double x = Double.parseDouble(scaleWidth);
                    double y = Double.parseDouble(scaleHeight);
                    //casts doubles as ints
                    int newWidth = (int)x;
                    int newHeight = (int)y;
                    //resizes frame to fit new image dimensions
                    frame.setSize(newWidth, newHeight);
                    //calls scale method to resize image using given dimensions
                    scale(newWidth, newHeight);
                }
            });
        ImageIcon rotateIcon = new ImageIcon("rotate-image.png");
        JMenuItem rotateImg = new JMenuItem("Rotate Image", rotateIcon);
        editMenu.add(rotateImg);
        rotateImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                }
            });

        //paint the frame
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }

    // convert to grayscale
    public void grayscale(int width, int height) {
        // create a grayscale image with original dimensions
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

        // convert colored image to grayscale
        ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
        grayScale.filter(image,image2);
        imageLabel.setIcon(new ImageIcon(image2));
        //getImage();
        image = image2;
    }

    //scales image by a given factor
    public void scale(int width, int height){
        //uses user-input dimensions to create new image
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image2.createGraphics();
        //gets new dimensions and resizes image
        g.drawImage(image, 0, 0, image2.getWidth(), image2.getHeight(), 0, 0, width(), height(), null);
        imageLabel.setIcon(new ImageIcon(image2));
        //getImage();
        image = image2;
    }

    //rotates the image
    public void rotate(int width, int height, int theta) {

    }

    public static void main(String[] args) {
        Picture p = new Picture();
        p.show();
    }
}
慢慢从新开始 2024-12-17 06:57:31
    public void getImage() {
        this.image2 = image;
    }

    //..............................................

    // convert to grayscale
    public void grayscale(int width, int height) {
    // create a grayscale image with original dimensions
    image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

    // convert colored image to grayscale
    ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
    grayScale.filter(image,image2);
    imageLabel.setIcon(new ImageIcon(image2));
    getImage();
}

ColorConvertOp.filter(BufferedImage src, BufferedImage dest)

您的灰度方法将 image2 作为目标,当您调用 getImage() 时,它会用原始图像替换 image2 ,而不将原始图像设置为新的 image2 。

让 Grayscale(int, int) 返回 BuffereImage 或将 getImage 更改为:

public void getImage() {
    this.image = image2;
}
    public void getImage() {
        this.image2 = image;
    }

    //..............................................

    // convert to grayscale
    public void grayscale(int width, int height) {
    // create a grayscale image with original dimensions
    image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

    // convert colored image to grayscale
    ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
    grayScale.filter(image,image2);
    imageLabel.setIcon(new ImageIcon(image2));
    getImage();
}

ColorConvertOp.filter(BufferedImage src, BufferedImage dest)

Your grayscale method has image2 as the dest, when you call getImage() it replaces image2 with the original not settieng the original to your new image2.

Either have grayscale(int, int) return a BuffereImage or change getImage to:

public void getImage() {
    this.image = image2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文