package causality; // packages import javax.swing.*; import java.awt.*; import java.util.*; /** *

* This class is a JPanel that displays an instance of DSeparationFacts, along * with an (optional) Image. *

* Copyright 1999 by David Danks. All rights reserved. *

* * @see DSeparationFacts * @version 1.0; Mar 20, 1999 * @author David Danks */ public class DSeparationPanel extends JPanel { /////////////////// Class Variable /////////////////// private static final Font FONT = new Font("Serif", Font.PLAIN, 18); private static final int MARGIN = 10; private static final int LINE_WIDTH = 3; private static final int SPACE = 4; private static final int INSET = 4; // constants for drawing the private static final int CENTER = 5; // independence symbol private static final int BASE = 2*INSET+CENTER; private static final int DROP = 7; private static final String NO_FACTS_STRING = "No independencies."; private static final String NULL_IMAGE_STRING = "No image."; /////////////////// Instance Variables /////////////////// private Image image; private DSeparationFacts dsf; private String[] facts; // private double scale = 1.0; /** * Determines the d-separation facts for the given DiGraph. Note: the * DSeparationPanel does not automatically pop up. * @param dg The DiGraph whose d-separation facts are requested * @param im An (optional) Image to display with the independencies */ public DSeparationPanel(DiGraph dg, Image im) { this(new DSeparationFacts(dg), im); } /** * Displays the given DSeparationFacts. Note: the DSeparationPanel does not * automatically pop up. * @param dsf The DSeparationFacts to display * @param im An (optional) Image to display */ public DSeparationPanel(DSeparationFacts dsf, Image im) { this.dsf = dsf; image = im; facts = dsf.getDSepFacts(); setFont(FONT); } /** * @return The DSeparationFacts to be displayed */ public DSeparationFacts getDSeparationFacts() { return dsf; } /** * @return The preferred size of the DSeparationPanel */ public Dimension getPreferredSize() { FontMetrics fm = getFontMetrics(getFont()); int width = 0; // space for line and gap around line (the line length is the panel width) int height = 3*LINE_WIDTH; // space for facts if (facts.length == 0) { height += fm.getHeight(); width = Math.max(width, fm.stringWidth(NO_FACTS_STRING)+2*MARGIN); } else { height += (facts.length+1)*fm.getHeight(); for (int i=0; itrue always, since the DSeparationPanel is opaque public boolean isOpaque() { return true; } /** * Paints the entire DSeparationPanel: both the Image and the independencies. * @param g The Graphics context in which to paint the DSeparationPanel */ public void paintComponent(Graphics g) { FontMetrics fm = getFontMetrics(getFont()); int xOff = 0; int yOff = 0; /* // draw the background g.setColor(Color.white); g.drawRect(0, 0, getWidth()-1, getHeight()-1); g.setColor(Color.black); */ // draw the image (or null string) if (image != null) { g.drawImage(image, (getWidth()-image.getWidth(this))/2, 0, image.getWidth(this), image.getHeight(this), this); yOff += image.getHeight(this); } else { g.drawString(NULL_IMAGE_STRING, MARGIN, yOff+fm.getHeight()); yOff += fm.getHeight(); } // draw the separating line yOff += LINE_WIDTH; for (int i=0; inull, then all of the DSeparationFacts are displayed. * * @param filter The filter listing the variables to be shown * @return An array of filtered d-separation facts */ private String[] filterFacts(String[] filter) { String[] fullFacts = dsf.getDSepFacts(); if (filter == null) return fullFacts; int numRemoved = 0; FACTS: for (int i=0; i=0; j--) { if (uncond.equals((String)toAdd.elementAt(j))) { toAdd.removeElementAt(j); continue FACTS; } } } } // if there aren't any facts to add, just return initialFacts if (toAdd.size() == 0) return; // now create a new facts array String[] added = new String[facts.length+toAdd.size()]; System.arraycopy(facts, 0, added, 0, facts.length); for (int i=0; i