Browse Source

copy Image only when Image has changed; show current Image when opened

Kevin Trometer 7 years ago
parent
commit
dba833d09a
2 changed files with 32 additions and 9 deletions
  1. 28 8
      src/ui/view/BackgroundPopUp.java
  2. 4 1
      src/ui/view/MyCanvas.java

+ 28 - 8
src/ui/view/BackgroundPopUp.java

@@ -29,6 +29,7 @@ import javax.swing.JOptionPane;
 import javax.swing.JRadioButton;
 import javax.swing.SwingConstants;
 import java.awt.GridLayout;
+import java.awt.Image;
 import java.awt.Color;
 import java.awt.FlowLayout;
 
@@ -51,6 +52,7 @@ public class BackgroundPopUp extends JDialog {
 	private final JButton btnOK = new JButton("OK");
 	private final JLabel lblImage = new JLabel();
 
+	private boolean imageChanged = false;
 	private ImageIcon icon = null;
 	private double imgScale = 1;
 	private final JButton btnCancel = new JButton("Cancel");
@@ -62,6 +64,7 @@ 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();
@@ -72,6 +75,20 @@ public class BackgroundPopUp extends JDialog {
 		super((java.awt.Frame) null, true);
 		getContentPane().setBackground(Color.WHITE);
 		this.setTitle("Set View Background");
+		// Show background Image
+		if (canvas != null) {
+			path = canvas.getBackgroundPath();
+		} else if (uNode != null) {
+			path = uNode.getImagePath();
+		}
+		if (!path.isEmpty()) {
+			textPath.setText(path);
+			icon = new ImageIcon(path);
+			imageWidth.setText("" + icon.getIconWidth());
+			imageHeight.setText("" + icon.getIconHeight());
+		}
+		this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
+				.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
 		setBounds(100, 100, 600, 340);
 
 		panelBrowse.setBorder(null);
@@ -134,6 +151,8 @@ public class BackgroundPopUp extends JDialog {
 					imageWidth.setText("" + icon.getIconWidth());
 					imageHeight.setText("" + icon.getIconHeight());
 
+					imageChanged = true;
+
 					// Calculate the Image scale to fit the Panel
 					if (Math.min(panelImageRadio.getWidth() - panelRadio.getWidth(),
 							panelImageRadio.getHeight()) == (panelImageRadio.getWidth() - panelRadio.getWidth())) {
@@ -163,7 +182,7 @@ public class BackgroundPopUp extends JDialog {
 
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				//Bild ausgewählt?
+				// Bild ausgewählt?
 				if (!path.isEmpty()) {
 					if (rdbtnImagePixel.isSelected()) {
 						mode = 0;
@@ -174,21 +193,22 @@ public class BackgroundPopUp extends JDialog {
 					}
 
 					if (canvas != null) {
-						copieImage();
+						if (imageChanged) {
+							copieImage();
+						}
 						canvas.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
 								Integer.parseInt(imageHeight.getText()));
 						canvas.repaint();
 					} else if (uNode != null) {
-						copieImage();
+						if (imageChanged) {
+							copieImage();
+						}
 						uNode.setBackgroundImage(path, mode, Integer.parseInt(imageWidth.getText()),
 								Integer.parseInt(imageHeight.getText()));
 					}
 					dispose();
 				} else {
-					JOptionPane.showMessageDialog(null,
-						    "No image selected!",
-						    "Warning!",
-						    JOptionPane.WARNING_MESSAGE);
+					JOptionPane.showMessageDialog(null, "No image selected!", "Warning!", JOptionPane.WARNING_MESSAGE);
 				}
 			}
 		});
@@ -246,7 +266,7 @@ public class BackgroundPopUp extends JDialog {
 				inStream.close();
 			if (outStream != null)
 				outStream.close();
-			
+
 		} catch (IOException eex) {
 			eex.printStackTrace();
 		}

+ 4 - 1
src/ui/view/MyCanvas.java

@@ -402,7 +402,6 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 
-		controller.addTextToConsole(imgPath);
 		// Paint the Background
 		if (!imgPath.isEmpty()) {
 			img = new ImageIcon(imgPath).getImage();
@@ -1156,4 +1155,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		backgroundWidth = width;
 		backgroundHeight = height;
 	}
+	
+	public String getBackgroundPath(){
+		return imgPath;
+	}
 }