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/apollo/gui/TimeAxis.java

    r375 r1102  
    11package org.apollo.gui;
    22
    3 import java.awt.Color;
    4 import java.awt.Font;
    5 import java.awt.FontMetrics;
    6 import java.awt.GradientPaint;
    7 import java.awt.Graphics;
    8 import java.awt.Graphics2D;
    9 import java.awt.Stroke;
    10 import java.awt.geom.Rectangle2D;
    11 import java.awt.image.BufferedImage;
    123import java.util.LinkedList;
    134
    145import org.apollo.ApolloSystem;
    15 import org.expeditee.gui.Browser;
     6import org.expeditee.core.Colour;
     7import org.expeditee.core.Dimension;
     8import org.expeditee.core.Fill;
     9import org.expeditee.core.Font;
     10import org.expeditee.core.GradientFill;
     11import org.expeditee.core.Point;
     12import org.expeditee.core.Stroke;
     13import org.expeditee.core.TextLayout;
     14import org.expeditee.gio.EcosystemManager;
     15import org.expeditee.gio.GraphicsManager;
     16import org.expeditee.gio.swing.SwingConversions;
    1617
    1718/**
     
    2324public class TimeAxis {
    2425       
    25         private static final long serialVersionUID = 1L;
    26        
    27         public static final Font TIME_BAR_FONT = new Font("Arial", Font.PLAIN, 14);
     26        public static final Font TIME_BAR_FONT = new Font("Arial", Font.Style.PLAIN, 14);
    2827        private static final Stroke TICK_STROKE = Strokes.SOLID_1;
    2928       
    30         private static final Color MAJOR_TICK_COLOR = Color.BLACK;
    31         private static final Color MINOR_TICK_COLOR = new Color(0.4f, 0.4f, 0.4f);
     29        private static final Colour MAJOR_TICK_COLOR = Colour.BLACK;
     30        private static final Colour MINOR_TICK_COLOR = new Colour(0.4f, 0.4f, 0.4f);
    3231       
    3332        private static final int DESIRED_MAJOR_TICK_PIXEL_SPACING = 100;
     
    4342        private long totalTrackMSLength = 0;
    4443
    45         public TimeAxis() {
     44        public TimeAxis()
     45        {
    4646        }
    4747
     
    5151                        long timeLengthSamples,
    5252                        long totalTrackMSLength,
    53                         int width) {
     53                        int width)
     54        {
    5455
    5556                assert(width > 0);
     
    146147                LinkedList<Integer> majorTickLabelPositions = new LinkedList<Integer>();
    147148       
    148                 Graphics g = null;
    149                 try {
    150                
    151                        
    152                         // Create a temp graphics for centered label positioning
    153                         if (Browser._theBrowser != null && Browser._theBrowser.g != null) {
    154                                 g = Browser._theBrowser.g.create();
    155                         } else {
    156                                 g = new BufferedImage(1,1,BufferedImage.TYPE_BYTE_INDEXED).getGraphics();
    157                         }
    158                        
    159                         do {
    160        
    161                                 majorTickPositions.add(new Integer((int)currentTickX));
    162 
    163                                 String label = createSpecificTimeLabel((long)currentTickTime, timeAggregation);
    164                                
    165                                 majorTickLabels.add(label);
    166 
    167                                 // Center label
    168                                 FontMetrics fm   = g.getFontMetrics(TIME_BAR_FONT);
    169                                 Rectangle2D rect = fm.getStringBounds(label, g);
    170                                 int textWidth  = (int)(rect.getWidth());
    171                                
    172                                 majorTickLabelPositions.add(new Integer((int)currentTickX - (textWidth >> 1)));
    173 
    174                                 currentTickX += majorTickPixelSpacing;
    175                                 currentTickTime += timeAggregation;
    176        
    177                         } while (currentTickX < width);
    178 
    179                        
    180                 } finally {
    181                         if (g != null)
    182                                 g.dispose();
    183                 }
     149                do {
     150                        majorTickPositions.add(new Integer((int)currentTickX));
     151
     152                        String label = createSpecificTimeLabel((long)currentTickTime, timeAggregation);
     153                       
     154                        majorTickLabels.add(label);
     155
     156                        // Center label
     157                        int textWidth = EcosystemManager.getTextLayoutManager().layoutStringSimple(label, TIME_BAR_FONT).getPixelBounds(0, 0).getWidth();
     158                       
     159                        majorTickLabelPositions.add(new Integer((int)currentTickX - (textWidth >> 1)));
     160
     161                        currentTickX += majorTickPixelSpacing;
     162                        currentTickTime += timeAggregation;
     163                       
     164                } while (currentTickX < width);
    184165               
    185166                this.majorTickLabels = majorTickLabels.toArray(new String[0]);
     
    227208       
    228209       
    229         public String createSpecificTimeLabel(long atTime, double timeAggregation) {
    230 
     210        public String createSpecificTimeLabel(long atTime, double timeAggregation)
     211        {
    231212                Long min = null;
    232213                Long sec = null;
     
    261242        }
    262243       
    263         public static String createTimeLabel(Long min, Long sec, Long ms) {
    264                
     244        public static String createTimeLabel(Long min, Long sec, Long ms)
     245        {
    265246                if (min == null && sec == null && ms != null) {
    266247                        return ms + "ms";
     
    314295        }
    315296       
    316         public void paint(Graphics g, int x, int y, int height, Color backgroundColor) {
     297        public void paint(int x, int y, int height, Colour backgroundColor) {
    317298               
    318299                int majorTickHeight = height / 2;
     
    320301
    321302                // Draw backing
     303                Fill backingFill;
    322304                if (ApolloSystem.useQualityGraphics) {
    323                        
    324                         GradientPaint gp = new GradientPaint(
    325                                         x + (width / 2), y + (int)(height * 0.5), backgroundColor,
    326                                         x + (width / 2), y, SampledTrackGraphView.DEFAULT_BACKGROUND_HIGHTLIGHTS_COLOR);
    327                         ((Graphics2D)g).setPaint(gp);
    328                        
     305                        backingFill = new GradientFill(backgroundColor, new Point(x + (width / 2), y + (int)(height * 0.5)), SwingConversions.fromSwingColor(SampledTrackGraphView.DEFAULT_BACKGROUND_HIGHTLIGHTS_COLOR), new Point(x + (width / 2), y));
    329306                } else {
    330                         g.setColor(backgroundColor);
    331                 }
    332                
    333                 g.fillRect(x, y, width, height);
    334                
    335                 g.setFont(TIME_BAR_FONT);
     307                        backingFill = new Fill(backgroundColor);
     308                }
     309               
     310                GraphicsManager gm = EcosystemManager.getGraphicsManager();
     311               
     312                gm.drawRectangle(new Point(x, y), new Dimension(width, height), 0.0, backingFill, null, null, null);
    336313               
    337314                if (majorTickXPositions != null) {
    338 
    339                         ((Graphics2D)g).setStroke(TICK_STROKE);
    340                        
    341                         g.setColor(MAJOR_TICK_COLOR);
    342315                        // Draw Major ticks and labels
    343316                        for (int i = 0; i < majorTickLabels.length; i++) {
    344317                               
    345318                                // Label
    346                                 g.drawString(
     319                                gm.drawString(
    347320                                                majorTickLabels[i],
    348                                                 x + majorTickLabelXPositions[i],
    349                                                 y + height - 2);
     321                                                new Point(x + majorTickLabelXPositions[i], y + height - 2),
     322                                                TIME_BAR_FONT,
     323                                                MAJOR_TICK_COLOR);
    350324                               
    351325                                // Tick
    352                                 g.drawLine(
    353                                                 x + majorTickXPositions[i],
     326                                gm.drawLine(x + majorTickXPositions[i],
    354327                                                y,
    355328                                                x + majorTickXPositions[i],
    356                                                 y + majorTickHeight);
     329                                                y + majorTickHeight, MAJOR_TICK_COLOR, TICK_STROKE);
    357330                               
    358331                        }
    359                        
    360                         g.setColor(MINOR_TICK_COLOR);
    361332                       
    362333                        // Draw minor ticks
    363334                        if (minorTickXPositions != null) {
    364335                                for (int tx : minorTickXPositions) {
    365                                         g.drawLine(
     336                                        gm.drawLine(
    366337                                                        x + tx,
    367338                                                        y,
    368339                                                        x + tx,
    369                                                         y + minorTickHeight);
     340                                                        y + minorTickHeight, MINOR_TICK_COLOR, TICK_STROKE);
    370341                                }
    371342                        }
     
    373344                } else if (alternateLabel != null) {
    374345                       
    375                         g.setColor(Color.BLACK);
    376                        
    377                         FontMetrics fm   = g.getFontMetrics(TIME_BAR_FONT);
    378                         Rectangle2D rect = fm.getStringBounds(alternateLabel, g);
    379                         int textWidth  = (int)(rect.getWidth());
    380                        
    381                         g.drawString(
    382                                         alternateLabel,
    383                                         x + ((width >> 1) - (textWidth >> 1)),
    384                                         y + (height - 2));
    385 
    386                 }
    387                
    388         }
    389 
    390         public int getWidth() {
     346                        TextLayout layout = EcosystemManager.getTextLayoutManager().layoutStringSimple(alternateLabel, TIME_BAR_FONT);
     347
     348                        int textWidth  = layout.getPixelBounds(0, 0).getWidth();
     349                        gm.drawTextLayout(layout, new Point(x + ((width >> 1) - (textWidth >> 1)), y + (height - 2)), Colour.BLACK);
     350                       
     351                }
     352               
     353        }
     354
     355        public int getWidth()
     356        {
    391357                return width;
    392358        }
Note: See TracChangeset for help on using the changeset viewer.