123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- package ui.view;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Paint;
- import java.awt.Rectangle;
- import java.awt.RenderingHints;
- 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.awt.geom.Line2D;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import javax.swing.AbstractButton;
- import javax.swing.ImageIcon;
- import javax.swing.JComponent;
- 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;
- //edge Object Start Point
- private Model model;
- private final Control controller;
- Graphics2D g2; //For Painting
- private int cx;
- private int cy;
-
- boolean dragging = false;
- boolean drawEdge = 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);
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- cps.getConnectedTo().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);
- g2 = (Graphics2D) g;
- g2.setStroke(new BasicStroke(GlobalVariables.SCALE/15));
- RenderingHints rh = new RenderingHints(
- RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- g2.setRenderingHints(rh);
-
-
- //Selection
- if(selectRect != null){
- g2.setColor(Color.GREEN);
- g2.fillRect((int)selectRect.getX(), (int)selectRect.getY(), (int)selectRect.getWidth(), (int)selectRect.getHeight());
- }
-
- //drawEdges
- g2.setColor(Color.BLACK);
- if(drawEdge)g2.drawLine(tempCps.getPos().x+GlobalVariables.SCALE_DIVIDED2, tempCps.getPos().y+GlobalVariables.SCALE_DIVIDED2, x, y);
-
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- for (CpsObject con : cps.getConnectedTo()) {
- if(con.getID() != model.getSelectedObjectID())
- g2.drawLine(cps.getPos().x+GlobalVariables.SCALE_DIVIDED2, cps.getPos().y+GlobalVariables.SCALE_DIVIDED2, con.getPos().x+GlobalVariables.SCALE_DIVIDED2, con.getPos().y+GlobalVariables.SCALE_DIVIDED2);
- }
- }
-
- g2.setColor(Color.GREEN);
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- for (CpsObject con : cps.getConnectedTo()) {
- if(con.getID() == model.getSelectedObjectID())
- g2.drawLine(cps.getPos().x+GlobalVariables.SCALE_DIVIDED2, cps.getPos().y+GlobalVariables.SCALE_DIVIDED2, con.getPos().x+GlobalVariables.SCALE_DIVIDED2, con.getPos().y+GlobalVariables.SCALE_DIVIDED2);
- }
- }
-
- //Objects
- for (CpsObject cps : model.getObjectsOnCanvas()) {
- img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
- g2.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;
- if(e.isControlDown())drawEdge = true;
- }
- }
-
- //Object Selection Highlighting (selectRect)
- objectSelectionHighlighting();
-
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- if(drawEdge){
- drawEdge = 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) {
- cps.AddConnection(tempCps);
- tempCps.AddConnection(cps);
- }
- }
- }
-
- if (dragging) {
- x = e.getX();
- y = e.getY();
-
- dragging = false;
- tempCps.setPos(e.getX() - GlobalVariables.SCALE_DIVIDED2, e.getY() - GlobalVariables.SCALE_DIVIDED2);
- tempCps = null;
- }
-
- // 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());
- }
-
- repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- // TODO Auto-generated method stub
- if(drawEdge){
- x = e.getX();
- y = e.getY();
- repaint();
- }else{
- try {
- tempCps.setPos(e.getX() - GlobalVariables.SCALE_DIVIDED2, e.getY() - GlobalVariables.SCALE_DIVIDED2);
- 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());
- }else {
- controller.setSelectedObjectID(0);
- selectRect.setRect(0, 0, 0, 0);
- }
- }
- }
|