Annotations.Graph can be bargraph, linegraph
[jalview.git] / src / jalview / gui / AnnotationPanel.java
index 9c2aac0..8f72f71 100755 (executable)
@@ -76,6 +76,12 @@ public class AnnotationPanel extends JPanel implements MouseListener,
         ap.annotationScroller.getVerticalScrollBar().addAdjustmentListener(this);\r
     }\r
 \r
+    public AnnotationPanel(AlignViewport av)\r
+    {\r
+      this.av = av;\r
+    }\r
+\r
+\r
     /**\r
      * DOCUMENT ME!\r
      *\r
@@ -89,7 +95,7 @@ public class AnnotationPanel extends JPanel implements MouseListener,
     /**\r
      * DOCUMENT ME!\r
      */\r
-    public void adjustPanelHeight()\r
+    public int adjustPanelHeight()\r
     {\r
         // setHeight of panels\r
         image = null;\r
@@ -117,7 +123,7 @@ public class AnnotationPanel extends JPanel implements MouseListener,
                     aa[i].height += 16;\r
                 }\r
 \r
-                if (aa[i].isGraph)\r
+                if (aa[i].graph>0)\r
                 {\r
                     aa[i].height += GRAPH_HEIGHT;\r
                 }\r
@@ -136,6 +142,8 @@ public class AnnotationPanel extends JPanel implements MouseListener,
         }\r
 \r
         this.setPreferredSize(new Dimension(1, height));\r
+\r
+        return height;\r
     }\r
 \r
     /**\r
@@ -148,6 +156,8 @@ public class AnnotationPanel extends JPanel implements MouseListener,
         if (activeRow == -1)\r
         {\r
             AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
+            if(aa==null)\r
+              return;\r
 \r
             for (int j = 0; j < aa.length; j++)\r
             {\r
@@ -178,6 +188,8 @@ public class AnnotationPanel extends JPanel implements MouseListener,
         if (activeRow == -1)\r
         {\r
             AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
+            if(aa==null)\r
+              return;\r
 \r
             for (int j = 0; j < aa.length; j++)\r
             {\r
@@ -526,7 +538,7 @@ public class AnnotationPanel extends JPanel implements MouseListener,
       g.setColor(Color.white);\r
       g.fillRect(0, 0, getWidth(), getHeight());\r
 \r
-      if (fastPaint)\r
+      if (fastPaint && image!=null)\r
       {\r
         g.drawImage(image, 0, 0, this);\r
         fastPaint = false;\r
@@ -602,8 +614,14 @@ public class AnnotationPanel extends JPanel implements MouseListener,
      * @param startRes DOCUMENT ME!\r
      * @param endRes DOCUMENT ME!\r
      */\r
-    public void drawComponent(Graphics2D g, int startRes, int endRes)\r
+    public void drawComponent(Graphics g, int startRes, int endRes)\r
     {\r
+      g.setFont(av.getFont());\r
+\r
+      if (fm == null)\r
+        fm = g.getFontMetrics();\r
+\r
+\r
         g.setColor(Color.white);\r
         g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());\r
 \r
@@ -639,7 +657,7 @@ public class AnnotationPanel extends JPanel implements MouseListener,
             }\r
 \r
 \r
-            if (row.isGraph)\r
+            if (row.graph>0)\r
             {\r
                 // this is so that we draw the characters below the graph\r
                 y += row.height;\r
@@ -701,7 +719,7 @@ public class AnnotationPanel extends JPanel implements MouseListener,
                                 0))) / 2;\r
                     g.setColor(row.annotations[j].colour);\r
 \r
-                    if (j == 0 || row.isGraph)\r
+                    if (j == 0 || row.graph>0)\r
                     {\r
                         g.drawString(row.annotations[j].displayCharacter, x+charOffset,\r
                             y + iconOffset + 3);\r
@@ -766,33 +784,19 @@ public class AnnotationPanel extends JPanel implements MouseListener,
                     }\r
                 }\r
 \r
