Source code: jplot/LineStyle.java
1 /*
2 * JPLOT -- Java Plotting Interface for any programme or
3 * as an independent GUI
4 *
5 * Copyright (C) 1999 Jan van der Lee
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 *
22 * Send bugs, suggestions or queries to <jplot@cig.ensmp.fr>
23 * The latest releases are found at
24 * http://www.cig.ensmp.fr/~vanderlee/jplot.html
25 *
26 * Initally developed for use by the Centre d'Informatique Geologique
27 * Ecole des Mines de Paris, Fontainebleau, France.
28 */
29
30 package jplot;
31
32 import javax.swing.*;
33 import java.awt.geom.*;
34 import java.awt.*;
35
36 /**
37 * Linestyle is a class which shows the current drawing style.
38 * Returns a panel or canvas on which is paint the drawing
39 * style according to the parameters past to the class.
40 */
41 public class LineStyle extends JPanel {
42 private Color backgroundColor;
43 private Stroke stroke;
44 private Stroke pointStroke;
45 private Dimension panelSize;
46 private double x_inset;
47 private double y_inset;
48 private double lineLength;
49 private LinePars lp;
50 private Rectangle rectangle;
51
52 public LineStyle(LinePars l) {
53 lp = l;
54 backgroundColor = Color.white;
55 panelSize = new Dimension(45,16);
56 lineLength = 35.0;
57 x_inset = ((double)panelSize.width - lineLength)/2.0;
58 y_inset = (double)panelSize.height/2.0;
59 rectangle = new Rectangle(0,0,panelSize.width,panelSize.height);
60 }
61
62 private void initialize() {
63 float[] dash = {lp.getDashLength()};
64 pointStroke = new BasicStroke(lp.getPenWidth());
65 if (dash[0] < 0.0f) stroke = new BasicStroke(0.0f);
66 else if (dash[0] == 0.0f) stroke = pointStroke;
67 else stroke = new BasicStroke(lp.getPenWidth(),BasicStroke.CAP_SQUARE,
68 BasicStroke.JOIN_MITER,10.0f,dash,0.0f);
69 }
70
71 public void paintComponent(Graphics g) {
72 Graphics2D g2 = (Graphics2D) g;
73 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
74 RenderingHints.VALUE_ANTIALIAS_ON);
75
76 // clear the canvas:
77 g2.setColor(backgroundColor);
78 g2.fill(rectangle);
79
80 // plot the linestyle:
81 initialize();
82 g2.setColor(lp.getColor());
83 if (lp.drawLine()) {
84 g2.setStroke(stroke);
85 g2.draw(new Line2D.Double(x_inset,y_inset,lineLength+x_inset,y_inset));
86 }
87 if (lp.getSymbol() < 13 && lp.getSymbolSize() > 0.0f) {
88 g2.setStroke(pointStroke);
89 if (lp.drawSymbol()) {
90 Graph.drawPointType(lp.getSymbol(),g2,x_inset+lineLength/2.0,
91 y_inset,lp.getSymbolSize());
92 }
93 }
94 }
95
96 public Dimension getPreferredSize() {
97 return panelSize;
98 }
99
100 public LinePars getLinePars() {
101 return lp;
102 }
103
104 public void setLinePars(LinePars lp) {
105 this.lp = lp;
106 }
107
108 public void setColor(Color c) {
109 lp.setColor(c);
110 }
111
112 public void setPenWidth(float f) {
113 lp.setPenWidth(f);
114 }
115
116 public void setBackground(Color c) {
117 backgroundColor = c;
118 }
119 }