2011-10-15 11 views
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(); 
    } 
} 
+0

그 코드는 컴파일되지 않습니다 :

중 하나는 그레이 스케일 (INT, INT)을 BuffereImage을 반환하거나 된 GetImage을 변경할 수 있습니다. 코드가 실제로 사용 되나요? –

+0

@AndrewThompson 컴파일해야합니다 ... 글자 그대로 IDE에서 복사/붙여 넣기했습니다. 완벽하게 실행됩니다. 이후 몇 가지 GUI 측면을 업데이트했습니다. 게시물을 편집하고 새 코드를 붙여 넣습니다. –

+0

우리는 질문에 대한 편집에 대한 정보를받지 못했습니다. (사람들에게 편집이 일어 났음을 알리는 것이 좋습니다.) –

답변

3

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(); 
    } 
} 
+0

완벽하고 잘 수행되었습니다. –

1
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의 이명 령)

귀하의 그레이 스케일 방법은 당신이 된 GetImage을()를 호출 할 때 원래가 원본을 settieng하지와 함께 이미지 2를 대체의 이명 령으로 이미지 2있다 당신의 새로운 이미지 2.

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