123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package ui.view;
- import javax.swing.JDialog;
- import javax.swing.JFileChooser;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import javax.swing.ButtonGroup;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import org.eclipse.wb.swing.FocusTraversalOnArray;
- import classes.CpsUpperNode;
- import java.awt.Component;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ComponentAdapter;
- import java.awt.event.ComponentEvent;
- import java.awt.BorderLayout;
- import javax.swing.JLabel;
- import javax.swing.JRadioButton;
- import javax.swing.SwingConstants;
- import java.awt.GridLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- /**
- * Popup for setting the Background Image for the current View.
- **/
- public class BackgroundPopUp extends JDialog {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private final JTextField textPath = new JTextField("");
- private final JButton btnBrowse = new JButton("Browse");
- private JPanel panelImageRadio = new JPanel();
- private String path = "";
- private final JPanel panelBrowse = new JPanel();
- private final JPanel panelOK = new JPanel();
- private final JButton btnOK = new JButton("OK");
- private final JLabel lblImage = new JLabel();
- private ImageIcon icon = null;
- private double imgScale = 1;
- private final JButton btnCancel = new JButton("Cancel");
- private final JPanel panelRadio = new JPanel();
- private final JRadioButton rdbtnImagePixel = new JRadioButton("Use Image Size");
- private final JRadioButton rdbtnStretched = new JRadioButton("Strech Image");
- private final JRadioButton rdbtnCustom = new JRadioButton("Custom Size");
- // Modes
- public static final int IMAGE_PIXELS = 0, STRETCHED = 1, CUSTOM = 2;
- private int mode = 0;
- private final JPanel panel = new JPanel();
- private final JTextField imageWidth = new JTextField();
- private final JTextField imageHeight = new JTextField();
- public BackgroundPopUp(MyCanvas canvas, CpsUpperNode uNode) {
- super((java.awt.Frame) null, true);
- getContentPane().setBackground(Color.WHITE);
- this.setTitle("Set View Background");
- setBounds(100, 100, 600, 340);
- panelBrowse.setBorder(null);
- panelImageRadio.setBorder(null);
- panelOK.setBorder(null);
- panelRadio.setBorder(null);
- getContentPane().add(panelImageRadio, BorderLayout.CENTER);
- panelImageRadio.setLayout(new BorderLayout(0, 0));
- lblImage.setBackground(Color.WHITE);
- lblImage.setHorizontalAlignment(SwingConstants.LEFT);
- panelImageRadio.add(lblImage, BorderLayout.CENTER);
- panelImageRadio.add(panelRadio, BorderLayout.EAST);
- // Radio Buttons
- panelRadio.setLayout(new GridLayout(0, 1, 0, 0));
- rdbtnImagePixel.setBackground(Color.WHITE);
- rdbtnImagePixel.setSelected(true);
- panelRadio.add(rdbtnImagePixel);
- rdbtnStretched.setBackground(Color.WHITE);
- panelRadio.add(rdbtnStretched);
- panelBrowse.setBackground(Color.WHITE);
- ButtonGroup bgroup = new ButtonGroup();
- bgroup.add(rdbtnImagePixel);
- bgroup.add(rdbtnStretched);
- panelRadio.add(rdbtnCustom);
- rdbtnCustom.setBackground(new Color(255, 255, 255));
- bgroup.add(rdbtnCustom);
- panel.setBackground(Color.WHITE);
- panelRadio.add(panel);
- panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- panel.add(imageWidth);
- imageWidth.setColumns(10);
- panel.add(imageHeight);
- imageHeight.setColumns(10);
- // Browse Panel
- getContentPane().add(panelBrowse, BorderLayout.NORTH);
- panelBrowse.setLayout(new BorderLayout(0, 0));
- panelBrowse.add(btnBrowse, BorderLayout.WEST);
- // Browse Row Functions
- btnBrowse.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser fileChooser = new JFileChooser();
- FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
- fileChooser.setFileFilter(filter);
- if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
- path = fileChooser.getSelectedFile().getPath();
- textPath.setText(path);
- icon = new ImageIcon(textPath.getText());
- imageWidth.setText(""+icon.getIconWidth());
- imageHeight.setText(""+icon.getIconHeight());
-
- // Calculate the Image scale to fit the Panel
- if (Math.min(panelImageRadio.getWidth() - panelRadio.getWidth(),
- panelImageRadio.getHeight()) == (panelImageRadio.getWidth() - panelRadio.getWidth())) {
- imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
- / panelImageRadio.getWidth();
- } else {
- imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
- / panelImageRadio.getHeight();
- }
- lblImage.setIcon(new ImageIcon(
- new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
- (int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_FAST)));
- }
- }
- });
- textPath.setEditable(false);
- panelBrowse.add(textPath, BorderLayout.CENTER);
- textPath.setColumns(20);
- panelImageRadio.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[] { btnBrowse, textPath }));
- panelOK.setBackground(Color.WHITE);
- getContentPane().add(panelOK, BorderLayout.SOUTH);
- panelOK.add(btnOK);
- btnOK.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (rdbtnImagePixel.isSelected()) {
- mode = 0;
- } else if (rdbtnStretched.isSelected()) {
- mode = 1;
- } else if (rdbtnCustom.isSelected()) {
- mode = 2;
- }
- if (canvas != null) {
- canvas.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()), Integer.parseInt(imageHeight.getText()));
- canvas.repaint();
- } else if (uNode != null) {
- uNode.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()), Integer.parseInt(imageHeight.getText()));
- }
- dispose();
- }
- });
- panelOK.add(btnCancel);
- btnCancel.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- dispose();
- }
- });
- this.addComponentListener(new ComponentAdapter() {
- @Override
- public void componentResized(ComponentEvent e) {
- if (icon != null) {
- // Calculate the Image scale to fit the Panel
- if (Math.min(panelImageRadio.getWidth() - panelRadio.getWidth(),
- panelImageRadio.getHeight()) == (panelImageRadio.getWidth() - panelRadio.getWidth())) {
- imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
- / panelImageRadio.getWidth();
- } else {
- imgScale = (double) Math.min(icon.getIconWidth(), icon.getIconHeight())
- / panelImageRadio.getHeight();
- }
- lblImage.setIcon(new ImageIcon(
- new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
- (int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_SMOOTH)));
- }
- }
- });
- }
- }
|