private void draw(Graphics2D g) {
        int width = substrate.getWidth();
        int height = substrate.getHeight();
        g.setBackground(Color.WHITE);
        g.clearRect(0, 0, width, height);

        if (hscale == 0 || queue == null) {
            return;
        }
        int ticks = (int) (width / hscale);
        int len = queue.size();
        int scanback;
        if (len > ticks) {
            scanback = ticks;
        } else {
            scanback = len;
        }

        rescaleBuffers(scanback + 2);

        int last_span = 0;
        for (float[] values : queue) {
            int span = values.length;
            for (int i = 0; i < span; i++) {
                // add values to set
                pcounts[i] += 1;
                int pos = pcounts[i];
                xbuffer[i][pos] = (int) (scanback * hscale);
                ybuffer[i][pos] = (int) linearScale(values[i], ymin, ymax, (float) height, 0.0f);
            }

            for (int i = span; i < last_span; i++) {
                // terminate set
                int cutoff = pcounts[i];
                if (cutoff > 0) {
                    System.out.format("c %d\n", cutoff);
                    g.setColor(colors[i]);
                    g.drawPolyline(xbuffer[i], ybuffer[i], cutoff + 1);
                }
                pcounts[i] = -1;
            }

            last_span = span;
            scanback--;
            if (scanback < 0) {
                break;
            }
        }
        // finalize all tracks making it to the end
        for (int i = 0; i < last_span; i++) {
            g.setColor(colors[i]);
            g.drawPolyline(xbuffer[i], ybuffer[i], pcounts[i] + 1);
        }
    }