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/Circle.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;
    25 import java.awt.geom.Point2D;
    2621import java.util.Collection;
    2722import java.util.LinkedList;
     23
     24import org.expeditee.core.Colour;
     25import org.expeditee.core.Dimension;
     26import org.expeditee.core.Fill;
     27import org.expeditee.core.Point;
     28import org.expeditee.core.Stroke;
     29import org.expeditee.core.bounds.AxisAlignedBoxBounds;
     30import org.expeditee.core.bounds.CombinationBoxBounds;
     31import org.expeditee.core.bounds.EllipticalBounds;
     32import org.expeditee.core.bounds.PolygonBounds;
     33import org.expeditee.gio.EcosystemManager;
    2834
    2935/**
     
    4248         * @param _source
    4349         */
    44         public Circle(Text source) {
     50        public Circle(Text source)
     51        {
    4552                super(source);
    4653                // Collection<Item> connected = source.getAllConnected();
     
    5057                _center.addEnclosure(this);
    5158                _line.setHidden(true);
    52                 updatePolygon();
     59                invalidateBounds();
    5360        }
    5461
     
    8794
    8895        @Override
    89         public Polygon getEnclosedShape() {
     96        public PolygonBounds getEnclosedShape() {
    9097                // assert(_poly != null);
    9198                // Ensure that vector items will gradient fill are painted OK!!
    92                 if (_poly == null) {
    93                         updatePolygon();
    94                 }
    95                 return _poly;
     99                return ((EllipticalBounds) updateBounds()).getPolygon(16);
    96100        }
    97101
     
    138142                newCircle._line.setVisible(_line.isVisible());
    139143                newCircle._source.setVisible(_source.isVisible());
    140                 newCircle.updatePolygon();
     144                newCircle.invalidateBounds();
    141145                return newCircle;
    142146        }
     
    152156
    153157        @Override
    154         public boolean contains(int x, int y) {
     158        public boolean contains(Point p) {
    155159                double radius = getRadius();
    156160
    157                 double distance = Math.sqrt(Math.pow(Math.abs(_center.getX() - x), 2)
    158                                 + Math.pow(Math.abs(_center.getY() - y), 2));
     161                double distance = Math.sqrt(Math.pow(Math.abs(_center.getX() - p.x), 2)
     162                                + Math.pow(Math.abs(_center.getY() - p.y), 2));
    159163
    160164                return Math.abs(distance - radius) < getGravity() * 2;
     
    167171         */
    168172        @Override
    169         public void paint(Graphics2D g) {
     173        public void paint() {
    170174                int radius = (int) Math.round(getRadius());
    171                 int diameter = radius * 2;
    172                 Color fillColor = getFillColor();
    173                 if (fillColor != null) {
    174                         setFillPaint(g);
    175                         g.fillOval(_center.getX() - radius, _center.getY() - radius,
    176                                         diameter, diameter);
     175                Point centre = new Point(_center.getX(), _center.getY());
     176                Dimension diameters = new Dimension(radius * 2, radius * 2);
     177                Fill fill = new Fill(getFillColor());
     178                Colour lineColour = null;
     179                Stroke lineStroke = null;
     180                if (isHighlighted()) {
     181                        lineColour = getHighlightColor();
     182                        lineStroke = HIGHLIGHT_STROKE;
     183                } else if (getThickness() > 0 || getFillColor() == null) {
     184                        lineColour = getPaintColor();
     185                        lineStroke = _line.getStroke();
    177186                }
    178                 if (getThickness() > 0 || fillColor == null) {
    179                         Color lineColor = getPaintColor();
    180                         g.setColor(lineColor);
    181                         g.setStroke(_line.getStroke());
    182                         g.drawOval(_center.getX() - radius, _center.getY() - radius,
    183                                         diameter, diameter);
     187               
     188                EcosystemManager.getGraphicsManager().drawOval(centre, diameters, 0.0f, fill, lineColour, lineStroke);
     189               
     190                if (isHighlighted()) {
     191                        _center.paint();
    184192                }
    185                 // Arc version, same result but allows for portions of the circle to be
    186                 // drawn
    187                 // g.drawArc(end.getX() - (distance / 2), end.getY() - (distance / 2),
    188                 // distance, distance, 0, 360);
    189 
    190                 if (isHighlighted()) {
    191                         // Flag the background color of the circle so that the item will be
    192                         // drawn with alternate color if the background is the same as
    193                         // the highlight}
    194                         _center.paint(g);
    195                         Color highlightColor = getHighlightColor();
    196                         g.setColor(highlightColor);
    197                         g.setStroke(HIGHLIGHT_STROKE);
    198                         g.drawOval(_center.getX() - radius, _center.getY() - radius,
    199                                         diameter, diameter);
    200                 }
    201         }
    202 
    203         @Override
    204         public void setHighlightMode(HighlightMode mode, Color color) {
    205                 _center.setHighlightMode(mode, color);
    206                 super.setHighlightMode(mode, color);
    207         }
    208 
    209         @Override
    210         public int setHighlightColor(Color c) {
     193        }
     194
     195        @Override
     196        public void setHighlightModeAndColour(HighlightMode mode, Colour color) {
     197                _center.setHighlightModeAndColour(mode, color);
     198                super.setHighlightModeAndColour(mode, color);
     199        }
     200
     201        @Override
     202        public void setHighlightColor(Colour c) {
    211203                _center.setHighlightColor(c);
    212                 return super.setHighlightColor(c);
    213         }
    214 
    215         @Override
    216         public void setFillColor(Color c) {
     204                /*return*/ super.setHighlightColor(c);
     205        }
     206
     207        @Override
     208        public void setFillColor(Colour c) {
    217209                super.setFillColor(c);
    218210                _center.setColor(c);
     
    221213
    222214        @Override
    223         public void setGradientColor(Color c) {
     215        public void setGradientColor(Colour c) {
    224216                super.setGradientColor(c);
    225217                invalidateCommonTrait(ItemAppearence.GradientColor);
     
    255247         */
    256248        @Override
    257         public void updatePolygon() {
    258                 double radius = getRadius();
    259                 // Approximation of circle for mouse interaction
    260                 int points = 20;
    261 
    262                 double radians = 0.0;
    263                 int xPoints[] = new int[points];
    264                 int yPoints[] = new int[xPoints.length];
    265 
    266                 for (int i = 0; i < xPoints.length; i++) {
    267                         xPoints[i] = (int) Math.round(radius * Math.cos(radians));
    268                         yPoints[i] = (int) Math.round(radius * Math.sin(radians));
    269                         radians += (2.0 * Math.PI) / xPoints.length;
    270                 }
    271 
    272                 _poly = new Polygon(xPoints, yPoints, xPoints.length);
    273                 _poly.translate(_center.getX(), _center.getY());
    274                 return;
     249        public EllipticalBounds updateBounds()
     250        {
     251                return new EllipticalBounds(_center.getPosition(), (int) (getRadius() * 2));
    275252        }
    276253
     
    280257        }
    281258
    282         /**
    283          * Resizes the circle from the center.
    284          */
    285         @Override
    286         public void setSize(float size) {
     259        /** Resizes the circle from the center. */
     260        @Override
     261        public void setSize(float size)
     262        {
    287263                double ratio = size / getRadius();
    288264               
     
    290266                _source.translate(_center.getPosition(), ratio);
    291267
    292                 updatePolygon();
     268                invalidateBounds();
    293269        }
    294270
     
    303279        }
    304280
    305         @Override
    306         public void translate(Point2D origin, double ratio) {
    307                 updatePolygon();
     281        // TODO: Why is this commented out? cts16
     282        @Override
     283        public void translate(Point origin, double ratio)
     284        {
     285                invalidateBounds();
    308286                // _center.translate(origin, ratio);
    309287                // super.translate(origin, ratio);
     
    311289
    312290        @Override
    313         public Rectangle[] getDrawingArea() {
     291        public AxisAlignedBoxBounds getDrawingArea() {
    314292
    315293                float thickness = getThickness();
     
    318296                int size = (int) ((2 * radius) + 3.0 + thickness);
    319297
    320                 return new Rectangle[] { new Rectangle((int) (_center.getX() - radius
     298                return new AxisAlignedBoxBounds((int) (_center.getX() - radius
    321299                                - 0.5 - (thickness / 2.0)), (int) (_center.getY() - radius
    322                                 - 0.5 - (thickness / 2.0)), size, size) };
     300                                - 0.5 - (thickness / 2.0)), size, size);
    323301        }
    324302
Note: See TracChangeset for help on using the changeset viewer.