001/* 002 * Copyright 2006 - 2013 003 * Stefan Balev <stefan.balev@graphstream-project.org> 004 * Julien Baudry <julien.baudry@graphstream-project.org> 005 * Antoine Dutot <antoine.dutot@graphstream-project.org> 006 * Yoann Pigné <yoann.pigne@graphstream-project.org> 007 * Guilhelm Savin <guilhelm.savin@graphstream-project.org> 008 * 009 * This file is part of GraphStream <http://graphstream-project.org>. 010 * 011 * GraphStream is a library whose purpose is to handle static or dynamic 012 * graph, create them from scratch, file or any source and display them. 013 * 014 * This program is free software distributed under the terms of two licenses, the 015 * CeCILL-C license that fits European law, and the GNU Lesser General Public 016 * License. You can use, modify and/ or redistribute the software under the terms 017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following 018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by 019 * the Free Software Foundation, either version 3 of the License, or (at your 020 * option) any later version. 021 * 022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 024 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 025 * 026 * You should have received a copy of the GNU Lesser General Public License 027 * along with this program. If not, see <http://www.gnu.org/licenses/>. 028 * 029 * The fact that you are presently reading this means that you have had 030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. 031 */ 032package org.graphstream.ui.swingViewer.util; 033 034import java.awt.BasicStroke; 035import java.awt.Stroke; 036 037import org.graphstream.ui.graphicGraph.stylesheet.Style; 038import org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units; 039 040/** 041 * Generator for strokes based on the given style. 042 */ 043public class StrokeFactory { 044 /** 045 * Generate a stroke of the appropriate width and style according to the 046 * given style and metrics. 047 * 048 * @param style 049 * The style to use. 050 * @param metrics 051 * The metrics to use. 052 * @return The stroke or null if the style specifies a "none" stroke mode. 053 */ 054 public static Stroke generateStroke(Style style, GraphMetrics metrics) { 055 if (style.getStrokeWidth().value == 0) 056 return null; 057 058 switch (style.getStrokeMode()) { 059 case PLAIN: 060 return generatePlainStroke(style, metrics); 061 case DOTS: 062 return generateDotsStroke(style, metrics); 063 case DASHES: 064 return generateDashesStroke(style, metrics); 065 default: 066 case NONE: 067 return null; 068 } 069 } 070 071 protected static Stroke generatePlainStroke(Style style, 072 GraphMetrics metrics) { 073 float width = (float)metrics.lengthToGu(style.getStrokeWidth()); 074 075 /* 076 * if( width == 1f ) return plainLine1px; // XXX Not a good optimisation 077 * else if( width == 2f ) return plainLine2px; // We draw the whole 078 * graph in GU else if( width == 3f ) return plainLine3px; // In graph 079 * units the width is never exactly 1,2, 5 ... else if( width == 5f ) 080 * return plainLine5px; else if( width == 10f ) return plainLine10px; 081 * else 082 */{ 083 return new BasicStroke(width); 084 } 085 } 086 087 protected static Stroke generateDotsStroke(Style style, GraphMetrics metrics) { 088 float width = (float)metrics.lengthToGu(style.getStrokeWidth()); 089 /* 090 * if( width == 1f ) return dotsLine1px; else if( width == 2f ) return 091 * dotsLine2px; else if( width == 3f ) return dotsLine3px; else if( 092 * width == 5f ) return dotsLine5px; else if( width == 10f ) return 093 * dotsLine10px; else 094 */{ 095 dots[0] = (float)metrics.lengthToGu(1f, Units.PX); 096 dots[1] = dots[0]; 097 return new BasicStroke(width, BasicStroke.CAP_BUTT, 098 BasicStroke.JOIN_MITER, 1f, dots, 0); 099 } 100 } 101 102 protected static Stroke generateDashesStroke(Style style, 103 GraphMetrics metrics) { 104 float width = (float)metrics.lengthToGu(style.getStrokeWidth()); 105 /* 106 * if( width == 1f ) return dashesLine1px; else if( width == 2f ) return 107 * dashesLine2px; else if( width == 3f ) return dashesLine3px; else if( 108 * width == 5f ) return dashesLine5px; else if( width == 10f ) return 109 * dashesLine10px; else 110 */{ 111 dashes[0] = (float)metrics.lengthToGu(3f, Units.PX); 112 dashes[1] = dashes[0]; 113 return new BasicStroke(width, BasicStroke.CAP_BUTT, 114 BasicStroke.JOIN_MITER, 1f, dashes, 0); 115 } 116 } 117 118 protected static float[] dots = { 1f, 1f }; 119 protected static float[] dashes = { 3f, 3f }; 120 /* 121 * protected static Stroke plainLine1px = new BasicStroke( 1 ); protected 122 * static Stroke dotsLine1px = new BasicStroke( 1, BasicStroke.CAP_BUTT, 123 * BasicStroke.JOIN_BEVEL, 1f, dots, 0 ); protected static Stroke 124 * dashesLine1px = new BasicStroke( 1, BasicStroke.CAP_BUTT, 125 * BasicStroke.JOIN_BEVEL, 1f, dots, 0 ); protected static Stroke 126 * plainLine2px = new BasicStroke( 2 ); protected static Stroke dotsLine2px 127 * = new BasicStroke( 2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1f, 128 * dots, 0 ); protected static Stroke dashesLine2px = new BasicStroke( 2, 129 * BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1f, dots, 0 ); protected 130 * static Stroke plainLine3px = new BasicStroke( 2 ); protected static 131 * Stroke dotsLine3px = new BasicStroke( 2, BasicStroke.CAP_BUTT, 132 * BasicStroke.JOIN_BEVEL, 1f, dots, 0 ); protected static Stroke 133 * dashesLine3px = new BasicStroke( 2, BasicStroke.CAP_BUTT, 134 * BasicStroke.JOIN_BEVEL, 1f, dots, 0 ); protected static Stroke 135 * plainLine5px = new BasicStroke( 2 ); protected static Stroke dotsLine5px 136 * = new BasicStroke( 2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1f, 137 * dots, 0 ); protected static Stroke dashesLine5px = new BasicStroke( 2, 138 * BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1f, dots, 0 ); protected 139 * static Stroke plainLine10px = new BasicStroke( 2 ); protected static 140 * Stroke dotsLine10px = new BasicStroke( 2, BasicStroke.CAP_BUTT, 141 * BasicStroke.JOIN_BEVEL, 1f, dots, 0 ); protected static Stroke 142 * dashesLine10px = new BasicStroke( 2, BasicStroke.CAP_BUTT, 143 * BasicStroke.JOIN_BEVEL, 1f, dots, 0 ); 144 */ 145}