1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import javax.swing.*;
7    import java.awt.*;
8    import java.awt.geom.*;
9    import java.util.*;
10   
11   public class SegPane extends JComponent implements Scaleable {
12     public Image image;
13     public Segment seg;
14     public double scale = 1.0;
15     
16     JScrollPane scrollpane;
17   
18     public SegPane() {
19       super();
20       scrollpane = new JScrollPane(this,
21           			 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
22           			 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
23     }
24   
25     public void cleanup() {
26       seg = null;
27       image = null;
28       scrollpane = null;
29     }
30   
31     public JScrollPane getScrollPane() {
32       return(scrollpane);
33     }
34   
35     public void setRotation(double a) {}
36   
37     public void setScale(double newscale) {
38       if (scale!=newscale) { 
39         scale = newscale;
40   //      System.out.println("before "+getSize());
41   //      Rectangle viewrect = scrollpane.getViewport().getViewRect();
42   //      System.out.println(""+scrollpane.getViewport().getViewRect());
43         setSize(new Dimension((int)(image.getWidth(this)*scale),
44           		    (int)(image.getHeight(this)*scale)));
45   //      System.out.println("image is ("+(int)(image.getWidth(this)*scale)+","+
46   //        		 (int)(image.getHeight(this)*scale)+" pane is "+getSize());
47   //      scrollpane.getViewport().setViewPosition(new Point(viewrect.x+1,viewrect.y+1));
48         repaint();
49   //      System.out.println(""+scrollpane.getViewport().getViewRect());
50   //      System.out.println("after "+getSize());
51       }
52     }
53   
54     public Dimension getPreferredSize() {
55       return(getSize());
56     }
57   
58     public Graphics getGraphics() {
59       Graphics2D res = (Graphics2D)super.getGraphics();
60       AffineTransform t = res.getTransform();
61       t.concatenate(AffineTransform.getScaleInstance(scale,scale));
62       res.setTransform(t);
63       return(res);
64     }
65   
66     public void paintComponent( java.awt.Graphics gr) {
67       if (image!=null) {
68         Graphics2D g = (Graphics2D)gr;
69         Color c = g.getColor();
70   
71   //      setBackground(Segment.backc);
72         g.setColor(Segment.backc);
73         java.awt.Rectangle r = getBounds();
74         g.fillRect(0,0,r.width,r.height);
75   
76         AffineTransform t = g.getTransform();
77         AffineTransform oldt = new AffineTransform(t);
78         t.concatenate(AffineTransform.getScaleInstance(scale,scale));
79         g.setTransform(t);
80   
81         g.drawImage(image,0,0,this);
82         if (seg.boundary.npoints>0) {
83           g.setColor(Segment.borderc);
84           g.drawPolygon(seg.boundary);
85         }
86         g.setTransform(oldt);
87         g.setColor(Segment.stationc);
88         seg.map.paint(g,scale);
89   //      float[] offset = seg.map.getOffset();
90         if (seg.map.overlaytrans!=null) {
91           g.setColor(Segment.surveyc);
92           AffineTransform overlay = new AffineTransform(seg.map.overlaytrans);
93           overlay.preConcatenate(AffineTransform.getScaleInstance(scale,scale));
94   //        overlay.scale(scale,scale);
95           seg.set.draw(g,overlay);
96         }
97         g.setColor(c);
98       }
99       else {
100        java.awt.Rectangle r = getBounds();
101        gr.drawLine(0,0,r.width,r.height);
102      }        	
103      return;
104    }
105  }
106