Ignore:
Timestamp:
05/10/18 16:04:51 (6 years ago)
Author:
davidb
Message:

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/Dot.java

    r919 r1102  
    1919package org.expeditee.items;
    2020
    21 import java.awt.Color;
    22 import java.awt.Graphics2D;
    23 import java.awt.Polygon;
    24 import java.awt.Rectangle;
    2521import java.util.ArrayList;
    2622import java.util.LinkedList;
    2723import java.util.List;
    2824
    29 import org.expeditee.gui.DisplayIO;
     25import org.expeditee.core.Colour;
     26import org.expeditee.core.Dimension;
     27import org.expeditee.core.Fill;
     28import org.expeditee.core.Point;
     29import org.expeditee.core.bounds.AxisAlignedBoxBounds;
     30import org.expeditee.core.bounds.Bounds;
     31import org.expeditee.core.bounds.PolygonBounds;
     32import org.expeditee.gio.EcosystemManager;
     33import org.expeditee.gio.GraphicsManager;
     34import org.expeditee.gui.DisplayController;
    3035import org.expeditee.gui.Frame;
    3136import org.expeditee.gui.FrameGraphics;
    32 import org.expeditee.gui.FrameKeyboardActions;
    3337
    3438/**
     
    7074
    7175        @Override
    72         public void setColor(Color c) {
     76        public void setColor(Colour c) {
    7377                super.setColor(c);
    7478
     
    7983
    8084        @Override
    81         public void setAnchorLeft(Float anchor) {
     85        public void setAnchorLeft(Integer anchor) {
    8286                if (!isLineEnd()) {
    8387                        super.setAnchorLeft(anchor);
     
    8892                invalidateCommonTrait(ItemAppearence.PreMoved);
    8993               
    90                 this._anchorLeft = anchor;
    91                 this._anchorRight = null;
     94                this._anchoring.setLeftAnchor(anchor);
    9295               
    9396                int oldX = getX();
     
    102105
    103106        @Override
    104         public void setAnchorRight(Float anchor) {
     107        public void setAnchorRight(Integer anchor) {
    105108                if (!isLineEnd()) {
    106109                        super.setAnchorRight(anchor);
     
    110113                invalidateCommonTrait(ItemAppearence.PreMoved);
    111114               
    112                 this._anchorRight = anchor;
    113                 this._anchorLeft = null;
     115                this._anchoring.setRightAnchor(anchor);
    114116               
    115117                int oldX = getX();
    116118                if (anchor != null) {
    117                         float deltaX = FrameGraphics.getMaxFrameSize().width - anchor
    118                                         - getBoundsWidth() - oldX;
     119                        float deltaX = DisplayController.getFramePaintArea().getWidth() - anchor - getBoundsWidth() - oldX;
    119120
    120121                        anchorConnected(AnchorEdgeType.Right, deltaX);
     
    126127
    127128        @Override
    128         public void setAnchorTop(Float anchor) {
     129        public void setAnchorTop(Integer anchor) {
    129130                if (!isLineEnd()) {
    130131                        super.setAnchorTop(anchor);
     
    134135                invalidateCommonTrait(ItemAppearence.PreMoved);
    135136               
    136                 this._anchorTop = anchor;
    137                 this._anchorBottom = null;
     137                this._anchoring.setTopAnchor(anchor);
    138138               
    139139                int oldY = getY();
     
    148148
    149149        @Override
    150         public void setAnchorBottom(Float anchor) {
     150        public void setAnchorBottom(Integer anchor) {
    151151                if (!isLineEnd()) {
    152152                        super.setAnchorBottom(anchor);
     
    156156                invalidateCommonTrait(ItemAppearence.PreMoved);
    157157               
    158                 this._anchorBottom = anchor;
    159                 this._anchorTop = null;
     158                this._anchoring.setBottomAnchor(anchor);
    160159               
    161160                int oldY = getY();
    162161                if (anchor != null) {
    163                         float deltaY = FrameGraphics.getMaxFrameSize().height - anchor
    164                                         - getBoundsHeight() - oldY;
     162                        float deltaY = DisplayController.getFramePaintArea().getHeight() - anchor - getBoundsHeight() - oldY;
    165163                        anchorConnected(AnchorEdgeType.Bottom, deltaY);
    166164                }
     
    173171
    174172        @Override
    175         public void paint(Graphics2D g) {
     173        public void paint() {
     174                GraphicsManager g = EcosystemManager.getGraphicsManager();
    176175                if (isHighlighted() /* && !FreeItems.getInstance().contains(this) */) {
    177                         Color highlightColor = getHighlightColor();
    178                         g.setColor(highlightColor);
    179                         g.setStroke(DOT_STROKE);
     176                        Colour highlightColor = getHighlightColor();
     177                        Fill fill = new Fill(highlightColor);
    180178                        // g.setStroke()
    181179                        // Draw the highlighting rectangle surrounding the dot
    182180                        // this is drawn even if its part of a rectangle
    183181
    184                         if (isVectorItem())
    185                                 updatePolygon();
    186 
    187                         Rectangle rect = getPolygon().getBounds();
    188                         if (_mode == HighlightMode.Enclosed ||
    189                         // Make sure single dots are highlighted filled
    190                                         this.getConnected().size() <= 1)
    191                                 g.fillRect(rect.x, rect.y, rect.width, rect.height);
    192                         else if (_mode == HighlightMode.Connected){
    193                                 g.setStroke(HIGHLIGHT_STROKE);
    194                                 g.drawRect(rect.x, rect.y, rect.width, rect.height);
    195                         }else if (_mode == HighlightMode.Normal) {
    196                                 g.fillOval(rect.x, rect.y, rect.width, rect.height);
     182                        if (isVectorItem()) invalidateBounds();
     183
     184                        AxisAlignedBoxBounds bounds = getBoundingBox();
     185                        if (_highlightMode == HighlightMode.Enclosed || this.getConnected().size() <= 1) { // Make sure single dots are highlighted filled
     186                                g.drawRectangle(bounds.getTopLeft(), bounds.getSize(), 0.0f, fill, highlightColor, DOT_STROKE, null);
     187                        } else if (_highlightMode == HighlightMode.Connected) {
     188                                g.drawRectangle(bounds.getTopLeft(), bounds.getSize(), 0.0f, null, highlightColor, HIGHLIGHT_STROKE, null);
     189                        } else if (_highlightMode == HighlightMode.Normal) {
     190                                g.drawOval(bounds.getCentre(), bounds.getSize(), 0.0f, fill, highlightColor, DOT_STROKE);
    197191                        }
    198192                        // System.out.println(_mode.toString());
     
    200194
    201195                // dots on lines are hidden
    202                 if (getLines().size() > 0)
    203                         return;
    204 
    205                 g.setColor(getPaintColor());
     196                if (getLines().size() > 0) return;
    206197
    207198                int thick = (int) getThickness();
    208                 if (thick < MINIMUM_DOT_SIZE)
    209                         thick = MINIMUM_DOT_SIZE;
    210 
    211                 int width = thick / 2;
    212 
     199                if (thick < MINIMUM_DOT_SIZE) thick = MINIMUM_DOT_SIZE;
     200               
     201                Fill fill = _filled ? new Fill(getPaintColor()) : null;
     202
     203                // TODO: On testing, this code doesn't seem to work (can't change to any type except square). cts16
    213204                switch (_type) {
    214                 case circle:
    215                         if (_filled)
    216                                 g.fillOval(getX() - width, getY() - width, thick, thick);
    217                         else {
    218                                 g.drawOval(getX() - width, getY() - width, thick, thick);
    219                         }
    220                         break;
    221                 case diamond:
    222                         int[] x = new int[4];
    223                         int[] y = new int[4];
    224 
    225                         x[0] = x[2] = getX();
    226                         x[1] = getX() - width;
    227                         x[3] = getX() + width;
    228 
    229                         y[1] = y[3] = getY();
    230                         y[0] = getY() - width;
    231                         y[2] = getY() + width;
    232 
    233                         if (_filled)
    234                                 g.fillPolygon(x, y, 4);
    235                         else {
    236                                 g.drawPolygon(x, y, 4);
    237                         }
    238                         break;
    239                 case triangle:
    240                         x = new int[3];
    241                         y = new int[3];
    242 
    243                         x[0] = getX();
    244                         x[1] = getX() - width;
    245                         x[2] = getX() + width;
    246 
    247                         y[0] = getY() - width;
    248                         y[1] = y[2] = getY() + width;
    249 
    250                         if (_filled)
    251                                 g.fillPolygon(x, y, 3);
    252                         else {
    253                                 g.drawPolygon(x, y, 3);
    254                         }
    255                         break;
    256                 case roundSquare:
    257                         int arc = thick / 2;
    258                         if (_filled)
    259                                 g.fillRoundRect(getX() - width, getY() - width, thick, thick,
    260                                                 arc, arc);
    261                         else {
    262                                 g.drawRoundRect(getX() - width, getY() - width, thick, thick,
    263                                                 arc, arc);
    264                         }
    265                         break;
    266                 default:
    267                         if (_filled)
    268                                 g.fillRect(getX() - width, getY() - width, thick, thick);
    269                         else {
    270                                 g.drawRect(getX() - width, getY() - width, thick, thick);
    271                         }
    272 
     205                        case circle:
     206                                g.drawOval(
     207                                                new Point(getX(), getY()),
     208                                                new Dimension(thick, thick),
     209                                                0.0,
     210                                                fill,
     211                                                getPaintColor(),
     212                                                null
     213                                );
     214                                break;
     215                        case diamond:
     216                                PolygonBounds diamond = PolygonBounds.getDiamond(thick, thick).translate(getX(), getY()).close();
     217                                g.drawPolygon(diamond, null, null, 0.0, fill, getPaintColor(), null);
     218                                break;
     219                        case triangle:
     220                                PolygonBounds triangle = PolygonBounds.getTriangle(thick, thick).translate(getX(), getY()).close();
     221                                g.drawPolygon(triangle, null, null, 0.0, fill, getPaintColor(), null);
     222                                break;
     223                        case roundSquare:
     224                                int arc = thick / 4;
     225                                g.drawRectangle(
     226                                        new Point(getX(), getY()),
     227                                        new Dimension(thick, thick),
     228                                        0.0,
     229                                        fill,
     230                                        getPaintColor(),
     231                                        null,
     232                                        new Dimension(arc, arc)
     233                                );
     234                                break;
     235                        default:
     236                                g.drawRectangle(
     237                                                new Point(getX(), getY()),
     238                                                new Dimension(thick, thick),
     239                                                0.0,
     240                                                fill,
     241                                                getPaintColor(),
     242                                                null,
     243                                                null
     244                                );
    273245                }
    274246
     
    276248
    277249        /**
    278          * Updates the points of the polygon surrounding this Dot
     250         * Updates the bounds surrounding this Dot.
     251         * TODO: Standardise Dot minimum size. cts16
    279252         */
    280         public void updatePolygon() {
     253        public Bounds updateBounds()
     254        {
    281255                int thick = Math.round(getThickness());
     256               
    282257                // Sets a minimum size for the dot
    283258                thick = Math.max(thick, getGravity() * 2);
     259               
     260                // Include the gravity in the thickness
     261                thick += 2 * getGravity();
     262               
    284263
    285264                int x = getX() - thick / 2;
    286265                int y = getY() - thick / 2;
    287 
    288                 _poly = new Polygon();
    289                 _poly.addPoint(x - getGravity(), y - getGravity());
    290                 _poly.addPoint(x + thick + getGravity(), y - getGravity());
    291                 _poly.addPoint(x + thick + getGravity(), y + thick + getGravity());
    292                 _poly.addPoint(x - getGravity(), y + thick + getGravity());
     266               
     267                return new AxisAlignedBoxBounds(x, y, thick, thick);
    293268        }
    294269
     
    303278
    304279        @Override
    305         public int setHighlightColor() {
    306                 super.setHighlightColor();
    307 
    308                 return Item.DEFAULT_CURSOR;
     280        public void setHighlightColorToDefault() {
     281                super.setHighlightColorToDefault();
     282
     283                //return Item.DEFAULT_CURSOR;
    309284        }
    310285
    311286        @Override
    312287        public void setAnnotation(boolean val) {
    313                 DisplayIO.setCursorPosition(this.getPosition());
    314                 FrameKeyboardActions.replaceDot(this, '@');
     288                DisplayController.setCursorPosition(this.getPosition());
     289                Item.replaceDot(this, '@');
    315290        }
    316291
     
    406381                        if (parent != null && parent != current) {
    407382                                this.setParent(parent);
    408                                 if (DisplayIO.getCurrentSide() == 0)
    409                                         this.setX(this.getX() - DisplayIO.getMiddle());
     383                                if (DisplayController.getCurrentSide() == DisplayController.TwinFramesSide.LEFT)
     384                                        this.setX(this.getX() - DisplayController.getTwinFramesSeparatorX());
    410385                                else
    411                                         this.setX(this.getX() + DisplayIO.getMiddle());
     386                                        this.setX(this.getX() + DisplayController.getTwinFramesSeparatorX());
    412387                        }
    413388                        break;
     
    420395                        if (line.getID() < 0 && !current.getItems().contains(line)) {
    421396                                line.setID(current.getNextItemID());
    422                                 line.setHighlightColor();
     397                                line.setHighlightColorToDefault();
    423398                                // Mike: Why was this line here?
    424399                                // anchor(line);
     
    436411
    437412        @Override
    438         public void lineColorChanged(Color c) {
     413        public void lineColorChanged(Colour c) {
    439414                if (getColor() != c) {
    440415                        setColor(c);
     
    450425                return super.dontSave();
    451426        }
     427
     428        @Override
     429        public float getSize()
     430        {
     431                return getThickness();
     432        }
    452433}
Note: See TracChangeset for help on using the changeset viewer.