/Users/lyon/j4p/src/graphics/dclap/SavePICT.java
|
1 // savepict.java
2 // d.g.gilbert, 1997
3
4 package graphics.dclap;
5
6 // pack edu.indiana.bio.graphics.dclap;
7
8
9 import java.applet.Applet;
10 import java.awt.*;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.PrintStream;
15
16
17 public class SavePICT {
18 Applet app = null;
19
20 public SavePICT() {
21 }
22
23 public SavePICT(Applet app) {
24 this.app = app;
25 }
26
27 protected void showStatus(String msg) {
28 if (app == null)
29 System.out.println(msg);
30 else
31 app.showStatus(msg);
32 }
33
34
35 public boolean saveToFile(PrintStream ps, String data) {
36 boolean okay = false;
37 try {
38 ps.print(data);
39 ps.close();
40 showStatus("Printing Complete.");
41 okay = true;
42 } catch (Throwable t) {
43 showStatus("Error while saving file.");
44 System.out.println(t);
45 }
46 return okay;
47 }
48
49 public void saveAsPict(Component theView, OutputStream os) {
50 Gr2PICT pict = new Gr2PICT(os, theView.getGraphics());
51 theView.paintAll(pict);
52 paintInside(theView, pict);
53 pict.finalize(); // make sure pict end is written
54 }
55
56 /**
57 toFile - saves any Frame to a pict file.
58 @author Douglas Lyon
59 @date 1/7/99
60 @version Kahindu 2.5
61 */
62 public static void toFile(Frame f) {
63 FileDialog fd = new FileDialog(f,
64 "Enter a pict file name", FileDialog.SAVE);
65 fd.show();
66 fd.setVisible(false);
67 String fn = fd.getDirectory() + fd.getFile();
68 //if usr canceled, return
69 if (fd.getFile() == null) return;
70 try {
71 FileOutputStream fos =
72 new FileOutputStream(fn);
73 Component c = f;
74 SavePICT p = new SavePICT();
75 p.saveAsPict(c, (OutputStream) fos);
76 fos.close();
77
78 } catch (IOException e) {
79 System.out.println(e);
80 }
81 }
82
83
84 protected void paintInside(Component comp, Gr2PICT g) {
85 Point p = comp.getLocation();
86
87 // set the origin for this component
88 g.translate(p.x, p.y);
89
90 // now draw this component's children inside its coordinate system
91 if (comp instanceof Container) {
92 Component[] comps = ((Container) comp).getComponents();
93 for (int i = 0; i < comps.length; i++)
94 paintInside(comps[i], g);
95 } else
96 updateComponent(comp, g);
97
98 // restore the coordinate system
99 g.translate(-p.x, -p.y);
100 }
101
102
103 protected void updateComponent(Component c, Graphics g) {
104 // draw a few special types of Component
105 Rectangle b = c.getBounds();
106 int halfheight = b.height / 2;
107 Color backc = c.getBackground(); // Color.white
108 Color forec = c.getForeground(); // Color.black;
109
110 if (c instanceof Button) {
111 if (c.getFont() != null) g.setFont(c.getFont());
112 g.setColor(backc);
113 g.fillRoundRect(0, 0, b.width, b.height, 12, 12);
114 g.setColor(forec);
115 g.drawRoundRect(0, 0, b.width, b.height, 12, 12);
116 String labs = ((Button) c).getLabel();
117 int x = (b.width - g.getFontMetrics().stringWidth(labs)) / 2;
118 g.drawString(labs, x, halfheight + 3);
119 } else if (c instanceof Checkbox) {
120 Checkbox cbx = (Checkbox) c;
121 int cwide = 12;
122 int cwide2 = cwide / 2;
123 int cwide3 = (cwide - cwide2) / 2;
124 int x = 3, y = 3;
125 if (c.getFont() != null) g.setFont(c.getFont());
126
127 if (cbx.getCheckboxGroup() != null) {
128 // radio button
129 g.setColor(backc);
130 g.fillOval(x, y, cwide, cwide);
131 g.setColor(forec);
132 g.drawOval(x, y, cwide, cwide);
133 if (cbx.getState()) g.fillOval(x + cwide3, y + cwide3, cwide2, cwide2);
134 } else {
135 // checkbox
136 g.setColor(backc);
137 g.fillRect(x, y, cwide, cwide);
138 g.setColor(forec);
139 g.drawRect(x, y, cwide, cwide);
140 if (cbx.getState()) {
141 g.drawLine(x, y, x + cwide - 1, y + cwide - 1);
142 g.drawLine(x + cwide - 1, y, x, y + cwide - 1);
143 }
144 }
145 x += cwide + 2;
146 g.drawString(cbx.getLabel(), x + 2, halfheight + 3);
147 } else if (c instanceof Choice) {
148 if (c.getFont() != null) g.setFont(c.getFont());
149 g.setColor(Color.black);
150 g.fillRect(3, 3, b.width - 4, b.height - 4);
151 g.setColor(Color.white);
152 g.fillRect(0, 0, b.width - 2, b.height - 2);
153 g.setColor(forec);
154 g.drawRect(0, 0, b.width - 2, b.height - 2);
155
156 // drop arrow
157 int[] xp = {b.width - 16, b.width - 6, b.width - 11, b.width - 16};
158 int[] yp = {halfheight - 3, halfheight - 3, halfheight + 2, halfheight - 3};
159 g.fillPolygon(xp, yp, 4);
160
161 g.drawString(((Choice) c).getSelectedItem(), 4, halfheight + 3);
162 } else if (c instanceof Label) {
163 if (c.getFont() != null) g.setFont(c.getFont());
164 g.setColor(forec);
165 g.drawString(((Label) c).getText(), 2, halfheight + 3);
166 } else if (c instanceof TextComponent) {
167 if (c.getFont() != null) g.setFont(c.getFont());
168 g.setColor(backc);
169 g.fillRect(0, 0, b.width, b.height);
170 g.setColor(forec);
171 g.drawRect(0, 0, b.width, b.height);
172 g.drawString(((TextComponent) c).getText().trim(), 2, halfheight + 3);
173 } else {
174 // let it draw itself
175 c.update(g);
176 }
177 }
178
179 }