1 import javax.swing.*;
2 import java.awt.event.*;
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.io.*;
6 import java.util.*;
7
8 public class PrefCalibImage extends PrefCalib implements java.io.Serializable,ActionListener {
9
10 private static final long serialVersionUID = Version.getSUID();
11
12 public static String BIGGER = "Bigger";
13 public static String MBIGGER = "Much Bigger";
14 public static String SMALLER = "Smaller";
15 public static String MSMALLER = "Much Smaller";
16
17 public static double bigfact = 4.0;
18 public static double smallfact = 1.4;
19
20 public double fact = 500;
21
22 public PrefCalibImage() {
23 scale = Print.imagescale;
24 }
25
26 public String getText() {
27 return("To Calibrate images, measure the image in the file calib.png\n"+
28 "with whatever printer setings or image viewer you care about\n"+
29 "and enter the length A in the box below. If the\n"+
30 "image is too small or large to measure use the buttons below\n"+
31 "to re-size it (you should get best results if the test\n"+
32 "pattern just fits on the screen or page).\n\n"+
33 "Mesurement MUST be in cm regardless of unit settings");
34 }
35
36 public void newPane() {
37 if (fact==0) fact = 100;
38 super.newPane();
39 JPanel sizepane = new JPanel();
40 sizepane.setLayout(new GridLayout(4,1));
41
42 JButton displaybutton = new JButton(BIGGER);
43 displaybutton.addActionListener(this);
44 sizepane.add(displaybutton);
45
46 displaybutton = new JButton(MBIGGER);
47 displaybutton.addActionListener(this);
48 sizepane.add(displaybutton);
49
50 displaybutton = new JButton(SMALLER);
51 displaybutton.addActionListener(this);
52 sizepane.add(displaybutton);
53
54 displaybutton = new JButton(MSMALLER);
55 displaybutton.addActionListener(this);
56 sizepane.add(displaybutton);
57
58 pane.add(sizepane);
59
60 afield.setText(""+fact*100*scale);
61
62 outputImage();
63 }
64
65 public String getTitle() {
66 return("Calibrate Image");
67 }
68
69 public void actionPerformed(ActionEvent e) {
70 if (e.getActionCommand()==BIGGER) fact *= smallfact;
71 else if (e.getActionCommand()==MBIGGER) fact *= bigfact;
72 else if (e.getActionCommand()==SMALLER) fact /= smallfact;
73 else if (e.getActionCommand()==MSMALLER) fact /= bigfact;
74 outputImage();
75 afield.setText(""+fact*100*scale);
76
77 }
78
79 public void save() {
80
81 super.save();
82 if (pane==null) return;
83
84 scale /= fact;
85
86 apply();
87 }
88
89 void outputImage() {
90 Comp comp;
91 try {
92 comp = (Comp)((new ObjectInputStream(getClass().getResource("calibrate.smb").openStream())).readObject());
93 } catch (Exception ex) {
94 ErrorLog.exception(ex,"Trouble reading Calibrate.smb");
95 comp=null;
96 }
97 Page p = null;
98 for (Iterator it = comp.members.iterator();it.hasNext();) {
99 Symbol s = (Symbol)it.next();
100 if (s instanceof Page) p = (Page)s;
101 }
102
103 p.ressetting = Page.PIXELMODE;
104 p.resolution = fact;
105 p.imageunit = Unit.pixel;
106 p.mapunit = Unit.pixel;
107 try {
108 FileOutputStream stream = new FileOutputStream("calib.png");
109 Dimension size = p.getSize(p.mapunit);
110 PngEncoder encode=new PngEncoder(size.width,size.height,stream);
111 encode.start();
112 BufferedImage pageimage = new BufferedImage(size.width,size.height,BufferedImage.TYPE_INT_ARGB);
113 View view = new View();
114 view.mapunit = Unit.pixel;
115 view.draw = pageimage.createGraphics();
116 view.draw.setColor(Color.white);
117 view.draw.fillRect(0,0,size.width,size.height);
118 view.draw.setColor(Color.black);
119
120 p.print(view,p.getFormat(p.mapunit),0);
121
122 try {
123 encode.addSlice(pageimage,null,null);
124 } catch (CookKillException e) {
125 ErrorLog.exception(e,"This can't be happening - there is a bug.");
126 }
127 encode.finish();
128 encode.clean();
129 stream.close();
130 } catch (IOException e) {
131 ErrorLog.exception(e,"Couldn't write calib.png");
132 }
133 }
134
135 public void apply() {
136 Print.imagescale=scale;
137 }
138
139 }
140