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/io/Conversion.java

    r919 r1102  
    1919package org.expeditee.io;
    2020
    21 import java.awt.Color;
    22 import java.awt.Font;
    23 import java.awt.Point;
    2421import java.lang.reflect.Field;
    2522import java.lang.reflect.Method;
     
    2926
    3027import org.expeditee.actions.Actions;
     28import org.expeditee.core.Colour;
     29import org.expeditee.core.Font;
     30import org.expeditee.core.Point;
    3131import org.expeditee.items.Item;
    3232import org.expeditee.items.Text;
     
    4343public class Conversion {
    4444
    45         public static final int RGB_MAX = 255;
    46 
    47         private static final float RGB_CONVERSION_FACTOR = 2.55F;
     45        public static final short RGB_MAX = Colour.COMPONENT_MAX_VALUE;
     46
     47        private static final float RGB_CONVERSION_FACTOR = RGB_MAX / 100.0f;
    4848
    4949        /**
     
    5656         * @return The Color object corresponding to the given code
    5757         */
    58         public static Color getColor(String colorCode, Color current) {
     58        public static Colour getColor(String colorCode, Colour current) {
    5959                if (colorCode == null) {
    6060                        return null;
     
    6666
    6767                // check if its a normal rgb code ie. 100 0 40
    68                 Color rgb = getRGBColor(colorCode, current);
     68                Colour rgb = getRGBColor(colorCode, current);
    6969                if (rgb != null)
    7070                        return rgb;
     
    104104                        return null;
    105105
    106                 return new Color(color[0], color[1], color[2]);
    107         }
    108 
    109         private static Color getRGBColor(String colorCode, Color current) {
     106                return new Colour(color[0], color[1], color[2]);
     107        }
     108
     109        private static Colour getRGBColor(String colorCode, Colour current) {
    110110                int color[] = new int[4];
    111111                // Assert.assertTrue(color.length == 3);
     
    135135                                color[i] = toRGB(color[i]);
    136136                        }
    137                         return new Color(color[0], color[1], color[2], color[3]);
     137                        return new Colour(color[0], color[1], color[2], color[3]);
    138138                } catch (Exception e) {
    139139                        return null;
     
    175175         *         is black.
    176176         */
    177         public static String getExpediteeColorCode(Color color) {
     177        public static String getExpediteeColorCode(Colour color) {
    178178                if (color == null)
    179179                        return null;
     
    195195         */
    196196        public static String getExpediteeFontCode(Font font) {
    197                 String fontName = font.getFamily();
    198                 String code = font.getFamily() + '_';
     197                String fontName = font.getFamilyName();
     198                String code = font.getFamilyName() + '_';
    199199
    200200                for (int i = 0; i < Text.FONT_WHEEL.length; i++) {
     
    206206
    207207                switch (font.getStyle()) {
    208                 case Font.BOLD:
     208                case BOLD:
    209209                        code += "b";
    210210                        break;
    211                 case Font.PLAIN:
     211                case PLAIN:
    212212                        code += "r";
    213213                        break;
    214                 case Font.ITALIC:
     214                case ITALIC:
    215215                        code += "i";
    216216                        break;
     
    237237
    238238                int separator = fontCode.indexOf('_');
    239                 String code = Text.FONT_WHEEL[0];
     239                String familyName = Text.FONT_WHEEL[0];
    240240                if (separator > 0) {
    241                         code = Actions.getCapitalizedFontName(fontCode.substring(0,
    242                                         separator)) + '-';
     241                        familyName = Actions.getCapitalizedFontName(fontCode.substring(0, separator));
    243242                        fontCode = fontCode.substring(separator);
    244243                } else {
     
    246245                        for (int i = 0; i < Text.FONT_CHARS.length; i++) {
    247246                                if (c == Text.FONT_CHARS[i]) {
    248                                         code = Text.FONT_WHEEL[i] + '-';
     247                                        familyName = Text.FONT_WHEEL[i];
    249248                                        break;
    250249                                }
    251250                        }
    252251                }
     252               
     253                Font font = new Font(familyName);
    253254
    254255                switch (fontCode.charAt(1)) {
    255256                case 'r':
    256                         code += "Plain";
     257                        font.setStyle(Font.Style.PLAIN);
    257258                        break;
    258259                case 'b':
    259                         code += "Bold";
     260                        font.setStyle(Font.Style.BOLD);
    260261                        break;
    261262                case 'i':
    262                         code += "Italic";
     263                        font.setStyle(Font.Style.ITALIC);
    263264                        break;
    264265                case 'p':
    265                         code += "BoldItalic";
     266                        font.setStyle(Font.Style.BOLD_ITALIC);
    266267                        break;
    267268                }
    268 
    269                 code += "-";
    270 
    271                 Font font = null;
    272269
    273270                try {
    274271                        int size = Integer.parseInt(fontCode.substring(2));
    275                         // double dsize = size * FONT_SCALE;
    276                         // if (dsize - ((int) dsize) > FONT_ROUNDING)
    277                         // dsize = Math.ceil(dsize);
    278 
    279                         // code += (int) dsize;
    280                         code += size;
    281 
    282                         font = Font.decode(code);
     272                       
     273                        font.setSize(size);
    283274                } catch (NumberFormatException nfe) {
    284                         font = Font.decode(fontCode);
     275                        // Just keep going
    285276                }
    286277
     
    362353                }
    363354
    364                 if (type.equals(Color.class)) {
     355                if (type.equals(Colour.class)) {
    365356                        if (value.length() == 0)
    366357                                return null;
     
    368359                        try {
    369360                                // Try to decode the string as a hex or octal color code
    370                                 return Color.decode(value);
     361                                return Colour.decode(value);
    371362                        } catch (NumberFormatException nfe) {
    372363                                try {
    373                                         // Try to find the field in the Color class with the same name as the given string
    374                                         Field[] fields = java.awt.Color.class.getFields();
     364                                        // Try to find the field in the Colour class with the same name as the given string
     365                                        Field[] fields = Colour.class.getFields();
    375366                                        Field field = null;
    376367                                        for (int i = 0; i < fields.length; i++) {
     
    380371                                                }
    381372                                        }
    382                                         return (Color) field.get(null);
     373                                        return (Colour) field.get(null);
    383374                                } catch (Exception e) {
    384                                         return getColor(value, (Color) orig);
     375                                        return getColor(value, (Colour) orig);
    385376                                }
    386377                        }
     
    388379
    389380                if (type.equals(int.class)) {
    390                         if (orig instanceof Integer
    391                                         && (value.startsWith("+") || value.startsWith("-"))) {
     381                        if (orig instanceof Integer && (value.startsWith("+") || value.startsWith("-"))) {
    392382                                value = value.replace("+", "");
    393383
     
    395385                        }
    396386
    397                         if (value.length() == 0 || value.equals("null"))
    398                                 return Item.DEFAULT_INTEGER;
     387                        if (value.length() == 0 || value.equals("null")) return Item.DEFAULT_INTEGER;
    399388
    400389                        return Integer.decode(value);
     
    427416
    428417                if (type.equals(Integer.class)) {
    429                         if (orig instanceof Integer
    430                                         && (value.startsWith("+") || value.startsWith("-"))) {
     418                        if (orig instanceof Integer && (value.startsWith("+") || value.startsWith("-"))) {
    431419                                value = value.replace("+", "");
    432420
    433                                 Integer newValue = ((Integer) orig) + Integer.parseInt(value);
     421                                Integer newValue = ((Integer) orig) + Integer.valueOf((int) Double.parseDouble(value));
    434422                                if (newValue <= 0)
    435423                                        return null;
     
    440428                                return null;
    441429
    442                         return Integer.parseInt(value);
     430                        return Integer.valueOf((int) Double.parseDouble(value));
    443431                }
    444432
     
    700688
    701689                // convert colors
    702                 if (output instanceof Color)
    703                         return getExpediteeColorCode((Color) output);
     690                if (output instanceof Colour)
     691                        return getExpediteeColorCode((Colour) output);
    704692
    705693                // covert points
     
    737725        }
    738726
    739         public static String getCssColor(Color c) {
     727        public static String getCssColor(Colour c) {
    740728                assert (c != null);
    741                 return "rgb(" + c.getRed() + "," + c.getGreen() + "," + c.getBlue()
     729                return "rgb(" + c.getRed255() + "," + c.getGreen255() + "," + c.getBlue255()
    742730                                + ")";
    743731        }
Note: See TracChangeset for help on using the changeset viewer.