-                if (validRes && row.isGraph)\r
+                if (validRes && row.graph>0)\r
                 {\r
-                    g.setColor(new Color(0, 0, 180));\r
-\r
-                    int height = (int) ((row.annotations[j].value / row.graphMax) * GRAPH_HEIGHT);\r
-\r
-                    if (row.windowLength > 1)\r
-                    {\r
-                        int total = 0;\r
+                    ///g.setColor(new Color(0, 0, 180));\r
 \r
-                        for (int i2 = j - (row.windowLength / 2);\r
-                                i2 < (j + (row.windowLength / 2)); i2++)\r
-                        {\r
-                            if ((i2 < 0) || (i2 >= av.alignment.getWidth()))\r
-                            {\r
-                                continue;\r
-                            }\r
+                    if(row.graph== AlignmentAnnotation.LINE_GRAPH )\r
+                        drawLineGraph(g, row, j, j+1, x, y);\r
+                    else if(row.graph == AlignmentAnnotation.BAR_GRAPH )\r
+                       drawBarGraph(g, row, j, j+1, x, y);\r
 \r
-                            total += row.annotations[i2].value;\r
-                        }\r
-\r
-                        total /= row.windowLength;\r
-                        height = (int) ((total / row.graphMax) * GRAPH_HEIGHT);\r
-                    }\r
+                    //int height = (int) ((row.annotations[j].value / row.graphMax) * GRAPH_HEIGHT);\r
 \r
-                    g.setColor(row.annotations[j].colour);\r
-                    g.fillRect(x, y - height, av.charWidth, height);\r
+                   // g.setColor(row.annotations[j].colour);\r
+                   // g.fillRect(x, y - height, av.charWidth, height);\r
                 }\r
             }\r
 \r
@@ -833,34 +837,73 @@ public class AnnotationPanel extends JPanel implements MouseListener,
                 }\r
             }\r
 \r
-            if (row.isGraph && row.hasText)\r
+            if (row.graph>0 && row.hasText)\r
             {\r
                 y += av.charHeight;\r
             }\r
 \r
-            if (!row.isGraph)\r
+            if (row.graph==0)\r
             {\r
                 y += aa[i].height;\r
             }\r
         }\r
     }\r
 \r
+    public void drawLineGraph(Graphics g, AlignmentAnnotation aa, int sRes, int eRes, int x, int y)\r
+    {\r
+      g.setColor(new Color(0, 0, 0));\r
+      if(sRes==0)\r
+        sRes++;\r
+\r
+      int y1=y, y2=y;\r
+      for (int j = sRes; j < eRes; j++)\r
+      {\r
+          if(aa.annotations[j-1]!=null)\r
+            y1 = y - (int) ((aa.annotations[j-1].value / aa.graphMax) * GRAPH_HEIGHT);\r
+\r
+          if(aa.annotations[j]!=null)\r
+          y2 = y - (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);\r
+\r
+          g.drawLine(x-av.charWidth/2, y1, x+av.charWidth/2, y2);\r
+\r
+          x += av.charWidth;\r
+        }\r
+    }\r
+\r
+    public void drawBarGraph(Graphics g, AlignmentAnnotation aa, int sRes, int eRes, int x, int y)\r
+    {\r
+      g.setColor(new Color(0, 0, 180));\r
+      int j, height=0;\r
+      for (j = sRes; j < eRes; j++)\r
+      {\r
+          g.setColor(aa.annotations[j].colour);\r
+          height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);\r
+\r
+          g.fillRect(x, y - height, av.charWidth, height);\r
+          x += av.charWidth;\r
+        }\r
+    }\r
+\r
     // used by overview window\r
     public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y, int sRes, int eRes)\r
     {\r
-        g.setColor(Color.white);\r
-        g.fillRect(0, 0, width, y);\r
-        g.setColor(new Color(0, 0, 180));\r
+      g.setColor(Color.white);\r
+      g.fillRect(0, 0, width, y);\r
+      g.setColor(new Color(0, 0, 180));\r
 \r
-        int x = 0;\r
+      int x = 0, height;\r
 \r
-        for (int j = sRes; j < eRes; j++)\r
-        {\r
-            g.setColor(new Color(0, 0, 180));\r
+      for (int j = sRes; j < eRes; j++)\r
+      {\r
+          g.setColor(new Color(0, 0, 180));\r
 \r
-            int height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);\r
-            g.fillRect(x, y - height, av.charWidth, height);\r
-            x += av.charWidth;\r
-        }\r
+          height = (int) ((aa.annotations[j].value / aa.graphMax) * y);\r
+          if(height>y)\r
+            height = y;\r
+\r
+          g.fillRect(x, y - height, av.charWidth, height);\r
+          x += av.charWidth;\r
+      }\r
     }\r
+\r
 }\r