Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JComboBox;
- import javax.swing.JFormattedTextField;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.UIManager;
- import javax.swing.border.BevelBorder;
- import javax.swing.border.SoftBevelBorder;
- import javax.swing.border.TitledBorder;
- import javax.swing.event.DocumentEvent;
- import javax.swing.event.DocumentListener;
- public class GUI {
- private JButton imageSizeBtnCancel;
- private JFrame frameimgSize;
- private JFormattedTextField widthArea;
- private JFormattedTextField heightArea;
- private JComboBox comboBoxWidth;
- private JCheckBox checkBoxImage;
- private JComboBox comboBoxHeight;
- private double aspectRatio = 16/9;
- public GUI(){
- frameimgSize = new JFrame("Image Size");
- frameimgSize.setLocationRelativeTo(null);
- frameimgSize.setAlwaysOnTop(true);
- frameimgSize.setSize(400, 200);
- frameimgSize.setDefaultCloseOperation(frameimgSize.DISPOSE_ON_CLOSE);
- frameimgSize.setResizable(false);
- frameimgSize.getContentPane().setLayout(null);
- frameimgSize.setVisible(true);
- JPanel panel = new JPanel();
- panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Image resize", TitledBorder.LEADING, TitledBorder.TOP, null, null));
- panel.setBounds(0, 11, 283, 150);
- frameimgSize.getContentPane().add(panel);
- panel.setLayout(null);
- JLabel widthLabel = new JLabel("Width:");
- widthLabel.setBounds(17, 40, 38, 20);
- panel.add(widthLabel);
- JLabel heightLabel = new JLabel("Height:");
- heightLabel.setBounds(17, 77, 39, 20);
- panel.add(heightLabel);
- JPanel panel_1 = new JPanel();
- panel_1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
- panel_1.setBounds(62, 40, 90, 20);
- panel.add(panel_1);
- panel_1.setLayout(null);
- widthArea = new JFormattedTextField("");
- widthArea.setBounds(3, 4, 85, 16);
- panel_1.add(widthArea);
- JPanel panel_2 = new JPanel();
- panel_2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
- panel_2.setBounds(62, 74, 90, 20);
- panel.add(panel_2);
- panel_2.setLayout(null);
- heightArea = new JFormattedTextField("");
- heightArea.setBounds(3, 4, 85, 16);
- panel_2.add(heightArea);
- checkBoxImage = new JCheckBox("Constrain Proportions");
- checkBoxImage.setBounds(12, 109, 235, 23);
- checkBoxImage.setSelected(true);
- panel.add(checkBoxImage);
- comboBoxWidth = new JComboBox();
- comboBoxWidth.addItem("Pixels");
- comboBoxWidth.addItem("Percent");
- comboBoxWidth.setBounds(181, 40, 72, 20);
- panel.add(comboBoxWidth);
- comboBoxHeight = new JComboBox();
- comboBoxHeight.addItem("Pixels");
- comboBoxHeight.addItem("Percent");
- comboBoxHeight.setBounds(181, 77, 72, 20);
- panel.add(comboBoxHeight);
- JButton imageSizebtnOK = new JButton("OK");
- imageSizebtnOK.setBounds(295, 21, 89, 23);
- imageSizebtnOK.addActionListener(new sizeImageOK() );
- frameimgSize.getContentPane().add(imageSizebtnOK);
- JButton imageSizeBtnCancel = new JButton("Cancel");
- imageSizeBtnCancel.setBounds(295, 61, 89, 23);
- imageSizeBtnCancel.addActionListener(new sizeImageCancel());
- frameimgSize.getContentPane().add(imageSizeBtnCancel);
- // retrieving AR for current selected image
- heightArea.getDocument().addDocumentListener(new sizeImageWidthUpdate() );
- widthArea.getDocument().addDocumentListener(new sizeImageHeightUpdate() );
- }
- //--------METHODS
- private void updateHeight(){
- if(widthArea.getText().length() != 0 && widthArea.getText().length() <= 5 &&
- widthArea.getText().charAt(0) != '0' && checkBoxImage.isSelected()){
- String width = widthArea.getText();
- try{
- int widthNum = Integer.parseInt(width);
- int heightNum = (int) Math.round(aspectRatio * widthNum);
- heightArea.setText(String.valueOf(heightNum) );
- System.out.println("Height should be updated");
- }
- catch(NumberFormatException e1){JOptionPane.showMessageDialog(new JOptionPane(),e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);}
- }
- }
- // image resizing, width updating method for keeping AR
- private void updateWidth(){
- if (checkBoxImage.isSelected()){
- String height = heightArea.getText();
- /**
- * to do, update width, height area
- * to the closest user input
- */
- if(heightArea.getText().length() != 0 && heightArea.getText().length() <= 4
- && heightArea.getText().charAt(0) != '0'){
- //parsing string to integer
- try{
- int heightNum = Integer.parseInt(height);
- int widthNum = (int) Math.round(aspectRatio * heightNum); // calculating required width to keep AR
- widthArea.setText(String.valueOf(widthNum) );
- System.out.println("width should be updated.");
- }
- catch(NumberFormatException e1){JOptionPane.showMessageDialog(new JOptionPane(),e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);}
- }
- }
- }
- //-------LISTENERS----------
- public class sizeImageCancel implements ActionListener{
- public void actionPerformed(ActionEvent e){
- frameimgSize.dispose();
- }
- }
- public class sizeImageOK implements ActionListener{
- public void actionPerformed(ActionEvent e){
- /**
- * To DO : retrieve width and height and check whether it is valid.
- * Check whether image is present for sizing.
- * Resize image, dispose frame.
- */
- }
- }
- //if focus on width add this listeners
- public class sizeImageHeightUpdate implements DocumentListener{
- public void changedUpdate(DocumentEvent e){}
- public void insertUpdate(DocumentEvent e){updateHeight();}
- public void removeUpdate(DocumentEvent e){}
- }
- //else if focus on height add this
- public class sizeImageWidthUpdate implements DocumentListener{
- public void changedUpdate(DocumentEvent e){}
- public void insertUpdate(DocumentEvent e){updateWidth(); }
- public void removeUpdate(DocumentEvent e){}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement