123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- 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.Point;
- import javax.swing.JPanel;
- import ui.controller.Control;
- import ui.model.Model;
- import java.awt.BorderLayout;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.FlowLayout;
- import javax.swing.SwingConstants;
- import classes.HolonElement;
- import javax.swing.JLabel;
- import java.awt.Cursor;
- class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
- private static final long serialVersionUID = 1L;
- private Control controller;
- private Model model;
- private Graphics2D g2;
- private CubicCurve2D c = new CubicCurve2D.Double();
- private Point[] pointList;
- private HolonElement temp = null;
- private boolean pointDrag = false;
- private int tempP;
- private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
- private final JButton resetButton = new JButton("Reset Graph");
- private final JLabel lblName = new JLabel("Name");
- public UnitGraph(final Model model, Control control) {
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- this.controller = control;
- this.model = model;
- this.pointList = new Point[model.getIterations()];
- for (int i = 0; i < pointList.length; i++) {
- pointList[i] = new Point(0, 0);
- }
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- this.addComponentListener(this);
- lblName.setText("None");
- resetButton.setToolTipText("Resets the Graph");
- resetButton.setBackground(Color.WHITE);
- resetButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- reset();
- }
- });
- setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- add(lblName);
- add(resetButton);
- }
- /*
- * Resets the Graph
- */
- public void reset() {
- for (int i = 0; i < pointList.length; i++) {
- pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), this.getHeight() / 3);
- }
- repaint();
- }
- /**
- * 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(1));
- g2.setColor(new Color(240, 240, 240));
- for (int i = 0; i < pointList.length; i++) {
- g2.drawLine((int) pointList[i].getX(), 0, (int) pointList[i].getX(), this.getHeight());
- }
- g2.setColor(Color.BLACK);
- g2.setStroke(new BasicStroke(2));
- for (int i = 0; i < pointList.length - 1; i++) {
- x1 = (int) pointList[i].getX();
- y1 = (int) pointList[i].getY();
- x2 = (int) pointList[i + 1].getX();
- y2 = (int) pointList[i + 1].getY();
- ctrlx1 = (int) pointList[i].getX() + ((int) pointList[i + 1].getX() - (int) pointList[i].getX()) / 2;
- ctrlx2 = (int) pointList[i + 1].getX() - ((int) pointList[i + 1].getX() - (int) pointList[i].getX()) / 2;
- if (y1 < y2) {
- ctrly1 = (int) pointList[i].getY() + ((int) pointList[i + 1].getY() - (int) pointList[i].getY()) / 10;
- ctrly2 = (int) pointList[i + 1].getY()
- - ((int) pointList[i + 1].getY() - (int) pointList[i].getY()) / 10;
- } else {
- ctrly1 = (int) pointList[i].getY() - ((int) pointList[i].getY() - (int) pointList[i + 1].getY()) / 10;
- ctrly2 = (int) pointList[i + 1].getY()
- + ((int) pointList[i].getY() - (int) pointList[i + 1].getY()) / 10;
- }
- c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
- // draw the curve
- g2.draw(c);
- }
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- if (temp != null) {
- if (pointDrag && e.getY() >= this.getHeight() / 3 && tempP != -1) {
- pointList[tempP].setLocation(pointList[tempP].getX(), e.getY());
- if (tempP != 0 && pointList[tempP - 1].getY() < pointList[tempP].getY()
- - (pointList[tempP].getY() - this.getHeight() / 3) / 2) {
- pointList[tempP - 1].setLocation(pointList[tempP - 1].getX(),
- pointList[tempP].getY() - (pointList[tempP].getY() - this.getHeight() / 3) / 2);
- }
- if (tempP != model.getIterations() - 1 && pointList[tempP + 1].getY() < pointList[tempP].getY()
- - (pointList[tempP].getY() - this.getHeight() / 3) / 2) {
- pointList[tempP + 1].setLocation(pointList[tempP + 1].getX(),
- pointList[tempP].getY() - (pointList[tempP].getY() - this.getHeight() / 3) / 2);
- }
- } else if (tempP != -1) {
- pointList[tempP].setLocation(pointList[tempP].getX(), this.getHeight() / 3);
- }
- repaint();
- }
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @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) {
- for (int i = 0; i < pointList.length; i++) {
- if (e.getX() - this.getWidth() / model.getIterations() / 2 <= pointList[i].getX()
- && e.getX() + this.getWidth() / model.getIterations() / 2 >= pointList[i].getX()
- && e.getY() - 10 < pointList[i].getY() && e.getY() + 10 > pointList[i].getY()) {
- tempP = i;
- pointDrag = true;
- }
- }
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- if (pointDrag) {
- pointDrag = false;
- tempP = -1;
- }
- }
- public void repaintWithNewElement(HolonElement ele) {
- reset();
- float[] arrayOfValue = ele.getEnergyAt();
- lblName.setText(ele.getEleName());
- temp = ele;
- // TODO
- }
- // resize listener
- public void componentResized(ComponentEvent e) {
- if (pointList[0].getY() != 0) {
- reset();
- } else {
- for (int i = 0; i < pointList.length; i++) {
- pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), this.getHeight() / 3);
- }
- }
- repaint();
- }
- @Override
- public void componentHidden(ComponentEvent e) {
- }
- @Override
- public void componentMoved(ComponentEvent e) {
- }
- @Override
- public void componentShown(ComponentEvent e) {
- }
- }
|