Browse Source

Canvas Drag and Drop

Kevin Trometer 8 years ago
parent
commit
c03b271ab0
3 changed files with 44 additions and 9 deletions
  1. BIN
      bin/ui/view/MyCanvas.class
  2. BIN
      res/Images/Thumbs.db
  3. 44 9
      src/ui/view/MyCanvas.java

BIN
bin/ui/view/MyCanvas.class


BIN
res/Images/Thumbs.db


+ 44 - 9
src/ui/view/MyCanvas.java

@@ -4,6 +4,7 @@ import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
 import java.util.LinkedList;
 
 import javax.swing.ImageIcon;
@@ -11,20 +12,22 @@ import javax.swing.JPanel;
 import ui.model.CpsObject;
 import ui.model.HolonObject;
 
-class MyCanvas extends JPanel implements MouseListener
+class MyCanvas extends JPanel implements MouseListener, MouseMotionListener 
 {
 	private Image img = null;      // Contains the image to draw on MyCanvas
 	private int x = 0;
 	private int y = 0;
 	LinkedList<CpsObject> choords = new LinkedList<>();
+	boolean dragging = false;
+	CpsObject tempCPS = null;
 	
     public MyCanvas()
     {
     	
     	img = new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage().getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
     	
-    	
         this.addMouseListener(this);
+        this.addMouseMotionListener(this);
     }
 
     public void paintComponent(Graphics g)
@@ -32,13 +35,18 @@ class MyCanvas extends JPanel implements MouseListener
         // Draws the image to the canvas
     	super.paintComponent(g);
     	for (CpsObject cps : choords){
-    		g.drawImage(img, cps.getPos().x, cps.getPos().y, null);
+    		g.drawImage(img, cps.getPos().x-15, cps.getPos().y-15, null);
     	}
     }
 
 	@Override
 	public void mouseClicked(MouseEvent e) {
 		// TODO Auto-generated method stub
+		HolonObject h = new HolonObject("Haus");
+        h.setPos(x, y);
+        
+    	choords.add(h);
+        System.out.println("Draw: "+e.getX()+" "+e.getY());
 	}
 
 	@Override
@@ -56,6 +64,17 @@ class MyCanvas extends JPanel implements MouseListener
 	@Override
 	public void mousePressed(MouseEvent e) {
 		// TODO Auto-generated method stub
+		x = e.getX();
+        y = e.getY();
+        
+		for (CpsObject cps : choords){
+    		int cx = cps.getPos().x;
+    		int cy = cps.getPos().y;
+    		if (x-15<=cx && y-15<=cy && x+15>=cx && y+15>= cy) {
+				tempCPS = cps;
+				dragging = true;
+			}
+    	}
 		
 	}
 
@@ -63,12 +82,28 @@ class MyCanvas extends JPanel implements MouseListener
 	public void mouseReleased(MouseEvent e) {
 		x = e.getX();
         y = e.getY();
-
-        HolonObject h = new HolonObject("Haus");
-        h.setPos(x, y);
         
-    	choords.add(h);
-        System.out.println("Draw!");
-		repaint();
+        if(dragging){
+        	dragging = false;
+        	tempCPS.setPos(e.getX(), e.getY());
+        	tempCPS = null;
+        }
+        repaint();
+	}
+
+	@Override
+	public void mouseDragged(MouseEvent e) {
+		// TODO Auto-generated method stub
+		if(dragging){
+			tempCPS.setPos(e.getX(), e.getY());
+			repaint();
+			System.out.println("drag: "+e.getX()+" "+e.getY());
+		}
+	}
+
+	@Override
+	public void mouseMoved(MouseEvent e) {
+		// TODO Auto-generated method stub
+		
 	}
 }