123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642 |
- package ui.view;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.geom.CubicCurve2D;
- import java.awt.geom.GeneralPath;
- import java.awt.geom.Line2D;
- import java.util.LinkedList;
- import java.awt.Point;
- import javax.swing.JPanel;
- import classes.HolonElement;
- import ui.controller.Control;
- import ui.model.Model;
- import classes.HolonSwitch;
- import java.awt.Cursor;
- class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
- private static final long serialVersionUID = 1L;
- private float MAXIMUM = 0;
- private Point recSize = new Point(8, 8); // Point Size
- private Graphics2D g2;
- private CubicCurve2D c = new CubicCurve2D.Double();
- private CubicCurve2D cr = new CubicCurve2D.Double();
- private CubicCurve2D cl = new CubicCurve2D.Double();
- private LinkedList<Point> pointList;
- private double scaleX;
- private double scaleY;
- private float[] arrayOfFloats = null;
- private boolean[] arrayOfBooleans = null;
- private double width = -1;
- private double height = -1;
- private boolean isElement = false;
- private boolean isSwitch = false;
- private HolonElement tempElement;
- private HolonSwitch tempSwitch;
- private Model model;
- private Control controller;
- private Line2D.Double line = null;
- GeneralPath graphCurve = new GeneralPath();
- private boolean pointDrag = false;
- private boolean init = true;
- private Point tempP = null;
- private double x = 0, y = 0;
- private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
- public UnitGraph(final Model model, Control control) {
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- this.controller = control;
- this.model = model;
- this.pointList = new LinkedList<>();
- this.setBackground(Color.WHITE);
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- this.addComponentListener(this);
- }
- /**
- * Paints all Components on the Canvas
- *
- * @param Graphics
- *
- */
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g2 = (Graphics2D) g;
- RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2.setRenderingHints(rh);
- g2.setStroke(new BasicStroke(0));
- graphCurve.reset();
- // Draw the Vertical Lines
- g2.setColor(Color.BLACK);
- for (int i = 0; i <= this.getWidth(); i += 10) {
- g2.drawLine(i, 0, i, this.getHeight());
- }
- for (int i = 0; i <= this.getHeight(); i += 5) {
- g2.drawLine(0, i, this.getWidth(), i);
- }
- if (isElement) {
- if (arrayOfFloats != null) {
- // array fillen
- fillArrayofValue();
- // Draw the Lines
- g2.setStroke(new BasicStroke(2));
- g2.setColor(Color.BLACK);
- for (int i = 0; i < pointList.size() - 1; i++) {
- c = buildCurve(pointList.get(i), pointList.get(i + 1));
- graphCurve.append(c, true);
- }
- g2.draw(graphCurve);
- // Draw the Points
- g2.setColor(Color.BLUE);
- for (int i = 0; i < pointList.size() - 0; i++) {
- g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
- (int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
- (int) recSize.getY());
- }
- // Iteration Value
- if (arrayOfFloats != null) {
- g2.drawString("" + arrayOfFloats[model.getCurIteration()],
- (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
- this.getHeight() - 10);
- }
- }
- /*
- * // Actual Iteration Point Visualization g2.setColor(Color.RED);
- * if (arrayOfValue != null) { for (int i = 0; i <
- * arrayOfValue.length; i++) { g2.fillOval((int) (i * width /
- * (model.getIterations() - 1) * scaleX - recSize.getX() / 2), (int)
- * (convertToCanvasY((int) arrayOfValue[i]) * scaleY -
- * recSize.getY() / 2), (int) recSize.getX(), (int) recSize.getY());
- * } }
- */
- } else if (isSwitch) {
- if (arrayOfBooleans != null) {
- // array fillen
- fillArrayofBooleans();
- // Draw the Lines
- g2.setStroke(new BasicStroke(1));
- g2.setColor(Color.BLACK);
- for (int i = 0; i < pointList.size() - 1; i++) {
- line = new Line2D.Double(pointList.get(i).getX() * scaleX, pointList.get(i).getY() * scaleY,
- pointList.get(i + 1).getX() * scaleX, pointList.get(i + 1).getY() * scaleY);
- graphCurve.append(line, true);
- }
- g2.draw(graphCurve);
-
- /*
- // Draw the Points
- g2.setColor(Color.BLUE);
- for (int i = 0; i < pointList.size() - 0; i++) {
- g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
- (int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
- (int) recSize.getY());
- }
- */
-
- // Iteration Value
- if (arrayOfBooleans != null) {
- g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
- (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
- this.getHeight() - 10);
- }
- }
- }
- // Iteration Line
- g2.setColor(Color.BLUE);
- g2.setStroke(new BasicStroke(1));
- g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
- (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- if (isElement) {
- elementDragged(e);
- } else if (isSwitch) {
- switchDragged(e);
- }
- }
- /**
- * Wenn ein Punkt bei einem HolonElement gedragged wird
- *
- * @param e
- */
- public void elementDragged(MouseEvent e) {
- if (pointDrag && tempP != null) {
- // Out of Bounds verhindern
- int i = pointList.indexOf(tempP);
- x = e.getX() / scaleX;
- y = e.getY() / scaleY;
- // y
- if (e.getY() <= 0) {
- y = 0 / scaleY;
- } else if (this.getHeight() <= e.getY()) {
- y = this.getHeight() / scaleY;
- }
- // x
- if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() <= x
- || pointList.get(i - 1).getX() >= x) {
- x = tempP.getX();
- }
- tempP.setLocation(x, y);
- repaint();
- }
- }
- /**
- * Wenn ein Punkt bei einem CpsSwitch gedragged wird
- *
- * @param e
- */
- public void switchDragged(MouseEvent e) {
- // TODO Switch dragged zeugs kommt hier hin
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- @Override
- public void mousePressed(MouseEvent e) {
- if (isElement) {
- elementPressed(e);
- } else if (isSwitch) {
- switchPressed(e);
- }
- }
- /**
- * Wenn ein Punkt von einem Element gedrück wird
- *
- * @param e
- */
- public void elementPressed(MouseEvent e) {
- boolean added = false;
- boolean deletePoint = false;
- double x = e.getX() / scaleX;
- double y = e.getY() / scaleY;
- // Click on Point
- tempP = null;
- if (pointList != null) {
- // look if a point was clicked
- for (Point p : pointList) {
- if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
- && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
- if (e.getButton() == MouseEvent.BUTTON3) {
- tempP = p;
- deletePoint = true;
- } else {
- pointDrag = true;
- tempP = p;
- }
- }
- }
- // New Point
- if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
- && e.getX() != this.getWidth() / scaleX) {
- for (int i = 0; i < pointList.size(); i++) {
- if (x < pointList.get(i).getX() && !added) {
- if (e.getY() <= 0) {
- pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
- } else {
- pointList.add(i, new Point((int) (x), (int) y));
- }
- added = true;
- pointDrag = true;
- tempP = pointList.get(i);
- }
- }
- }
- // Delete a Point
- if (deletePoint && tempP.getX() != 0
- && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
- pointList.remove(tempP);
- }
- repaint();
- }
- }
- /**
- * Wenn ein Punkt von einem Switch gedrück wird
- *
- * @param e
- */
- public void switchPressed(MouseEvent e) {
- boolean added = false;
- boolean deletePoint = false;
- double x = e.getX() / scaleX;
- double y = e.getY() / scaleY;
- // Click on Point
- tempP = null;
- if (pointList != null) {
- for (Point p : pointList) {
- if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
- && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
- if (e.getButton() == MouseEvent.BUTTON3) {
- tempP = p;
- deletePoint = true;
- } else {
- pointDrag = true;
- tempP = p;
- }
- }
- }
- // New Point
- if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && x != 0 && x != width) {
- for (int i = 0; i < pointList.size(); i++) {
- if (x < pointList.get(i).getX() && !added) {
- double it = 0;
- double dist = (1 * this.getWidth() / (model.getIterations() - 1))/scaleX/2;
- //Iterationspuunkt in der Nähe vom Click holen
- for (int j = 0; (j < model.getIterations() && it < x); j++) {
- it = (j * this.getWidth() / (model.getIterations() - 1))/scaleX;
- }
-
- //Punkt hinzufügen, je nachdem ob es oberhalb oder unterhalb der hälfte ist
- if (y < height / 2 && (pointList.get(i - 1).getY() != 0 || pointList.get(i).getY() != 0)) {
- pointList.add(i, new Point((int) (it+dist), (int) height-1));
- pointList.add(i, new Point((int) (it+dist), 0));
- pointList.add(i, new Point((int) (it-dist), 0));
- pointList.add(i, new Point((int) (it-dist), (int) height-1));
- } else if (x >= height/2 && (pointList.get(i-1).getY() != height-1 || pointList.get(i ).getY() != height-1)) {
- pointList.add(i, new Point((int) (it+dist), 0));
- pointList.add(i, new Point((int) (it+dist), (int) height-1));
- pointList.add(i, new Point((int) (it-dist), (int) height-1));
- pointList.add(i, new Point((int) (it-dist), 0));
- }
- added = true;
- }
- }
- }
- repaint();
- }
- // TODO Siwtch pressed zeugs hier hin
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- if (pointDrag) {
- pointDrag = false;
- tempP = null;
- }
- }
- public void componentResized(ComponentEvent e) {
- // Wenn ein anderes Element genommen wird
- if (init) {
- init = false;
- // for scale on the first initialisation
- if (width == -1 && height == -1) {
- width = this.getWidth();
- height = this.getHeight();
- }
- scaleX = this.getWidth() / width;
- scaleY = this.getHeight() / height;
- }
- // Scale
- scaleX = this.getWidth() / width;
- scaleY = this.getHeight() / height;
- repaint();
- }
- @Override
- public void componentHidden(ComponentEvent e) {
- }
- @Override
- public void componentMoved(ComponentEvent e) {
- }
- @Override
- public void componentShown(ComponentEvent e) {
- }
- /*
- * Emptys the Graph
- */
- public void empty() {
- pointList = null;
- tempElement = null;
- tempSwitch = null;
- arrayOfFloats = null;
- arrayOfBooleans = null;
- isSwitch = false;
- isElement = false;
- repaint();
- }
- /*
- * Resets the Points for the Element
- */
- public void reset() {
- pointList.removeAll(pointList);
- pointList.addFirst(new Point(0, 0));
- pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
- repaint();
- }
- /**
- * converts the number to fit the canvas
- *
- * @param double
- * d, the number to convert
- * @return the converted number
- */
- public double convertToCanvasY(float d) {
- return (height - (d * (height / MAXIMUM)));
- }
- /**
- * converts the number to fit the value
- *
- * @param double
- * d, the number to convert
- * @return the converted number
- */
- public float convertToValueY(double d) {
- return (float) Math.round(((height - (height * (d / height))) / (height / MAXIMUM)) * 10) / 10;
- }
- /**
- * Visualize the HolonElement on the Graph
- *
- * @param HolonElement
- * ele, which should be visualized
- */
- public void repaintWithNewElement(HolonElement ele) {
- arrayOfFloats = ele.getEnergyAt();
- tempElement = ele;
- pointList = ele.getGraphPoints();
- isSwitch = false;
- isElement = true;
- MAXIMUM = tempElement.getEnergy();
- // First time clicked on the Element
- if (pointList.isEmpty()) {
- pointList.addFirst(new Point(0, 0));
- pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
- }
- repaint();
- }
- /**
- * Visualize the HolonElement on the Graph
- *
- * @param HolonElement
- * ele, which should be visualized
- */
- public void repaintWithNewSwitch(HolonSwitch s) {
- arrayOfBooleans = s.getActiveAt();
- tempSwitch = s;
- pointList = s.getGraphPoints();
- isSwitch = true;
- isElement = false;
- // First time clicked on the Element
- if (pointList.isEmpty()) {
- pointList.addFirst(new Point(0, 0));
- pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
- }
- repaint();
- }
- /**
- * Build a Curve for the Graph
- *
- * @param Point,Point
- * ,startpoint p1 and endpoint p2
- *
- * @return CubicCurve2D, c, the CubicCurve2D for the Graph
- */
- public CubicCurve2D buildCurve(Point p1, Point p2) {
- x1 = (int) p1.getX();
- y1 = (int) p1.getY();
- x2 = (int) p2.getX();
- y2 = (int) p2.getY();
- // calculate the controllpoints
- ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
- ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
- if (y1 < y2) {
- ctrly1 = (int) p1.getY() + ((int) p2.getY() - (int) p1.getY()) / 10;
- ctrly2 = (int) p2.getY() - ((int) p2.getY() - (int) p1.getY()) / 10;
- } else {
- ctrly1 = (int) p1.getY() - ((int) p1.getY() - (int) p2.getY()) / 10;
- ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
- }
- // set the curve
- c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
- x2 * scaleX, y2 * scaleY);
- return c;
- }
- /**
- * Fills the Arrays with booleans
- */
- public void fillArrayofBooleans() {
- for (int i = 0; i < arrayOfBooleans.length; i++) {
- arrayOfBooleans[i] = true;
- }
- }
- /**
- * Fills the Arrays of each HolonElement
- */
- public void fillArrayofValue() {
- for (int i = 0; i < arrayOfFloats.length; i++) {
- arrayOfFloats[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
- }
- }
- /**
- *
- * @param xVal,
- * the x value for the y value
- * @return y, the value at x
- */
- public float getYValueAt(int xVal) {
- for (int i = 0; i < pointList.size() - 1; i++) {
- // get the Points
- if (xVal <= pointList.get(i + 1).getX()) {
- // Curve erstellen
- Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
- pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
- Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
- return getIntersectionPoint(l1, l2);
- }
- }
- return 0;
- }
- /**
- *
- * @param xVal,
- * the x value for the y value
- * @return y, the value at x
- */
- public float getYValueAt_2(int xVal) {
- for (int i = 0; i < pointList.size() - 1; i++) {
- // get the Points
- if (xVal >= pointList.get(i).getX()) {
- // Curve erstellen
- c = buildCurve(pointList.get(i), pointList.get(i + 1));
- c.subdivide(cl, cr);
- // Teil der Kurve aussuchen
- if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
- c = cl;
- // Kurve Links von "unten"
- if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
- for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
- if (c.contains(xVal * scaleX, j * scaleY)) {
- return (float) (j);
- }
- }
- } else {// Kurve Links von "oben"
- for (float j = 0; j < height; j += 0.1f) {
- if (c.contains(xVal * scaleX, j * scaleY)) {
- return (float) (j);
- }
- }
- }
- } else {
- c = cr;
- // Kurve Links von "unten"
- if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
- for (float j = 0; j < height; j += 0.1f) {
- if (c.contains(xVal * scaleX, j * scaleY)) {
- return (float) (j);
- }
- }
- } else {// Kurve Links von "oben"
- for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
- if (c.contains(xVal * scaleX, j * scaleY)) {
- return (float) (j);
- }
- }
- }
- }
- }
- }
- return getYValueAt(xVal);
- }
- /**
- *
- * @param l1,
- * the first Line
- * @param l2,
- * the second Line
- *
- * @return The Intersection Point
- */
- public float getIntersectionPoint(Line2D l1, Line2D l2) {
- if (!l1.intersectsLine(l2)) {
- return 0;// null;
- }
- double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
- double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
- double det = sx * ry - sy * rx;
- if (det == 0) {
- return 0;// null;
- } else {
- double z = (sx * (qy - py) + sy * (px - qx)) / det;
- if (z < 0 || z > 1) {
- return 0;// new Point(0, 0); // intersection at end point!
- }
- return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
- // (py + z * ry));
- }
- } // end intersection line-line
- }
|