Advertisement
Guest User

Untitled

a guest
Jun 25th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.30 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.JButton;
  5. import javax.swing.JCheckBox;
  6. import javax.swing.JComboBox;
  7. import javax.swing.JFormattedTextField;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JPanel;
  12. import javax.swing.UIManager;
  13. import javax.swing.border.BevelBorder;
  14. import javax.swing.border.SoftBevelBorder;
  15. import javax.swing.border.TitledBorder;
  16. import javax.swing.event.DocumentEvent;
  17. import javax.swing.event.DocumentListener;
  18.  
  19.  
  20. public class GUI {
  21.    private JButton imageSizeBtnCancel;
  22.    private JFrame frameimgSize;
  23.    private JFormattedTextField widthArea;
  24.    private JFormattedTextField heightArea;
  25.    private JComboBox comboBoxWidth;
  26.    private JCheckBox checkBoxImage;
  27.    private JComboBox comboBoxHeight;
  28.    private double aspectRatio = 16/9;
  29.    public GUI(){
  30.        
  31.           frameimgSize = new JFrame("Image Size");
  32.           frameimgSize.setLocationRelativeTo(null);
  33.           frameimgSize.setAlwaysOnTop(true);
  34.           frameimgSize.setSize(400, 200);
  35.           frameimgSize.setDefaultCloseOperation(frameimgSize.DISPOSE_ON_CLOSE);
  36.           frameimgSize.setResizable(false);
  37.           frameimgSize.getContentPane().setLayout(null);
  38.           frameimgSize.setVisible(true);
  39.          
  40.           JPanel panel = new JPanel();
  41.           panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Image resize", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  42.           panel.setBounds(0, 11, 283, 150);
  43.           frameimgSize.getContentPane().add(panel);
  44.           panel.setLayout(null);
  45.           JLabel widthLabel = new JLabel("Width:");
  46.           widthLabel.setBounds(17, 40, 38, 20);
  47.           panel.add(widthLabel);
  48.          
  49.           JLabel heightLabel = new JLabel("Height:");
  50.           heightLabel.setBounds(17, 77, 39, 20);
  51.           panel.add(heightLabel);
  52.          
  53.           JPanel panel_1 = new JPanel();
  54.           panel_1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
  55.           panel_1.setBounds(62, 40, 90, 20);
  56.           panel.add(panel_1);
  57.           panel_1.setLayout(null);
  58.          
  59.           widthArea = new JFormattedTextField("");
  60.           widthArea.setBounds(3, 4, 85, 16);
  61.           panel_1.add(widthArea);
  62.          
  63.           JPanel panel_2 = new JPanel();
  64.           panel_2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
  65.           panel_2.setBounds(62, 74, 90, 20);
  66.           panel.add(panel_2);
  67.           panel_2.setLayout(null);
  68.          
  69.           heightArea = new JFormattedTextField("");
  70.           heightArea.setBounds(3, 4, 85, 16);
  71.           panel_2.add(heightArea);
  72.          
  73.           checkBoxImage = new JCheckBox("Constrain Proportions");
  74.           checkBoxImage.setBounds(12, 109, 235, 23);
  75.           checkBoxImage.setSelected(true);
  76.           panel.add(checkBoxImage);
  77.          
  78.           comboBoxWidth = new JComboBox();
  79.           comboBoxWidth.addItem("Pixels");
  80.           comboBoxWidth.addItem("Percent");      
  81.           comboBoxWidth.setBounds(181, 40, 72, 20);
  82.           panel.add(comboBoxWidth);
  83.          
  84.           comboBoxHeight = new JComboBox();
  85.           comboBoxHeight.addItem("Pixels");
  86.           comboBoxHeight.addItem("Percent");
  87.          
  88.           comboBoxHeight.setBounds(181, 77, 72, 20);
  89.           panel.add(comboBoxHeight);
  90.          
  91.           JButton imageSizebtnOK = new JButton("OK");
  92.           imageSizebtnOK.setBounds(295, 21, 89, 23);
  93.           imageSizebtnOK.addActionListener(new sizeImageOK() );
  94.           frameimgSize.getContentPane().add(imageSizebtnOK);
  95.          
  96.           JButton imageSizeBtnCancel = new JButton("Cancel");
  97.           imageSizeBtnCancel.setBounds(295, 61, 89, 23);
  98.           imageSizeBtnCancel.addActionListener(new sizeImageCancel());
  99.  
  100.           frameimgSize.getContentPane().add(imageSizeBtnCancel);
  101.           // retrieving AR for current selected image
  102.  
  103.           heightArea.getDocument().addDocumentListener(new sizeImageWidthUpdate() );
  104.           widthArea.getDocument().addDocumentListener(new sizeImageHeightUpdate() );
  105.    }
  106.    
  107.    //--------METHODS
  108.    private void updateHeight(){
  109.          
  110.           if(widthArea.getText().length() != 0 && widthArea.getText().length() <= 5 &&
  111.                   widthArea.getText().charAt(0) != '0' && checkBoxImage.isSelected()){
  112.               String width = widthArea.getText();
  113.               try{
  114.                   int widthNum = Integer.parseInt(width);
  115.                   int heightNum = (int) Math.round(aspectRatio * widthNum);
  116.                   heightArea.setText(String.valueOf(heightNum) );
  117.                   System.out.println("Height should be updated");
  118.               }
  119.               catch(NumberFormatException e1){JOptionPane.showMessageDialog(new JOptionPane(),e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);}
  120.           }
  121.       }
  122.       // image resizing, width updating method for keeping AR
  123.       private void updateWidth(){
  124.          
  125.           if (checkBoxImage.isSelected()){
  126.               String height = heightArea.getText();
  127.               /**
  128.                * to do, update width, height area
  129.                * to the closest user input
  130.                */
  131.               if(heightArea.getText().length() != 0 && heightArea.getText().length() <= 4
  132.                       && heightArea.getText().charAt(0) != '0'){
  133.                   //parsing string to integer
  134.                   try{
  135.                       int heightNum = Integer.parseInt(height);
  136.                       int widthNum = (int) Math.round(aspectRatio * heightNum);   // calculating required width to keep AR
  137.                       widthArea.setText(String.valueOf(widthNum) );
  138.                       System.out.println("width should be updated.");
  139.                   }
  140.                   catch(NumberFormatException e1){JOptionPane.showMessageDialog(new JOptionPane(),e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);}
  141.               }
  142.           }
  143.       }
  144.    //-------LISTENERS----------
  145.    
  146.    public class sizeImageCancel implements ActionListener{
  147.           public void actionPerformed(ActionEvent e){
  148.               frameimgSize.dispose();
  149.           }
  150.       }
  151.       public class sizeImageOK implements ActionListener{
  152.           public void actionPerformed(ActionEvent e){
  153.               /**
  154.                * To DO : retrieve width and height and check whether it is valid.
  155.                * Check whether image is present for sizing.
  156.                * Resize image, dispose frame.
  157.                */
  158.              
  159.           }
  160.       }
  161.      
  162.       //if focus on width add this listeners
  163.       public class sizeImageHeightUpdate implements DocumentListener{
  164.           public void changedUpdate(DocumentEvent e){}
  165.           public void insertUpdate(DocumentEvent e){updateHeight();}
  166.           public void removeUpdate(DocumentEvent e){}
  167.       }
  168.       //else if focus on height add this
  169.       public class sizeImageWidthUpdate implements DocumentListener{
  170.           public void changedUpdate(DocumentEvent e){}
  171.           public void insertUpdate(DocumentEvent e){updateWidth(); }
  172.           public void removeUpdate(DocumentEvent e){}
  173.       }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement