Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.41 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package org.rp181.datarender;
  6.  
  7. import java.awt.Color;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics;
  10. import java.awt.image.BufferedImage;
  11. import org.rp181.dataview.CurrentDataTopComponent;
  12. import org.rp181.dataview.SettingsTopComponent;
  13. import org.rp181.logicdata.Logic8Set;
  14.  
  15. /**
  16.  *
  17.  * @author phani
  18.  */
  19. public class Renderer {
  20.  
  21.     private static Renderer instance = null;
  22.     ColorSet defaultColorSet = new ColorSet();
  23.     //
  24.     public static final int LEFT_SIDE_MARGIN = 100;
  25.     private static final int TOP_MARGIN = 40;
  26.     private double zoom = 1;
  27.     /**
  28.      *
  29.      */
  30.     private int xoff = 0;
  31.     private int width = -1;
  32.     private int height = -1;
  33.     private int[] chanMap = new int[8];
  34.     FontMetrics fm;
  35.     boolean color = true;
  36.     public boolean leftMargin = true;
  37.     boolean topMargin = true;
  38.     boolean coloredLines = true;
  39.     public int currentChannel = 1;
  40.     public int currentRow = 1;
  41.     BufferedImage image;
  42.     Graphics g = null;
  43.     int sampleStep;
  44.     int rowHeight;
  45.     int data[];
  46.     double time;
  47.     Logic8Set currentSet;
  48.     SettingsTopComponent settings;
  49.     //
  50.     BufferedImage background;
  51.     Graphics backgroundGraphics;
  52.     BufferedImage channels;
  53.     Graphics channelsGraphics;
  54.     BufferedImage highlight;
  55.     Graphics highlightGraphics;
  56.     BufferedImage datadisplay;
  57.     Graphics dataGraphics;
  58.     BufferedImage cursors;
  59.     Graphics cursorsGraphics;
  60.     int oldRowsActivated = -1;
  61.     boolean updateBackground = true;
  62.     boolean updateChannels = true;
  63.     boolean updateData = true;
  64.  
  65.     public Renderer getInstance() {
  66.         if (instance == null) {
  67.             instance = new Renderer();
  68.         }
  69.         return instance;
  70.     }
  71.  
  72.     public void setZoom(double z) {
  73.         zoom = z;
  74.         updateData = true;
  75.     }
  76.  
  77.     public double getZoom() {
  78.         return zoom;
  79.     }
  80.  
  81.     public void setOffset(int x) {
  82.         xoff = x;
  83.         updateData = true;
  84.     }
  85.  
  86.     public int getOffset() {
  87.         return xoff;
  88.     }
  89.     Color darkBg = new Color(20, 20, 20);
  90.     int left;
  91.     int top;
  92.     int xP;
  93.     int yP;
  94.  
  95.     public BufferedImage render(int w, int h, int xP, int yP, Logic8Set dat) {
  96.         this.xP = xP;
  97.         this.yP = yP;
  98.         currentSet = dat;
  99.         settings = SettingsTopComponent.getDefault();
  100.         if (settings.colorB.isSelected()) {
  101.             defaultColorSet.setColorMode(ColorSet.MODE_COLOR);
  102.         } else {
  103.             defaultColorSet.setColorMode(ColorSet.MODE_BLACK_AND_WHITE);
  104.  
  105.         }
  106.  
  107.         //if (upToDate == false)
  108.         {
  109.             try {
  110.                 color = settings.colorB.isSelected();
  111.                 leftMargin = settings.leftM.isSelected();
  112.                 topMargin = settings.topM.isSelected();
  113.                 coloredLines = settings.colorLines.isSelected();
  114.                 if (!leftMargin) {
  115.                     left = 0;
  116.                 } else {
  117.                     left = LEFT_SIDE_MARGIN;
  118.                 }
  119.  
  120.                 if (!topMargin) {
  121.                     top = 0;
  122.                 } else {
  123.                     top = TOP_MARGIN;
  124.                 }
  125.  
  126.                 if (w != width || h != height) {
  127.                     width = w;
  128.                     height = h;
  129.  
  130.  
  131.  
  132.                     background = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  133.                     backgroundGraphics = background.createGraphics();
  134.  
  135.                     cursors = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  136.                     cursorsGraphics = background.createGraphics();
  137.  
  138.                     channels = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  139.                     channelsGraphics = background.createGraphics();
  140.  
  141.                     datadisplay = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  142.                     dataGraphics = background.createGraphics();
  143.  
  144.                     highlight = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  145.                     highlightGraphics = background.createGraphics();
  146.  
  147.                     updateBackground = true;
  148.                     updateData = true;
  149.                 }
  150.  
  151.                 image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  152.                 g = image.createGraphics();
  153.                 fm = g.getFontMetrics();
  154.  
  155.  
  156.                 sampleStep = (int) ((currentSet.getSampleLength() / (long) width) - left);
  157.                 sampleStep = (int) ((double) sampleStep / zoom);
  158.                 rowHeight = (height - top) / rowsActivated();
  159.  
  160.                 while ((xoff + width) * sampleStep > currentSet.getSampleLength()) {
  161.                     xoff--;
  162.                 }
  163.                 xoff = Math.max(0, xoff);
  164.                 currentRow = -1;
  165.  
  166.                 for (int d = 1; d <= 8; d++) {
  167.                     if (isActivated(d)) {
  168.                         currentRow++;
  169.                         chanMap[d - 1] = currentRow;
  170.                     } else {
  171.                         chanMap[d - 1] = -1;
  172.                     }
  173.                 }
  174.  
  175.                 if (updateBackground) {
  176.                     drawBackground();
  177.                 }
  178.                 if (oldRowsActivated != rowsActivated()) {
  179.                     drawChannels();
  180.                 }
  181.                 if (settings.chanHigh.isSelected() && color && yP > 0) {
  182.                     drawHighlight();
  183.                 }
  184.                 if (updateData) {
  185.                     drawData();
  186.                 }
  187.                 updateCursors();
  188.  
  189.                 g.drawImage(background, 0, 0, null);
  190.                 g.drawImage(channels, 0, 0, null);
  191.                 if (settings.chanHigh.isSelected() && color && yP > 0) {
  192.                     g.drawImage(highlight, 0, 0, null);
  193.                 }
  194.                 g.drawImage(datadisplay, 0, 0, null);
  195.                 g.drawImage(cursors, 0, 0, null);
  196.                 return image;
  197.  
  198.             } catch (Exception e) {
  199.                 e.printStackTrace();
  200.                 g.drawImage(background, 0, 0, null);
  201.                 g.drawImage(channels, 0, 0, null);
  202.                 return image;
  203.             }
  204.         }
  205.  
  206.         //return null;
  207.     }
  208.  
  209.     public void drawInfo(Graphics g, int x, int y, int rowHeight, long sampleStep) {
  210.         int rowY = y;
  211.         if (topMargin) {
  212.             rowY -= TOP_MARGIN;
  213.         }
  214.         int row = 0;
  215.         while (rowY > rowHeight) {
  216.             rowY -= rowHeight;
  217.             row++;
  218.         }
  219.         int d = -1;
  220.         for (int i = 0; i < 8; i++) {
  221.             if (chanMap[i] == row) {
  222.                 d = i + 1;
  223.             }
  224.         }
  225.  
  226.         int xPos = x;
  227.         if (leftMargin) {
  228.             xPos -= LEFT_SIDE_MARGIN;
  229.         }
  230.         //xPos = xoff;
  231.         CurrentDataTopComponent.getDefault().channel.setText("Channel " + (row + 1));
  232.         Long[] cData = currentSet.getCompressedData(row);
  233.         CurrentDataTopComponent.getDefault().stateChanges.setText("State Changes: " + cData.length + "");
  234.         CurrentDataTopComponent.getDefault().totalTime.setText("Total Time: " + currentSet.getSampleLength() * currentSet.getTimeStep() + "");
  235.         CurrentDataTopComponent.getDefault().startSample.setText("Start Sample #: ");
  236.         CurrentDataTopComponent.getDefault().stopSample.setText("Stop Sample #: ");
  237.         CurrentDataTopComponent.getDefault().time.setText("Segment Time: ");
  238.         long cursor = getSampleNumber(xPos);
  239.  
  240.         long sampleNum = 0;
  241.         long oldSampleNum = 0;
  242.         for (int i = 0; i < cData.length; i++) {
  243.             oldSampleNum = sampleNum;
  244.             sampleNum += cData[i];
  245.             if (cursor <= sampleNum) {
  246.                 g.setColor(defaultColorSet.getInfoLineColor());
  247.                 CurrentDataTopComponent.getDefault().startSample.setText("Start Sample #: " + oldSampleNum);
  248.                 CurrentDataTopComponent.getDefault().stopSample.setText("Stop Sample #: " + sampleNum);
  249.                 CurrentDataTopComponent.getDefault().time.setText("Segment Time: " + ((sampleNum - oldSampleNum) * currentSet.getTimeStep()) + " S");
  250.                 if (!leftMargin) {
  251.                     g.drawLine((int) (oldSampleNum / sampleStep) - xoff, y, (int) (sampleNum / sampleStep) - xoff, y);
  252.                 } else {
  253.                     g.drawLine(Math.max(LEFT_SIDE_MARGIN, (int) (oldSampleNum / sampleStep) - xoff + LEFT_SIDE_MARGIN), y, (int) (sampleNum / sampleStep) - xoff + LEFT_SIDE_MARGIN, y);
  254.                 }
  255.  
  256.                 break;
  257.             }
  258.         }
  259.     }
  260.  
  261.     public boolean intToBoolean(int i) {
  262.         if (i > 0) {
  263.             return true;
  264.         } else {
  265.             return false;
  266.         }
  267.     }
  268.  
  269.     public int getCurrentChannel() {
  270.         return currentChannel;
  271.     }
  272.     Color bgColor = new Color(80, 80, 80);
  273.  
  274.     private void highlight(int yP, int rowHeight, Graphics g, int left, int top, int sampleStep) {
  275.         int rowY = yP;
  276.         if (topMargin) {
  277.             rowY -= TOP_MARGIN;
  278.         }
  279.         int row = 0;
  280.         while (rowY > rowHeight) {
  281.             rowY -= rowHeight;
  282.             row++;
  283.         }
  284.         int d = -1;
  285.         for (int i = 0; i < 8; i++) {
  286.             if (chanMap[i] == row) {
  287.                 d = i + 1;
  288.             }
  289.         }
  290.  
  291.         if (color && d > 0) {
  292.             drawPlate(bgColor, g, left, (rowHeight) * row + top, width - left, rowHeight - 10);
  293.             g.setColor(Color.white);
  294.             if (coloredLines) {
  295.                 g.setColor(defaultColorSet.getChannelColor(d));
  296.                 if (d == 8) {
  297.                     g.setColor(Color.white);
  298.                 }
  299.             }
  300.             int prev = -1;
  301.             for (int i = 0; i < width; i++) {
  302.                 data = currentSet.getSample((i + xoff) * sampleStep);
  303.                 if (prev < 0) {
  304.                     prev = data[d - 1];
  305.                 } else {
  306.                     if (data[d - 1] != prev) {
  307.                         prev = data[d - 1];
  308.                         g.drawLine(i - 1 + left, ((rowHeight / 5) + rowHeight * row) + top, i - 1 + left, ((rowHeight / 5) * 4 + rowHeight * row) + top);
  309.                     }
  310.                 }
  311.                 boolean thick = false;
  312.                 int y = ((rowHeight) / 5) * data[d - 1];
  313.                 if (y == 0) {
  314.                     y = ((rowHeight) / 5) * 4;
  315.                     thick = true;
  316.                 }
  317.                 y += (rowHeight) * row;
  318.                 y += top;
  319.                 if (!thick) {
  320.                     g.fillRect(i + left, y, 1, 1);
  321.                 } else {
  322.                     if (settings.thickBottom.isSelected()) {
  323.                         g.fillRect(i + left, y - 1, 1, 1);
  324.                     }
  325.                     g.fillRect(i + left, y, 1, 1);
  326.                 }
  327.             }
  328.         }
  329.     }
  330.  
  331.     public long getSampleNumber(int x) {
  332.         long sampleNum = -1;
  333.         if (x > width || x < 0) {
  334.             return sampleNum;
  335.         } else {
  336.             int left;
  337.             if (!leftMargin) {
  338.                 left = 0;
  339.             } else {
  340.                 left = LEFT_SIDE_MARGIN;
  341.             }
  342.  
  343.             int sampleStepC = (int) ((currentSet.getSampleLength() / (long) width) - left);
  344.             sampleStepC = (int) ((double) sampleStepC / zoom);
  345.             return (x + xoff) * sampleStepC;
  346.         }
  347.     }
  348.     Color co;
  349.  
  350.     /**
  351.      *
  352.      * @param c
  353.      * @param g
  354.      * @param x
  355.      * @param y
  356.      * @param width
  357.      * @param height
  358.      */
  359.     public void drawPlate(Color c, Graphics g, int x, int y, int width, int height) {
  360.         for (int i = 0; i <= 10; i++) {
  361.             co = new Color(Math.max(c.getRed() - (100 - i * 10), 0), Math.max(c.getGreen() - (100 - i * 10), 0), Math.max(c.getBlue() - (100 - i * 10), 0));
  362.             g.setColor(co);
  363.             g.fillRect(x, y + i, width, height);
  364.         }
  365.  
  366.         g.setColor(c);
  367.         g.fillRect(x, y + 10, width, height - 10);
  368.     }
  369.  
  370.     public void repaintNow() {
  371.         updateBackground = true;
  372.         updateData = true;
  373.     }
  374.  
  375.     /**
  376.      *
  377.      * @param i
  378.      * @return
  379.      */
  380.     public boolean isActivated(int i) {
  381.         return settings.chanField.getText().contains(i + "");
  382.     }
  383.  
  384.     /**
  385.      *
  386.      * @return
  387.      */
  388.     public int rowsActivated() {
  389.         String str = settings.chanField.getText();
  390.  
  391.         int rows = 0;
  392.  
  393.         if (str.contains("1")) {
  394.             rows++;
  395.         }
  396.         if (str.contains("2")) {
  397.             rows++;
  398.         }
  399.         if (str.contains("3")) {
  400.             rows++;
  401.         }
  402.         if (str.contains("4")) {
  403.             rows++;
  404.         }
  405.         if (str.contains("5")) {
  406.             rows++;
  407.         }
  408.         if (str.contains("6")) {
  409.             rows++;
  410.         }
  411.         if (str.contains("7")) {
  412.             rows++;
  413.         }
  414.         if (str.contains("8")) {
  415.             rows++;
  416.         }
  417.         return rows;
  418.     }
  419.  
  420.     private void drawBackground() {
  421.         backgroundGraphics.setColor(defaultColorSet.getBackgroundColor());
  422.         backgroundGraphics.fillRect(0, 0, width, height);
  423.     }
  424.  
  425.     private void drawChannels() {
  426.         //channels = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  427.         //channelsGraphics = channels.createGraphics();
  428.         channelsGraphics.setColor(clear);
  429.         channelsGraphics.fillRect(0, 0, width, height);
  430.         for (int d = 1; d <= 8; d++) {
  431.             currentRow = chanMap[d - 1];
  432.             if (isActivated(d)) {
  433.  
  434.                 if (color) {
  435.                     drawPlate(defaultColorSet.getBackgroundColor(), channelsGraphics, left, (rowHeight) * currentRow + top, width - left, rowHeight);
  436.                 }
  437.  
  438.                 if (leftMargin) {
  439.                     if (color) {
  440.                         channelsGraphics.setColor(defaultColorSet.getChannelColor(d));
  441.                         drawPlate(defaultColorSet.getChannelColor(d), channelsGraphics, 0, (rowHeight) * currentRow + top, 20, rowHeight);
  442.                         drawPlate(darkBg, channelsGraphics, 20, (rowHeight) * currentRow + top, left - 20, rowHeight);
  443.                     }
  444.                     if (settings.chanText.isSelected()) {
  445.                         channelsGraphics.setColor(defaultColorSet.getTextColor());
  446.                         channelsGraphics.drawString("Channel " + d, 30, (rowHeight) * (currentRow) + top + 25);
  447.                     }
  448.                 }
  449.             }
  450.         }
  451.     }
  452.  
  453.     private void drawHighlight() {
  454.         // highlight = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  455.         // highlightGraphics = highlight.createGraphics();
  456.         highlightGraphics.setColor(clear);
  457.         highlightGraphics.fillRect(0, 0, width, height);
  458.         highlight(yP, rowHeight, highlightGraphics, left, top, sampleStep);
  459.     }
  460.  
  461.     private void drawData() {
  462.         // datadisplay = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  463.         //dataGraphics = datadisplay.createGraphics();
  464.         dataGraphics.setColor(clear);
  465.         dataGraphics.fillRect(0, 0, width, height);
  466.         for (int d = 1; d <= 8; d++) {
  467.             if (isActivated(d)) {
  468.                 currentRow = chanMap[d - 1];
  469.                 int prev = -1;
  470.                 for (int i = 0; i < width; i++) {
  471.  
  472.                     if (i % 200 == 0 && settings.timingMarkers.isSelected()) {
  473.                         dataGraphics.setColor(defaultColorSet.getTextColor());
  474.  
  475.                         time = ((i + xoff) * sampleStep) * currentSet.getTimeStep();
  476.                         String str = "";
  477.                         String str2 = null;
  478.                         if (time >= 1 || time == 0) {
  479.                             str = time + "";
  480.                             str = str.substring(0, 1);
  481.                             str = str + " S";
  482.                             str2 = ((time - Integer.parseInt(str.replaceAll(" S", ""))) * 1000) + "";
  483.                             str2 = str2.substring(0, Math.min(6, str2.length()));
  484.  
  485.                             if (Double.parseDouble(str2) == 0) {
  486.                                 str2 = null;
  487.                             } else {
  488.                                 str2 = str2.concat(" ms");
  489.                             }
  490.                             dataGraphics.drawString(str, i + left - (fm.stringWidth(str) / 2), top - 25);
  491.                             if (str2 != null) {
  492.                                 dataGraphics.drawString(str2, i + left - (fm.stringWidth(str2) / 2), top - 12);
  493.                             }
  494.                         } else {
  495.                             str = (time * 1000) + "";
  496.                             str = str.substring(0, Math.min(5, str.length()));
  497.                             str = str + " ms";
  498.                             dataGraphics.drawString(str, i + left - (fm.stringWidth(str) / 2), top - 12);
  499.  
  500.                         }
  501.  
  502.                         dataGraphics.fillRect(i + left - 2, top - 5, 4, 4);
  503.  
  504.                     }
  505.                     if (!color) {
  506.                         dataGraphics.setColor(Color.black);
  507.                     } else if (coloredLines) {
  508.                         dataGraphics.setColor(defaultColorSet.getChannelColor(d));
  509.                         if (d == 8) {
  510.                             dataGraphics.setColor(Color.white);
  511.                         }
  512.                     }
  513.  
  514.                     data = currentSet.getSample((i + xoff) * sampleStep);
  515.  
  516.                     if (prev < 0) {
  517.                         prev = data[d - 1];
  518.                     } else {
  519.                         if (data[d - 1] != prev) {
  520.                             prev = data[d - 1];
  521.                             dataGraphics.drawLine(i - 1 + left, ((rowHeight / 5) + rowHeight * currentRow) + top,
  522.                                     i - 1 + left, ((rowHeight / 5) * 4 + rowHeight * currentRow) + top);
  523.                         }
  524.                     }
  525.                     boolean thick = false;
  526.                     int y = ((rowHeight) / 5) * data[d - 1];
  527.                     if (y == 0) {
  528.                         y = ((rowHeight) / 5) * 4;
  529.                         thick = true;
  530.                     }
  531.                     y += (rowHeight) * currentRow;
  532.                     y += top;
  533.                     if (!thick) {
  534.                         dataGraphics.fillRect(i + left, y, 1, 1);
  535.                     } else {
  536.                         if (settings.thickBottom.isSelected()) {
  537.                             dataGraphics.fillRect(i + left, y - 1, 1, 1);
  538.                         }
  539.  
  540.                         dataGraphics.fillRect(i + left, y, 1, 1);
  541.  
  542.                     }
  543.                 }
  544.             }
  545.         }
  546.     }
  547.  
  548.     private void updateCursors() {
  549.         // cursors = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  550.         //cursorsGraphics = cursors.createGraphics();
  551.         cursorsGraphics.setColor(clear);
  552.         cursorsGraphics.fillRect(0, 0, width, height);
  553.         if (settings.vertCursor.isSelected()) {
  554.             cursorsGraphics.setColor(defaultColorSet.getVerticalCursorColor());
  555.             if (leftMargin) {
  556.                 cursorsGraphics.drawLine(Math.max(0, xP), top, xP, height);
  557.             } else {
  558.                 cursorsGraphics.drawLine(xP, top, xP, height);
  559.             }
  560.         }
  561.         drawInfo(cursorsGraphics, xP, yP, rowHeight, sampleStep);
  562.     }
  563.     private Color clear = new Color(255, 255, 255, 0);
  564. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement