graph now has type, not a boolean
[jalview.git] / src / jalview / gui / AnnotationPanel.java
index 4d3fc0f..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,9 +95,10 @@ 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
         AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
         int height = 0;\r
 \r
@@ -116,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
@@ -135,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
@@ -147,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
@@ -177,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
@@ -332,7 +345,10 @@ public class AnnotationPanel extends JPanel implements MouseListener,
 \r
         for (int i = 0; i < aa.length; i++)\r
         {\r
-            height += aa[i].height;\r
+            if (aa[i].visible)\r
+            {\r
+              height += aa[i].height;\r
+            }\r
 \r
             if (evt.getY() < height)\r
             {\r
@@ -519,29 +535,32 @@ public class AnnotationPanel extends JPanel implements MouseListener,
      */\r
     public void paintComponent(Graphics g)\r
     {\r
-        g.setColor(Color.white);\r
-        g.fillRect(0, 0, getWidth(), getHeight());\r
-\r
-        if (fastPaint)\r
-        {\r
-            g.drawImage(image, 0, 0, this);\r
-            fastPaint = false;\r
+      g.setColor(Color.white);\r
+      g.fillRect(0, 0, getWidth(), getHeight());\r
 \r
-            return;\r
-        }\r
+      if (fastPaint && image!=null)\r
+      {\r
+        g.drawImage(image, 0, 0, this);\r
+        fastPaint = false;\r
+        return;\r
+      }\r
 \r
-        imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;\r
+      imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;\r
 \r
+      if (image == null || imgWidth != image.getWidth())\r
+      {\r
         image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),\r
-                BufferedImage.TYPE_INT_RGB);\r
+                                  BufferedImage.TYPE_INT_RGB);\r
         gg = (Graphics2D) image.getGraphics();\r
         gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
-            RenderingHints.VALUE_ANTIALIAS_ON);\r
+                            RenderingHints.VALUE_ANTIALIAS_ON);\r
 \r
         gg.setFont(av.getFont());\r
+        fm = gg.getFontMetrics();\r
+      }\r
 \r
-        drawComponent(gg, av.startRes, av.endRes + 1);\r
-        g.drawImage(image, 0, 0, this);\r
+      drawComponent(gg, av.startRes, av.endRes + 1);\r
+      g.drawImage(image, 0, 0, this);\r
     }\r
 \r
     /**\r
@@ -583,7 +602,9 @@ public class AnnotationPanel extends JPanel implements MouseListener,
         gg.translate(-transX, 0);\r
 \r
         fastPaint = true;\r
+\r
         repaint();\r
+\r
     }\r
 \r
     /**\r
@@ -593,11 +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
-      if(fm==null)\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
@@ -632,7 +656,8 @@ public class AnnotationPanel extends JPanel implements MouseListener,
                 continue;\r
             }\r
 \r
-            if (row.isGraph)\r
+\r
+            if (row.graph>0)\r
             {\r
                 // this is so that we draw the characters below the graph\r
                 y += row.height;\r
@@ -694,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
@@ -759,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
-\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
+                    ///g.setColor(new Color(0, 0, 180));\r
 \r
-                            total += row.annotations[i2].value;\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.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
@@ -826,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
-    // used by overview window\r
-    public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y)\r
+    public void drawLineGraph(Graphics g, AlignmentAnnotation aa, int sRes, int eRes, int x, int y)\r
     {\r
-        g.setColor(Color.white);\r
-        g.fillRect(0, 0, width, y);\r
-        g.setColor(new Color(0, 0, 180));\r
+      g.setColor(new Color(0, 0, 0));\r
+      if(sRes==0)\r
+        sRes++;\r
 \r
-        int x = 0;\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
-        for (int j = 0; j < aa.annotations.length; j++)\r
-        {\r
-            g.setColor(new Color(0, 0, 180));\r
+          if(aa.annotations[j]!=null)\r
+          y2 = y - (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);\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
+          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
+\r
+      int x = 0, height;\r
+\r
+      for (int j = sRes; j < eRes; j++)\r
+      {\r
+          g.setColor(new Color(0, 0, 180));\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