View Javadoc

1   package com.insanityengine.ghia.apps;
2   
3   import java.awt.*;
4   import java.awt.image.*;
5   import java.awt.event.*;
6   
7   import javax.swing.*;
8   import javax.swing.event.*;
9   
10  import java.util.*;
11  
12  /***
13   *
14   * <P>
15   * Massage those comments into fat ascii text boundary marker
16   * </P>
17   *
18   * @author BrianHammond
19   *
20   */
21  
22  public class CommentMassager extends JFrame 
23  implements ActionListener, ItemListener {
24  	
25  	// lamer...
26  	public final static void main( String argv[] ) {
27  		CommentMassager l = new CommentMassager( argv );
28  		l.setVisible( true );
29  	}	
30  
31  	public CommentMassager( String argv[] ) {
32  		super( "CommentMassager" );
33  		createUI( argv );
34  
35  		addWindowListener( new WindowAdapter() {
36  			public void windowClosing( WindowEvent e ) {
37  				System.exit( 0 );
38  			}
39  		} );
40  	}
41  	
42  	// silliness
43  
44  	public void println( Object o ) {
45  		System.out.println( o );
46  	}
47  
48  	JTextArea area;
49  	JTextField msg, fnt;
50  	JCheckBox cbox;
51  
52  
53  	private void createUI( String argv[] ) {
54  		Container main = getContentPane();
55  
56  		JMenuBar menuBar = new JMenuBar();
57  		setJMenuBar( menuBar );
58  		
59  		JMenu menu = new JMenu( "File" );
60  		menu.setMnemonic( KeyEvent.VK_F );
61  		menuBar.add( menu );
62  
63  		JMenuItem menuItem = new JMenuItem( "Quit", KeyEvent.VK_Q );
64  		menu.add( menuItem );
65  		menuItem.addActionListener( new ActionListener() {
66  			public void actionPerformed(ActionEvent e) {
67  				System.exit( 0 );
68  			}
69  		} );
70  		
71  		main.setLayout( new BorderLayout() );
72  
73  		msg = new JTextField( "TEST" );
74  		fnt = new JTextField( "TimesRoman.BOLD.12" );
75  
76  		area = new JTextArea( 12, 80 );
77  		area.setFont( new Font( "Courier", Font.PLAIN, 12 ) );
78  		area.setEditable( false );
79  
80  		JPanel north = new JPanel();
81  			north.setLayout( new BorderLayout() );
82  
83  			JPanel weak = new JPanel();
84  				weak.setLayout( new BorderLayout() );
85  
86  			JPanel msg_row = new JPanel();
87  				msg_row.setLayout( new BorderLayout() );
88  				msg_row.add( new JLabel( "Message" ), BorderLayout.WEST );
89  				msg_row.add( msg, BorderLayout.CENTER );
90  			weak.add( msg_row, BorderLayout.NORTH );
91  			JPanel fnt_row = new JPanel();
92  				fnt_row.setLayout( new BorderLayout() );
93  				fnt_row.add( new JLabel( "Font"  ), BorderLayout.WEST );
94  				fnt_row.add( fnt, BorderLayout.CENTER );
95  			weak.add( fnt_row, BorderLayout.SOUTH );
96  			
97  		north.add( weak, BorderLayout.CENTER );
98  		JButton butt = new JButton( "[ATTACK]" );
99  			butt.addActionListener( this );
100 
101 		north.add( butt, BorderLayout.EAST );
102 		main.add( north, BorderLayout.NORTH );
103 
104 		cbox = new JCheckBox( "Use Hashes not Slashes" );
105 		cbox.addItemListener( this );
106 		JPanel south = new JPanel();
107 			south.add( cbox );
108 			//south.add( new JLabel( "Use Hashes not Slashes" ) );
109 		main.add( south, BorderLayout.SOUTH );
110 
111 		main.add( area, BorderLayout.CENTER );
112 
113 		pack();
114 	}
115 
116 	Image img = null;
117 	Graphics g = null;
118 	int w, h;
119 
120 	int pixels[] = null;
121 	boolean hash = false;
122 
123 	public void itemStateChanged( ItemEvent e ) {
124 		hash = !hash;
125 	}
126 
127 	public void actionPerformed(ActionEvent e) {
128 		String msg_str = msg.getText();
129 		String fnt_str = fnt.getText();
130 
131 		w = 400;
132 		h = 64;
133 
134 
135 		if ( null == img ) {
136 			img = createImage( w, h );
137 			g = img.getGraphics();
138 			pixels = new int[ w * h ];
139 		}
140 		if ( null == img ) {
141 			area.setText( "boohoo... could not create image..." );
142 			return;
143 		}
144 
145 		StringTokenizer st = new StringTokenizer( fnt_str, "." );
146 		String fname = "TimesRoman";
147 		int ftype = Font.BOLD;
148 		int fsize = 12;
149 			
150 		String str;
151 		str = st.nextToken();
152 		if ( null != str ) fname = str;
153 
154 		str = st.nextToken();
155 		if ( null != str ) {
156 			switch( str.charAt( 0 ) ) {
157 				case 'B': ftype = Font.BOLD; break;
158 				//case 'I': ftype = Font.ITALICS; break;
159 				case 'P': ftype = Font.PLAIN; break;
160 			}
161 		}
162 
163 		str = st.nextToken();
164 		if ( null != str ) {	
165 			try {
166 				fsize = Integer.valueOf( str ).intValue();
167 			} catch ( Exception eee ) {
168 			}
169 		}
170 		System.out.println( "" + " fname = " + fname  + " ftype = " + ftype  + " fsize = " + fsize );
171 
172 
173 		Font f = new Font( fname, ftype, fsize );
174 		if ( null != f ) g.setFont( f );
175 		else System.out.println( "bad font!" );
176 
177 		System.out.println( "msg_str = " + msg_str  + " fnt_str = " + fnt_str );
178 
179 		g.setColor( Color.white );
180 		g.fillRect( 0, 0, w, h );
181 		g.setColor( Color.black );
182 		g.drawString( msg_str, 0, 20 );
183 
184 	
185 		PixelGrabber pg;
186      	pg = new PixelGrabber( img.getSource(), 0, 0, w, h, pixels, 0, w );
187         try { 
188 			pg.grabPixels(); 
189 		} catch ( Exception egg ) {
190 			egg.printStackTrace();
191 			return;
192 		}
193 
194 		int max = w * h;
195 		int i, j, idx;
196 
197 		int x1, y1;
198 		int x2, y2;
199 
200 		x2 = y2 = -1;
201 		x1 = y1 = w + h;
202 
203 		for ( i = idx = 0 ; i < h ; i++ ) {
204 			for ( j = 0 ; j < w ; j++, idx++ ) {
205 				if ( -1 != pixels[ idx ] ) {
206 				
207 					if ( j > x2 ) x2 = j;
208 					if ( i > y2 ) y2 = i;
209 
210 					if ( j < x1 ) x1 = j;
211 					if ( i < y1 ) y1 = i;
212 				}
213 			}
214 		}
215 System.out.println( "" + " x1 = " + x1  + " y1 = " + y1  + " x2 = " + x2  + " y2 = " + y2 );
216 
217 
218 		String add;
219 		String com = "//";
220 		String spc = "  ";
221 		
222 		if ( hash ) { 
223 			com = "#";
224 			spc = " ";
225 		}
226 
227 		area.setText( "" );
228 
229 		for ( i = y1 ; i <= y2 ; i++ ) {
230 
231 			for ( j = x1 ; j <= x2 ; j++ ) {
232 				idx = j + i * w;
233 
234 				if ( -1 != pixels[ idx ] ) add = com;
235 				else add = spc;
236 
237 				area.append( add );
238 				System.out.print( add );
239 			}
240 			area.append( "\n" );
241 			System.out.println();
242 			
243 		}
244 	}
245 };
246 
247 /***
248  *
249  * $Log: CommentMassager.java,v $
250  * Revision 1.4  2005/03/19 17:50:02  brian
251  * repackaging
252  *
253  * Revision 1.3  2004/09/01 01:10:42  brian
254  * fix class level javadoc placement
255  *
256  * Revision 1.2  2004/09/01 00:11:06  brian
257  * author, log and header stuff
258  *
259  *
260  */