[4] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * VisualizeUtils.java |
---|
| 19 | * Copyright (C) 2000 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.visualize; |
---|
| 24 | |
---|
| 25 | import weka.core.Utils; |
---|
| 26 | import java.util.Properties; |
---|
| 27 | import java.io.FileInputStream; |
---|
| 28 | |
---|
| 29 | import java.awt.Color; |
---|
| 30 | import javax.swing.JOptionPane; |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * This class contains utility routines for visualization |
---|
| 35 | * |
---|
| 36 | * @author Mark Hall (mhall@cs.waikato.ac.nz) |
---|
| 37 | * @version $Revision: 1.9 $ |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | public class VisualizeUtils { |
---|
| 41 | |
---|
| 42 | /** The name of the properties file */ |
---|
| 43 | protected static String PROPERTY_FILE = "weka/gui/visualize/Visualize.props"; |
---|
| 44 | |
---|
| 45 | /** Contains the visualization properties */ |
---|
| 46 | protected static Properties VISUALIZE_PROPERTIES; |
---|
| 47 | |
---|
| 48 | /** Default maximum precision for the display of numeric values */ |
---|
| 49 | protected static int MAX_PRECISION = 10; |
---|
| 50 | |
---|
| 51 | static { |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | try { |
---|
| 55 | VISUALIZE_PROPERTIES = Utils.readProperties(PROPERTY_FILE); |
---|
| 56 | String precision = |
---|
| 57 | VISUALIZE_PROPERTIES.getProperty("weka.gui.visualize.precision"); |
---|
| 58 | if (precision == null) { |
---|
| 59 | /* |
---|
| 60 | System.err.println("Warning: no configuration property found in" |
---|
| 61 | +PROPERTY_FILE |
---|
| 62 | +" for weka.gui.visualize.precision. Using" |
---|
| 63 | +" default instead.");*/ |
---|
| 64 | } else { |
---|
| 65 | MAX_PRECISION = Integer.parseInt(precision); |
---|
| 66 | // System.err.println("Setting numeric precision to: "+precision); |
---|
| 67 | } |
---|
| 68 | } catch (Exception ex) { |
---|
| 69 | JOptionPane.showMessageDialog(null, |
---|
| 70 | "VisualizeUtils: Could not read a visualization configuration file.\n" |
---|
| 71 | +"An example file is included in the Weka distribution.\n" |
---|
| 72 | +"This file should be named \"" + PROPERTY_FILE + "\" and\n" |
---|
| 73 | +"should be placed either in your user home (which is set\n" |
---|
| 74 | +"to \"" + System.getProperties().getProperty("user.home") + "\")\n" |
---|
| 75 | +"or the directory that java was started from\n", |
---|
| 76 | "Plot2D", |
---|
| 77 | JOptionPane.ERROR_MESSAGE); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * Parses a string containing either a named colour or r,g,b values. |
---|
| 83 | * @param colourDef the string containing the named colour (or r,g,b) |
---|
| 84 | * @param defaultColour the colour to return if parsing fails |
---|
| 85 | * @return the Color corresponding to the string. |
---|
| 86 | */ |
---|
| 87 | public static Color processColour(String colourDef, Color defaultColour) { |
---|
| 88 | String colourDefBack = new String(colourDef); |
---|
| 89 | Color retC = defaultColour; |
---|
| 90 | if (colourDef.indexOf(",") >= 0) { |
---|
| 91 | // Looks like property value is in R, G, B format |
---|
| 92 | try { |
---|
| 93 | int index = colourDef.indexOf(","); |
---|
| 94 | int R = Integer.parseInt(colourDef.substring(0,index)); |
---|
| 95 | colourDef = colourDef.substring(index+1,colourDef.length()); |
---|
| 96 | index = colourDef.indexOf(","); |
---|
| 97 | int G = Integer.parseInt(colourDef.substring(0,index)); |
---|
| 98 | colourDef = colourDef.substring(index+1,colourDef.length()); |
---|
| 99 | int B = Integer.parseInt(colourDef); |
---|
| 100 | //System.err.println(R+" "+G+" "+B); |
---|
| 101 | retC = new Color(R,G,B); |
---|
| 102 | } catch (Exception ex) { |
---|
| 103 | System.err.println("VisualizeUtils: Problem parsing colour property " |
---|
| 104 | +"value ("+colourDefBack+")."); |
---|
| 105 | } |
---|
| 106 | } else { |
---|
| 107 | // assume that the string is the name of a default Color.color |
---|
| 108 | if (colourDef.compareTo("black") == 0) { |
---|
| 109 | retC = Color.black; |
---|
| 110 | } else if (colourDef.compareTo("blue") == 0) { |
---|
| 111 | retC = Color.blue; |
---|
| 112 | } else if (colourDef.compareTo("cyan") == 0) { |
---|
| 113 | retC = Color.cyan; |
---|
| 114 | } else if (colourDef.compareTo("darkGray") == 0) { |
---|
| 115 | retC = Color.darkGray; |
---|
| 116 | } else if (colourDef.compareTo("gray") == 0) { |
---|
| 117 | retC = Color.gray; |
---|
| 118 | } else if (colourDef.compareTo("green") == 0) { |
---|
| 119 | retC = Color.green; |
---|
| 120 | } else if (colourDef.compareTo("lightGray") == 0) { |
---|
| 121 | retC = Color.lightGray; |
---|
| 122 | } else if (colourDef.compareTo("magenta") == 0) { |
---|
| 123 | retC = Color.magenta; |
---|
| 124 | } else if (colourDef.compareTo("orange") == 0) { |
---|
| 125 | retC = Color.orange; |
---|
| 126 | } else if (colourDef.compareTo("pink") == 0) { |
---|
| 127 | retC = Color.pink; |
---|
| 128 | } else if (colourDef.compareTo("red") == 0) { |
---|
| 129 | retC = Color.red; |
---|
| 130 | } else if (colourDef.compareTo("white") == 0) { |
---|
| 131 | retC = Color.white; |
---|
| 132 | } else if (colourDef.compareTo("yellow") == 0) { |
---|
| 133 | retC = Color.yellow; |
---|
| 134 | } else { |
---|
| 135 | System.err.println("VisualizeUtils: colour property name not recognized " |
---|
| 136 | +"("+colourDefBack+")."); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | return retC; |
---|
| 140 | } |
---|
| 141 | } |
---|