1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import java.awt.*;
7    import javax.swing.*;
8    import java.awt.event.*;
9    public class Scale extends JComponent implements MouseListener,MouseMotionListener{
10     public double fact = 100;
11     double oldfact = fact;
12     static double HMARGIN = 0.1;
13     static double VMARGIN = 0.2;
14     double grabpoint;
15   
16     java.util.HashSet targets = new java.util.HashSet();
17   
18     public Scale(double initval,Scaleable target) {
19       super();
20       targets.add(target);
21       oldfact=fact=initval;
22       addMouseListener(this);
23       addMouseMotionListener(this);
24       setSize(100,50);
25       setPreferredSize(new Dimension(100,50));
26       setMinimumSize(new Dimension(100,50));
27     }
28   
29     public void addScaleable(Scaleable target) {
30       targets.add(target);
31     }
32   
33     public void removeScaleable(Scaleable target) {
34       targets.remove(target);
35     }
36   
37     public void setValue(double val) {
38       fact=oldfact=val;
39     }
40   
41     public void change(double newfact) {
42       if (newfact!=fact) {
43         fact=oldfact=newfact;
44         for (java.util.Iterator it = targets.iterator();it.hasNext();)
45           ((Scaleable)it.next()).setScale(fact);
46       }
47     }
48   
49     public void paint(Graphics g) {
50       double width = (getSize().width * (1-2*HMARGIN))/fact;
51       double exp;
52       for (exp = 1.0; exp<width; exp *=10.0);
53       for (; exp>=width; exp /= 10.0);
54       int mant = (int)(width/exp);
55       if (mant==1) {
56         mant = 10;
57         exp /= 10.0;
58       }
59   
60       int top = (int)(getSize().height*VMARGIN);
61       int bot = (int)(getSize().height*(1.0-VMARGIN));
62       double x = getSize().width*HMARGIN;
63       for (int i=0; i<mant; i++) {
64         g.drawLine((int)x,top,(int)x,bot);
65         x += exp*fact;
66       }
67       g.drawLine((int)x,top,(int)x,bot);
68       g.drawLine((int)(getSize().width*HMARGIN),getSize().height/2,(int)x,getSize().height/2);
69   
70       if (mant==10) {mant=1; exp*=10.0;}
71       String number;        
72       if (exp>=0.99)
73         for (number=Integer.toString(mant);exp>9.9;exp/=10.0) number = number+"0";
74       else {
75         for (number = "0.";exp<0.09;exp*=10) number = number + "0";
76         number = number + Integer.toString(mant);
77       }
78       x -= g.getFontMetrics().stringWidth(number)/2;
79       g.drawString(number,(int)x,(int)(getSize().height*VMARGIN)-1);
80     }
81     public void mouseClicked(MouseEvent evt) {}
82     public void mouseMoved(MouseEvent evt) {}
83     public void mouseExited(MouseEvent evt) {}
84     public void mouseEntered(MouseEvent evt) {}
85   
86     public void mousePressed(MouseEvent evt) {
87       grabpoint = evt.getX() - getSize().width*HMARGIN;
88       if (grabpoint<getSize().width*HMARGIN) grabpoint= getSize().width*HMARGIN;
89       oldfact=fact;
90     }
91     public void mouseDragged(MouseEvent evt) {
92       double point = evt.getX() - getSize().width*HMARGIN;
93       if (point<getSize().width*HMARGIN) point= getSize().width*HMARGIN;
94       fact *= point/grabpoint;
95       grabpoint = point;
96       repaint();
97     }
98     public void mouseReleased(MouseEvent evt) {
99       if (fact!=oldfact) 
100        for (java.util.Iterator it = targets.iterator();it.hasNext();)
101          ((Scaleable)it.next()).setScale(fact);
102    }
103  }
104  
105  
106  
107