Bläddra i källkod

setting the background works, uses the image size at the moment

Kevin Trometer 7 år sedan
förälder
incheckning
1b7d7ad828
4 ändrade filer med 179 tillägg och 37 borttagningar
  1. 102 21
      src/ui/view/BackgroundPopUp.java
  2. 11 9
      src/ui/view/GUI.java
  3. 34 7
      src/ui/view/MyCanvas.java
  4. 32 0
      src/ui/view/UpperNodeCanvas.java

+ 102 - 21
src/ui/view/BackgroundPopUp.java

@@ -2,7 +2,6 @@ package ui.view;
 
 import javax.swing.JDialog;
 import javax.swing.JFileChooser;
-import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import javax.swing.filechooser.FileNameExtensionFilter;
@@ -10,21 +9,19 @@ import javax.swing.ImageIcon;
 import javax.swing.JButton;
 import org.eclipse.wb.swing.FocusTraversalOnArray;
 
-import javafx.scene.image.Image;
-import javafx.stage.FileChooser;
+import classes.CpsUpperNode;
 
 import java.awt.Component;
-import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.io.File;
+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.BoxLayout;
 import javax.swing.SwingConstants;
-import java.awt.FlowLayout;
-import java.awt.Choice;
+import java.awt.GridLayout;
+import java.awt.Color;
 
 /**
  * Popup for setting the Background Image for the current View.
@@ -45,20 +42,53 @@ public class BackgroundPopUp extends JDialog {
 	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");
 
-	public BackgroundPopUp() {
+	// Modes
+	public static final int IMAGE_PIXELS = 0, STRETCHED = 1, CUSTOM = 2;
+	private int mode = 0;
+
+	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);
 
+		panelBrowse.setBorder(null);
+		panelImageRadio.setBorder(null);
+		panelOK.setBorder(null);
+		panelRadio.setBorder(null);
+
 		getContentPane().add(panelImageRadio, BorderLayout.CENTER);
-		panelImageRadio.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
-		lblImage.setHorizontalAlignment(SwingConstants.RIGHT);
+		panelImageRadio.setLayout(new BorderLayout(0, 0));
+		lblImage.setBackground(Color.WHITE);
+		lblImage.setHorizontalAlignment(SwingConstants.LEFT);
+
+		panelImageRadio.add(lblImage, BorderLayout.CENTER);
+
+		panelImageRadio.add(panelRadio, BorderLayout.EAST);
+		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);
+		rdbtnCustom.setBackground(Color.WHITE);
+
+		panelRadio.add(rdbtnCustom);
+		panelBrowse.setBackground(Color.WHITE);
 
-		panelImageRadio.add(lblImage);
 		getContentPane().add(panelBrowse, BorderLayout.NORTH);
-		panelBrowse.add(btnBrowse);
+		panelBrowse.setLayout(new BorderLayout(0, 0));
+		panelBrowse.add(btnBrowse, BorderLayout.WEST);
 
 		// Browse Row Functions
 		btnBrowse.addActionListener(new ActionListener() {
@@ -68,26 +98,77 @@ public class BackgroundPopUp extends JDialog {
 				FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
 				fileChooser.setFileFilter(filter);
 				if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
-					textPath.setText(fileChooser.getSelectedFile().getPath());
-					ImageIcon icon = new ImageIcon(textPath.getText());
-					imgScale = Math.max(icon.getIconWidth(), icon.getIconHeight()) / 200;
-					lblImage.setIcon(new ImageIcon(new ImageIcon(textPath.getText()).getImage().getScaledInstance(
+					path = fileChooser.getSelectedFile().getPath();
+					textPath.setText(path);
+					icon = new ImageIcon(textPath.getText());
+
+					// 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)));
+							java.awt.Image.SCALE_FAST)));
 				}
 			}
 		});
-
 		textPath.setEditable(false);
 
-		panelBrowse.add(textPath);
+		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 (canvas != null) {
+					canvas.setBackgroundImage(path, mode, icon.getIconWidth(), icon.getIconHeight());
+					canvas.repaint();
+				} else if (uNode != null) {
+
+				}
+				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)));
+				}
+			}
+		});
 	}
 
 }

+ 11 - 9
src/ui/view/GUI.java

@@ -516,8 +516,10 @@ public class GUI<E> implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				if (((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
 						.getComponent(0) instanceof UpperNodeCanvas)
-					controller.paste(((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
-							.getComponent(0)).upperNode, ((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
+					controller.paste(
+							((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
+									.getComponent(0)).upperNode,
+							((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
 									.getComponent(0)).getMousePosition());
 				else
 					controller.paste(null, canvas.getMousePosition());
@@ -536,8 +538,8 @@ public class GUI<E> implements CategoryListener {
 				if (!model.getSelectedCpsObjects().isEmpty()) {
 					if (((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
 							.getComponent(0) instanceof UpperNodeCanvas)
-					controller.cut(((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent())
-							.getViewport().getComponent(0)).upperNode);
+						controller.cut(((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent())
+								.getViewport().getComponent(0)).upperNode);
 					else
 						controller.cut(null);
 					if (!model.getClipboradObjects().isEmpty()) {
@@ -675,20 +677,20 @@ public class GUI<E> implements CategoryListener {
 				canvas.repaint();
 			}
 		});
-		
+
 		mnNewMenuView.add(canvasSize);
-		
+
 		mnNewMenuView.add(background);
 		background.addActionListener(new ActionListener() {
-			
+
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				BackgroundPopUp backgroundDialog = new BackgroundPopUp();
+				BackgroundPopUp backgroundDialog = new BackgroundPopUp(canvas, null);
 				backgroundDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 				backgroundDialog.setVisible(true);
 			}
 		});
-		
+
 		splitPane3.setRightComponent(sizeSlider);
 
 		splitPane3.setLeftComponent(lblImageSize);

+ 34 - 7
src/ui/view/MyCanvas.java

@@ -93,6 +93,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 	private Point mousePosition = new Point(); // Mouse Position when
 												// rightclicked
 
+	// Animation Stuff
 	javax.swing.Timer animT; // animation Timer
 	private final int ANIMTIME = 500; // animation Time
 
@@ -101,8 +102,12 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 	private int animDuration = ANIMTIME; // animation Duration
 	private int animDelay = 1000 / animFPS; // animation Delay
 	private int animSteps = animDuration / animDelay; // animation Steps;
-	private long start = 0; // for time
-	private long elapsedTime = 0; // outprint
+
+	// Background Image
+	private String imgPath = "";
+	private int backgroundMode = 0;
+	private int backgroundWidth = 0;
+	private int backgroundHeight = 0;
 
 	// contains the value of the Capacity for new created Edges
 
@@ -355,7 +360,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		itemCut.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				controller.cut(null);;
+				controller.cut(null);
+				;
 				itemPaste.setEnabled(true);
 				repaint();
 			}
@@ -396,8 +402,12 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 
-		img = new ImageIcon(this.getClass().getResource("/Images/Darmstadt.JPG")).getImage();
-		g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(), null);
+		//Paint the Background
+		if (!imgPath.isEmpty()) {
+			
+			img = new ImageIcon(imgPath).getImage();
+			g2.drawImage(img, 0, 0, backgroundWidth, backgroundHeight, null);
+		}
 
 		// Test SubNet Coloring
 		int i = 0;
@@ -693,7 +703,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					} else {
 						controller.addSelectedObject(tempCps);
 					}
-					
+
 				}
 
 				// If drawing an Edge (CTRL down)
@@ -750,7 +760,6 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			controller.addSelectedObject(tempCps);
 		}
 
-
 		dragged = false;
 
 		// Rightclick List
@@ -1117,4 +1126,22 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		return showedInformation;
 	}
 
+	/**
+	 * Set the Background Image;
+	 * 
+	 * @param imagePath
+	 *            Image Path
+	 * @param mode
+	 *            Image Mode
+	 * @param width
+	 *            Image custom width
+	 * @param height
+	 *            Image custom height
+	 */
+	public void setBackgroundImage(String imagePath, int mode, int width, int height) {
+		imgPath = imagePath;
+		backgroundMode = mode;
+		backgroundWidth = width;
+		backgroundHeight = height;
+	}
 }

