Browse Source

background image finished for main canvas - Image is not copied

Kevin Trometer 7 years ago
parent
commit
8a98875405
2 changed files with 57 additions and 15 deletions
  1. 43 12
      src/ui/view/BackgroundPopUp.java
  2. 14 3
      src/ui/view/MyCanvas.java

+ 43 - 12
src/ui/view/BackgroundPopUp.java

@@ -5,6 +5,7 @@ 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;
@@ -22,6 +23,7 @@ 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.
@@ -53,12 +55,15 @@ public class BackgroundPopUp extends JDialog {
 	// 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, 400, 250);
+		setBounds(100, 100, 600, 340);
 
 		panelBrowse.setBorder(null);
 		panelImageRadio.setBorder(null);
@@ -73,7 +78,10 @@ public class BackgroundPopUp extends JDialog {
 		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);
 
@@ -81,11 +89,24 @@ public class BackgroundPopUp extends JDialog {
 		rdbtnStretched.setBackground(Color.WHITE);
 
 		panelRadio.add(rdbtnStretched);
-		rdbtnCustom.setBackground(Color.WHITE);
-
-		panelRadio.add(rdbtnCustom);
 		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);
@@ -101,7 +122,9 @@ public class BackgroundPopUp extends JDialog {
 					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())) {
@@ -112,9 +135,9 @@ public class BackgroundPopUp extends JDialog {
 								/ panelImageRadio.getHeight();
 					}
 
-					lblImage.setIcon(new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(
-							(int) (icon.getIconWidth() / imgScale), (int) (icon.getIconHeight() / imgScale),
-							java.awt.Image.SCALE_FAST)));
+					lblImage.setIcon(new ImageIcon(
+							new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
+									(int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_FAST)));
 				}
 			}
 		});
@@ -131,8 +154,16 @@ public class BackgroundPopUp extends JDialog {
 
 			@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, icon.getIconWidth(), icon.getIconHeight());
+					canvas.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()), Integer.parseInt(imageHeight.getText()));
 					canvas.repaint();
 				} else if (uNode != null) {
 
@@ -163,9 +194,9 @@ public class BackgroundPopUp extends JDialog {
 								/ panelImageRadio.getHeight();
 					}
 
-					lblImage.setIcon(new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(
-							(int) (icon.getIconWidth() / imgScale), (int) (icon.getIconHeight() / imgScale),
-							java.awt.Image.SCALE_SMOOTH)));
+					lblImage.setIcon(new ImageIcon(
+							new ImageIcon(path).getImage().getScaledInstance((int) (icon.getIconWidth() / imgScale),
+									(int) (icon.getIconHeight() / imgScale), java.awt.Image.SCALE_SMOOTH)));
 				}
 			}
 		});

+ 14 - 3
src/ui/view/MyCanvas.java

@@ -402,11 +402,22 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 
-		//Paint the Background
+		// Paint the Background
 		if (!imgPath.isEmpty()) {
-			
 			img = new ImageIcon(imgPath).getImage();
-			g2.drawImage(img, 0, 0, backgroundWidth, backgroundHeight, null);
+			switch (backgroundMode) {
+			case BackgroundPopUp.IMAGE_PIXELS:
+				g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
+				break;
+			case BackgroundPopUp.STRETCHED:
+				g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(), null);
+				break;
+			case BackgroundPopUp.CUSTOM:
+				g2.drawImage(img, 0, 0, backgroundWidth, backgroundHeight, null);
+				break;
+			default:
+				break;
+			}
 		}
 
 		// Test SubNet Coloring