From f85c8c88d6947b9f290409f1271429144df2c120 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Thu, 27 Oct 2005 11:12:06 +0000 Subject: [PATCH] MyGraphics added to enhance applet GUI --- src/jalview/appletgui/APopupMenu.java | 8 +- src/jalview/appletgui/AlignFrame.java | 78 +++++-- src/jalview/appletgui/AnnotationLabels.java | 5 + src/jalview/appletgui/AnnotationPanel.java | 6 + src/jalview/appletgui/FeatureRenderer.java | 291 +++++++++++++++++++++------ src/jalview/appletgui/IdCanvas.java | 6 + src/jalview/appletgui/IdPanel.java | 3 +- src/jalview/appletgui/MyGraphics.java | 21 ++ src/jalview/appletgui/RotatableCanvas.java | 5 + src/jalview/appletgui/ScalePanel.java | 5 + src/jalview/appletgui/SeqCanvas.java | 13 +- src/jalview/appletgui/TreeCanvas.java | 6 + 12 files changed, 360 insertions(+), 87 deletions(-) create mode 100755 src/jalview/appletgui/MyGraphics.java diff --git a/src/jalview/appletgui/APopupMenu.java b/src/jalview/appletgui/APopupMenu.java index 272d653..0d1dad3 100755 --- a/src/jalview/appletgui/APopupMenu.java +++ b/src/jalview/appletgui/APopupMenu.java @@ -46,7 +46,7 @@ public class APopupMenu MenuItem noColourmenuItem = new MenuItem(); protected CheckboxMenuItem conservationMenuItem = new CheckboxMenuItem(); - AlignmentPanel ap; + final AlignmentPanel ap; MenuItem unGroupMenuItem = new MenuItem(); MenuItem nucleotideMenuItem = new MenuItem(); Menu colourMenu = new Menu(); @@ -54,7 +54,7 @@ public class APopupMenu CheckboxMenuItem showText = new CheckboxMenuItem(); CheckboxMenuItem showColourText = new CheckboxMenuItem(); - public APopupMenu(AlignmentPanel ap, Sequence seq, Vector links) + public APopupMenu(AlignmentPanel apanel, Sequence seq, Vector links) { /////////////////////////////////////////////////////////// // If this is activated from the sequence panel, the user may want to @@ -63,7 +63,7 @@ public class APopupMenu // If from the IDPanel, we must display the sequence menu ////////////////////////////////////////////////////////// - this.ap = ap; + this.ap = apanel; try { @@ -105,7 +105,7 @@ public class APopupMenu { public void actionPerformed(ActionEvent e) { - jalview.bin.JalviewLite.showURL(url, target); + ap.alignFrame.showURL(url, target); } }); linkMenu.add(item); diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index b632442..c9c5117 100755 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -29,7 +29,7 @@ import java.awt.event.*; import java.util.*; import java.io.InputStreamReader; import java.io.BufferedReader; -import java.io.FileReader; +import java.net.URL; public class AlignFrame extends GAlignFrame @@ -147,33 +147,36 @@ public class AlignFrame * @param String DOCUMENT ME! */ - public void parseGroupsFile(String file) + public void parseFeaturesFile(String file) { try { - BufferedReader in = new BufferedReader(new FileReader(file)); + URL url = new URL(file); + + BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); SequenceI seq = null; - String line, text, token; - UserColourScheme ucs; + String line, type, desc, token; + int index, start, end; StringTokenizer st; - SequenceGroup sg; + SequenceFeature sf; + FeatureRenderer fr = alignPanel.seqPanel.seqCanvas.getFeatureRenderer(); int lineNo = 0; while ( (line = in.readLine()) != null) { lineNo++; st = new StringTokenizer(line, "\t"); - if (st.countTokens() != 6) + if (st.countTokens() == 2) { - System.out.println("Groups file " + file + - " is invalid. Read help file.\nLine: \n" - +lineNo +": "+line); - break; + type = st.nextToken(); + UserColourScheme ucs = new UserColourScheme(st.nextToken()); + fr.setColour(type, ucs.findColour("A")); + continue; } while (st.hasMoreElements()) { - text = st.nextToken(); + desc = st.nextToken(); token = st.nextToken(); if (!token.equals("ID_NOT_SPECIFIED")) { @@ -188,20 +191,29 @@ public class AlignFrame start = Integer.parseInt(st.nextToken()); end = Integer.parseInt(st.nextToken()); - ucs = new UserColourScheme(st.nextToken()); seq = viewport.alignment.getSequenceAt(index); start = seq.findIndex(start) - 1; end = seq.findIndex(end) - 1; - sg = new SequenceGroup(text, ucs, true, true, false, start, end); - sg.addSequence(seq, false); + type = st.nextToken(); + + if(fr.getColour(type)==null) + { + // Probably the old style groups file + UserColourScheme ucs = new UserColourScheme(type); + fr.setColour(type, ucs.findColour("A")); + } - viewport.alignment.addGroup(sg); + sf = new SequenceFeature(type, desc, "", start, end); + seq.addSequenceFeature(sf); } } + viewport.showSequenceFeatures = true; + ((Alignment)viewport.alignment).featuresAdded = true; + alignPanel.repaint(); } @@ -1348,7 +1360,7 @@ public class AlignFrame protected void documentation_actionPerformed(ActionEvent e) { - jalview.bin.JalviewLite.showURL("http://www.jalview.org/help.html"); + showURL("http://www.jalview.org/help.html"); } protected void about_actionPerformed(ActionEvent e) @@ -1415,4 +1427,36 @@ public class AlignFrame jalview.bin.JalviewLite.addFrame(frame, "Jalview", 580, 200); } + + /** + * Displays the given URL in a new browser window + * + * @param url URL to display in browser window. + *
New window will be named "HELP_WINDOW" + */ + public void showURL(String url) + { + showURL(url, "HELP"); + } + + public void showURL(String url, String target) + { + if (applet == null) + { + System.out.println("Not running as applet - no browser available."); + } + else + { + try + { + applet.getAppletContext().showDocument(new java.net.URL(url), + target); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + } + } diff --git a/src/jalview/appletgui/AnnotationLabels.java b/src/jalview/appletgui/AnnotationLabels.java index 1df7455..dcdccc5 100755 --- a/src/jalview/appletgui/AnnotationLabels.java +++ b/src/jalview/appletgui/AnnotationLabels.java @@ -159,6 +159,11 @@ public class AnnotationLabels public void drawComponent(Graphics g, int width) { + if (!jalview.bin.JalviewLite.AWT1) + { + MyGraphics.AntiAlias(g); + } + FontMetrics fm = g.getFontMetrics(g.getFont()); g.setColor(Color.white); g.fillRect(0, 0, getSize().width, getSize().height); diff --git a/src/jalview/appletgui/AnnotationPanel.java b/src/jalview/appletgui/AnnotationPanel.java index a1597f8..15e2671 100755 --- a/src/jalview/appletgui/AnnotationPanel.java +++ b/src/jalview/appletgui/AnnotationPanel.java @@ -219,6 +219,12 @@ public class AnnotationPanel fastPaint = false; } + if (!jalview.bin.JalviewLite.AWT1) + { + MyGraphics.AntiAlias(gg); + } + + if (fastPaint) { g.drawImage(image, 0, 0, this); diff --git a/src/jalview/appletgui/FeatureRenderer.java b/src/jalview/appletgui/FeatureRenderer.java index 9d6503d..50f1378 100755 --- a/src/jalview/appletgui/FeatureRenderer.java +++ b/src/jalview/appletgui/FeatureRenderer.java @@ -16,94 +16,259 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ - package jalview.appletgui; -import java.util.*; +import jalview.datamodel.*; import java.awt.*; -import jalview.datamodel.*; +import java.util.*; + +import java.awt.image.*; + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ public class FeatureRenderer { - AlignViewport av; - - SequenceGroup currentSequenceGroup = null; - SequenceGroup[] allGroups = null; - Color resBoxColour; - Graphics graphics; - - public FeatureRenderer(AlignViewport av) - { - this.av = av; - } - - public void drawSequence(Graphics g, SequenceI seq, SequenceGroup[] sg, - int start, int end, int x1, int y1, int width, - int height) - { - Vector features = seq.getSequenceFeatures(); - Enumeration e = features.elements(); - while (e.hasMoreElements()) + AlignViewport av; + SequenceGroup currentSequenceGroup = null; + SequenceGroup[] allGroups = null; + Color resBoxColour; + Graphics graphics; + float transparency = .4f; + + // The following vector holds the features which are + // to be added, in the correct order or rendering + Vector featuresDisplayed; + + /** + * Creates a new FeatureRenderer object. + * + * @param av DOCUMENT ME! + */ + public FeatureRenderer(AlignViewport av) { - SequenceFeature sf = (SequenceFeature) e.nextElement(); - if (sf.getBegin() > seq.getEnd()) - { - continue; - } + this.av = av; + initColours(); + String version = System.getProperty("java.version"); + if (version.indexOf("1.1") == 0) + highlightTransparent = false; - int fstart = seq.findIndex(sf.getBegin()) - 1; - int fend = seq.findIndex(sf.getEnd()) - 1; + } - if ( (fstart <= end && fend >= start)) - { - if (fstart < 0) // fix for if the feature we have starts before the sequence start, +boolean highlightTransparent = true; + /** + * DOCUMENT ME! + * + * @param g DOCUMENT ME! + * @param seq DOCUMENT ME! + * @param sg DOCUMENT ME! + * @param start DOCUMENT ME! + * @param end DOCUMENT ME! + * @param x1 DOCUMENT ME! + * @param y1 DOCUMENT ME! + * @param width DOCUMENT ME! + * @param height DOCUMENT ME! + */ + public void drawSequence(Graphics g, SequenceI seq, SequenceGroup[] sg, + int start, int end, int x1, int y1, int width, int height) + { + + if(seq.getSequenceFeatures()==null) + return; + + Enumeration e = null, e2; + String type; + if(featuresDisplayed!=null) + e = featuresDisplayed.elements(); + else + e = seq.getSequenceFeatures().elements(); + + if(highlightTransparent) { - fstart = 0; // but the feature end is still valid!! + // MyGraphics.SetTransparency(g, transparency); } - if (fstart == fend) - { - g.setColor(Color.red); - g.fillRoundRect( (fstart - start) * width, y1, width, height, 4, 4); - g.setColor(Color.white); - - char s = seq.getSequence().charAt(fstart); - FontMetrics fm = g.getFontMetrics(); - int charOffset = (width - fm.charWidth(s)) / 2; - int pady = height / 5; - g.drawString(String.valueOf(s), - charOffset + x1 + width * (fstart - start), - y1 + height - pady); - } - else + while (e.hasMoreElements()) { - for (int i = fstart; i <= fend; i++) - { - char s = seq.getSequence().charAt(i); - if (jalview.util.Comparison.isGap(s)) + SequenceFeature sf=null; + if(featuresDisplayed!=null) { + e2 = seq.getSequenceFeatures().elements(); + type = e.nextElement().toString(); + while(e2.hasMoreElements()) + { + sf = (SequenceFeature) e2.nextElement(); + if(sf.getType().equals(type)) + break; + else + sf = null; + } + } + else + { + sf = (SequenceFeature) e.nextElement(); + type = sf.getType(); + } + + if(sf==null) continue; + + + if (sf.getBegin() > seq.getEnd()) + { + continue; } - g.setColor(Color.blue); - g.fillRect( (i - start) * width, y1, width, height); + int fstart = seq.findIndex(sf.getBegin()) - 1; + int fend = seq.findIndex(sf.getEnd()) - 1; + + if (((fstart <= end) && (fend >= start))) + { + if (fstart < start) + { // fix for if the feature we have starts before the sequence start, + fstart = start; // but the feature end is still valid!! + } + + if (fend >= end) + { + fend = end; + } + + if (fstart == fend) + { + g.setColor(getColour(type)); + g.fillRoundRect((fstart - start) * width, y1, width, + height, 4, 4); + g.setColor(Color.white); + + char s = seq.getSequence().charAt(fstart); + FontMetrics fm = g.getFontMetrics(); + int charOffset = (width - fm.charWidth(s)) / 2; + int pady = height / 5; + g.drawString(String.valueOf(s), + charOffset + x1 + (width * (fstart - start)), + (y1 + height) - pady); + } + else + { + for (int i = fstart; i <= fend; i++) + { + char s = seq.getSequence().charAt(i); - g.setColor(Color.white); + if (jalview.util.Comparison.isGap(s)) + { + continue; + } - FontMetrics fm = g.getFontMetrics(); - int charOffset = (width - fm.charWidth(s)) / 2; - int pady = height / 5; - g.drawString(String.valueOf(s), - charOffset + x1 + width * (i - start), - y1 + height - pady); + g.setColor(getColour(type)); + g.fillRect((i - start) * width, y1, width, height); + + g.setColor(Color.white); + + FontMetrics fm = g.getFontMetrics(); + int charOffset = (width - fm.charWidth(s)) / 2; + int pady = height / 5; + g.drawString(String.valueOf(s), + charOffset + x1 + (width * (i - start)), + (y1 + height) - pady); + } + } + } } - } + + if (highlightTransparent) + { + // MyGraphics.SetTransparency(g, 1.0f); + } + } + + public Color getColour(String featureType) + { + return (Color)featureColours.get(featureType); + } + + public void setColour(String featureType, Color col) + { + featureColours.put(featureType, col); + } + + public void setTransparency(float value) + { + transparency = value; + } + + public float getTransparency() + { + return transparency; + } + + public void setFeaturePriority(Object [][] data) + { + // The feature table will display high priority + // features at the top, but theses are the ones + // we need to render last, so invert the data + featuresDisplayed = new Vector(); + for(int i=data.length-1; i>-1; i--) + { + String type = data[i][0].toString(); + setColour(type, (Color)data[i][1]); + if( ((Boolean)data[i][2]).booleanValue() ) + featuresDisplayed.addElement(type); } + } + Hashtable featureColours = new Hashtable(); + void initColours() + { + featureColours.put("active site", new Color(255, 75, 0)); + featureColours.put("binding site", new Color(245, 85, 0)); + featureColours.put("calcium-binding region", new Color(235, 95, 0)); + featureColours.put("chain", new Color(225, 105, 0)); + featureColours.put("coiled-coil region", new Color(215, 115, 0)); + featureColours.put("compositionally biased region", new Color(205, 125, 0)); + featureColours.put("cross-link", new Color(195, 135, 0)); + featureColours.put("disulfide bond", new Color(185, 145, 0)); + featureColours.put("DNA-binding region", new Color(175, 155, 0)); + featureColours.put("domain", new Color(165, 165, 0)); + featureColours.put("glycosylation site", new Color(155, 175, 0)); + featureColours.put("helix", new Color(145, 185, 0)); + featureColours.put("initiator methionine", new Color(135, 195, 5)); + featureColours.put("lipid moiety-binding region", new Color(125, 205, 15)); + featureColours.put("metal ion-binding site", new Color(115, 215, 25)); + featureColours.put("modified residue", new Color(105, 225, 35)); + featureColours.put("mutagenesis site", new Color(95, 235, 45)); + featureColours.put("non-consecutive residues", new Color(85, 245, 55)); + featureColours.put("non-terminal residue", new Color(75, 255, 65)); + featureColours.put("nucleotide phosphate-binding region", + new Color(65, 245, 75)); + featureColours.put("peptide", new Color(55, 235, 85)); + featureColours.put("propeptide", new Color(45, 225, 95)); + featureColours.put("region of interest", new Color(35, 215, 105)); + featureColours.put("repeat", new Color(25, 205, 115)); + featureColours.put("selenocysteine", new Color(15, 195, 125)); + featureColours.put("sequence conflict", new Color(5, 185, 135)); + featureColours.put("sequence variant", new Color(0, 175, 145)); + featureColours.put("short sequence motif", new Color(0, 165, 155)); + featureColours.put("signal peptide", new Color(0, 155, 165)); + featureColours.put("site", new Color(0, 145, 175)); + featureColours.put("splice variant", new Color(0, 135, 185)); + featureColours.put("strand", new Color(0, 125, 195)); + featureColours.put("topological domain", new Color(0, 115, 205)); + featureColours.put("transit peptide", new Color(0, 105, 215)); + featureColours.put("transmembrane region", new Color(0, 95, 225)); + featureColours.put("turn", new Color(0, 85, 235)); + featureColours.put("unsure residue", new Color(0, 75, 245)); + featureColours.put("zinc finger region", new Color(0, 65, 255)); } - } } + + + diff --git a/src/jalview/appletgui/IdCanvas.java b/src/jalview/appletgui/IdCanvas.java index a277adb..d581273 100755 --- a/src/jalview/appletgui/IdCanvas.java +++ b/src/jalview/appletgui/IdCanvas.java @@ -161,6 +161,12 @@ public class IdCanvas gg.setFont(av.getFont()); } + if(!jalview.bin.JalviewLite.AWT1) + { + MyGraphics.AntiAlias(gg); + } + + //Fill in the background gg.setColor(Color.white); Font italic = new Font(av.getFont().getName(), Font.ITALIC, diff --git a/src/jalview/appletgui/IdPanel.java b/src/jalview/appletgui/IdPanel.java index b6e6a2b..82fc507 100755 --- a/src/jalview/appletgui/IdPanel.java +++ b/src/jalview/appletgui/IdPanel.java @@ -127,7 +127,8 @@ public class IdPanel try { - jalview.bin.JalviewLite.showURL(url, target); + + alignPanel.alignFrame.showURL(url, target); } catch (Exception ex) { diff --git a/src/jalview/appletgui/MyGraphics.java b/src/jalview/appletgui/MyGraphics.java new file mode 100755 index 0000000..835aff2 --- /dev/null +++ b/src/jalview/appletgui/MyGraphics.java @@ -0,0 +1,21 @@ +package jalview.appletgui; +import java.awt.*; + +public class MyGraphics +{ + public static void AntiAlias(Graphics g) + { + Graphics2D g2 = (Graphics2D)g; + g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + } + + + public static void SetTransparency(Graphics g, float transparency) + { + Graphics2D g2 = (Graphics2D) g; + g2.setComposite( + AlphaComposite.getInstance( + AlphaComposite.SRC_OVER, transparency)); + } +} diff --git a/src/jalview/appletgui/RotatableCanvas.java b/src/jalview/appletgui/RotatableCanvas.java index 5daa320..b27ab8a 100755 --- a/src/jalview/appletgui/RotatableCanvas.java +++ b/src/jalview/appletgui/RotatableCanvas.java @@ -293,6 +293,11 @@ public class RotatableCanvas public void paint(Graphics g) { + if (!jalview.bin.JalviewLite.AWT1) + { + MyGraphics.AntiAlias(g); + } + if (points == null) { g.setFont(new Font("Verdana", Font.PLAIN, 18)); diff --git a/src/jalview/appletgui/ScalePanel.java b/src/jalview/appletgui/ScalePanel.java index af226f3..336da66 100755 --- a/src/jalview/appletgui/ScalePanel.java +++ b/src/jalview/appletgui/ScalePanel.java @@ -162,6 +162,11 @@ public class ScalePanel public void drawScale(Graphics gg, int startx, int endx, int width, int height) { + if (!jalview.bin.JalviewLite.AWT1) + { + MyGraphics.AntiAlias(gg); + } + gg.setFont(av.getFont()); //Fill in the background diff --git a/src/jalview/appletgui/SeqCanvas.java b/src/jalview/appletgui/SeqCanvas.java index 1de9418..ddc32e8 100755 --- a/src/jalview/appletgui/SeqCanvas.java +++ b/src/jalview/appletgui/SeqCanvas.java @@ -54,6 +54,11 @@ public class SeqCanvas } + public FeatureRenderer getFeatureRenderer() + { + return fr; + } + void drawNorthScale(Graphics g, int startx, int endx, int ypos) { int scalestartx = startx - startx % 10 + 10; @@ -235,6 +240,10 @@ public class SeqCanvas { img = createImage(imgWidth, imgHeight); gg = img.getGraphics(); + if(!jalview.bin.JalviewLite.AWT1) + { + MyGraphics.AntiAlias(gg); + } gg.setFont(av.getFont()); } @@ -368,12 +377,12 @@ public class SeqCanvas if(av.showAnnotation) { - g.translate(0, cHeight + ypos); + g.translate(0, cHeight + ypos+4); if(annotations==null) annotations = new AnnotationPanel(av); annotations.drawComponent( g, startRes, endx + 1); - g.translate(0, -cHeight - ypos); + g.translate(0, -cHeight - ypos-4); } g.translate(-LABEL_WEST, 0); diff --git a/src/jalview/appletgui/TreeCanvas.java b/src/jalview/appletgui/TreeCanvas.java index d4202c3..f74a1f6 100755 --- a/src/jalview/appletgui/TreeCanvas.java +++ b/src/jalview/appletgui/TreeCanvas.java @@ -354,8 +354,14 @@ public class TreeCanvas if(tree==null) return; + if (!jalview.bin.JalviewLite.AWT1) + { + MyGraphics.AntiAlias(g); + } + g.setFont(font); + FontMetrics fm = g.getFontMetrics(font); if (nameHash.size() == 0) -- 1.7.10.2