package ui.view; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Paint; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.LinkedList; import javax.swing.AbstractButton; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JToolTip; import classes.CpsObject; import classes.GlobalVariables; import classes.HolonElement; import classes.HolonObject; import ui.controller.Control; import ui.model.*; 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; private Model model; private final Control controller; private int cx; private int cy; boolean dragging = false; boolean dropDelete = false; private CpsObject tempCps = null; private Rectangle selectRect = new Rectangle(); // PopUpMenu private JPopupMenu popmenu = new JPopupMenu(); private JMenuItem itemDelete = new JMenuItem("Delete Object"); private JToolTip objectTT = new JToolTip(); public MyCanvas(final Model model, Control control) { this.add(objectTT); this.controller = control; this.model = model; popmenu.add(itemDelete); itemDelete.setEnabled(false); itemDelete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { model.getObjectsOnCanvas().remove(tempCps); tempCps = null; selectRect.setRect(0, 0, 0, 0); repaint(); } }); this.addMouseListener(this); this.addMouseMotionListener(this); } /** * Paints all Components on the Canvas * * @param Graphics * */ public void paintComponent(Graphics g) { super.paintComponent(g); //Selection if(selectRect != null){ g.setColor(new Color(100, 255, 100)); g.fillRect((int)selectRect.getX(), (int)selectRect.getY(), (int)selectRect.getWidth(), (int)selectRect.getHeight()); } //Edges //g.drawLine(x1, y1, x2, y2); //Objects for (CpsObject cps : model.getObjectsOnCanvas()) { img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage(); g.drawImage(img, cps.getPos().x, cps.getPos().y, GlobalVariables.SCALE, GlobalVariables.SCALE, null); } } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub x = e.getX(); y = e.getY(); tempCps = null; //Object Selection for (CpsObject cps : model.getObjectsOnCanvas()) { cx = cps.getPos().x; cy = cps.getPos().y; if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) { tempCps = cps; } } //Object Selection Highlighting (selectRect) objectSelectionHighlighting(); repaint(); } @Override public void mouseReleased(MouseEvent e) { if (dragging) { x = e.getX(); y = e.getY(); dragging = false; tempCps.setPos(e.getX() - GlobalVariables.SCALE/2, e.getY() - GlobalVariables.SCALE/2); tempCps = null; repaint(); } // Rechtsklick Liste if (e.getButton() == e.BUTTON3) { if (e.getButton() == e.BUTTON3 && tempCps != null) { itemDelete.setEnabled(true); }else { itemDelete.setEnabled(false); } popmenu.show(e.getComponent(), e.getX(), e.getY()); } } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub try { tempCps.setPos(e.getX() - GlobalVariables.SCALE/2, e.getY() - GlobalVariables.SCALE/2); dragging = true; selectRect.setLocation(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20)); objectTT.setTipText(tempCps.getName()); objectTT.setLocation(tempCps.getPos().x, tempCps.getPos().y); repaint(); } catch (Exception e2) { // TODO: handle exception } } @Override public void mouseMoved(MouseEvent e) { x = e.getX(); y = e.getY(); boolean on = false; for (CpsObject cps : model.getObjectsOnCanvas()) { cx = cps.getPos().x; cy = cps.getPos().y; if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) { objectTT.setTipText(cps.getName()); objectTT.setLocation(cx, cy); on = true; } } if(!on){ objectTT.setLocation(-200, -200); objectTT.setTipText(""); } } /** * Sets the Highlighting of the Selected Object */ private void objectSelectionHighlighting() { if(tempCps != null){ selectRect.setBounds(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20), GlobalVariables.SCALE+GlobalVariables.SCALE/10, GlobalVariables.SCALE+GlobalVariables.SCALE/10); controller.setSelectedObjectID(tempCps.getID()); System.out.println("Select"); }else { controller.setSelectedObjectID(0); selectRect.setRect(0, 0, 0, 0); System.out.println("Unselect"); } } }