|
@@ -4,6 +4,7 @@ import java.awt.Graphics;
|
|
import java.awt.Image;
|
|
import java.awt.Image;
|
|
import java.awt.event.MouseEvent;
|
|
import java.awt.event.MouseEvent;
|
|
import java.awt.event.MouseListener;
|
|
import java.awt.event.MouseListener;
|
|
|
|
+import java.awt.event.MouseMotionListener;
|
|
import java.util.LinkedList;
|
|
import java.util.LinkedList;
|
|
|
|
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.ImageIcon;
|
|
@@ -11,20 +12,22 @@ import javax.swing.JPanel;
|
|
import ui.model.CpsObject;
|
|
import ui.model.CpsObject;
|
|
import ui.model.HolonObject;
|
|
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 Image img = null; // Contains the image to draw on MyCanvas
|
|
private int x = 0;
|
|
private int x = 0;
|
|
private int y = 0;
|
|
private int y = 0;
|
|
LinkedList<CpsObject> choords = new LinkedList<>();
|
|
LinkedList<CpsObject> choords = new LinkedList<>();
|
|
|
|
+ boolean dragging = false;
|
|
|
|
+ CpsObject tempCPS = null;
|
|
|
|
|
|
public MyCanvas()
|
|
public MyCanvas()
|
|
{
|
|
{
|
|
|
|
|
|
img = new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage().getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
|
|
img = new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage().getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
|
|
|
|
|
|
-
|
|
|
|
this.addMouseListener(this);
|
|
this.addMouseListener(this);
|
|
|
|
+ this.addMouseMotionListener(this);
|
|
}
|
|
}
|
|
|
|
|
|
public void paintComponent(Graphics g)
|
|
public void paintComponent(Graphics g)
|
|
@@ -32,13 +35,18 @@ class MyCanvas extends JPanel implements MouseListener
|
|
// Draws the image to the canvas
|
|
// Draws the image to the canvas
|
|
super.paintComponent(g);
|
|
super.paintComponent(g);
|
|
for (CpsObject cps : choords){
|
|
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
|
|
@Override
|
|
public void mouseClicked(MouseEvent e) {
|
|
public void mouseClicked(MouseEvent e) {
|
|
// TODO Auto-generated method stub
|
|
// 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
|
|
@Override
|
|
@@ -56,6 +64,17 @@ class MyCanvas extends JPanel implements MouseListener
|
|
@Override
|
|
@Override
|
|
public void mousePressed(MouseEvent e) {
|
|
public void mousePressed(MouseEvent e) {
|
|
// TODO Auto-generated method stub
|
|
// 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) {
|
|
public void mouseReleased(MouseEvent e) {
|
|
x = e.getX();
|
|
x = e.getX();
|
|
y = e.getY();
|
|
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
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|