+ 32 - 0
src/ui/view/UpperNodeCanvas.java

@@ -101,6 +101,7 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 
 	private UpdateController updCon;
 
+	// Animation Stuff
 	javax.swing.Timer animT; // animation Timer
 	private final int ANIMTIME = 500; // animation Time
 
@@ -112,6 +113,12 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 	private Position unPos;
 	private ArrayList<Position> savePos;
 
+	// Background Image
+	private String imgPath = "";
+	private int backgroundMode = 0;
+	private int backgroundWidth = 0;
+	private int backgroundHeight = 0;
+
 	/**
 	 * Constructor.
 	 * 
@@ -410,6 +417,12 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 
+		// Paint the Background
+		if (!imgPath.isEmpty()) {
+			img = new ImageIcon(imgPath).getImage();
+			g2.drawImage(img, borderPos, 0, model.getCanvasX(), model.getCanvasY(), null);
+		}
+
 		// Left Border
 		borderPos = (int) (model.getScale() + scalediv20 + scalediv20 + 10);
 		g2.setColor(new Color(230, 230, 230));
@@ -1433,4 +1446,23 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		return showedInformation;
 	}
 
+	/**
+	 * Set the Background Image;
+	 * 
+	 * @param imagePath
+	 *            Image Path
+	 * @param mode
+	 *            Image Mode
+	 * @param width
+	 *            Image custom width
+	 * @param height
+	 *            Image custom height
+	 */
+	public void setBackgroundImage(String imagePath, int mode, int width, int height) {
+		imgPath = imagePath;
+		backgroundMode = mode;
+		backgroundWidth = width;
+		backgroundHeight = height;
+	}
+
 }