5 import java.awt.event.*;
10 static final String DEFAULT_PARAM=
"-t 2 -c 100";
21 final static Color colors[] =
33 point(
double x,
double y,
byte value)
43 Vector<point> point_list =
new Vector<point>();
44 byte current_value = 1;
50 final Button button_change =
new Button(
"Change");
51 Button button_run =
new Button(
"Run");
52 Button button_clear =
new Button(
"Clear");
53 Button button_save =
new Button(
"Save");
54 Button button_load =
new Button(
"Load");
57 BorderLayout layout =
new BorderLayout();
58 this.setLayout(layout);
60 Panel p =
new Panel();
61 GridBagLayout gridbag =
new GridBagLayout();
64 GridBagConstraints
c =
new GridBagConstraints();
65 c.fill = GridBagConstraints.HORIZONTAL;
68 gridbag.setConstraints(button_change,
c);
69 gridbag.setConstraints(button_run,
c);
70 gridbag.setConstraints(button_clear,
c);
71 gridbag.setConstraints(button_save,
c);
72 gridbag.setConstraints(button_load,
c);
75 gridbag.setConstraints(input_line,
c);
85 this.
add(p,BorderLayout.SOUTH);
87 button_change.addActionListener(
new ActionListener()
88 {
public void actionPerformed (ActionEvent e)
91 button_run.addActionListener(
new ActionListener()
92 {
public void actionPerformed (ActionEvent e)
93 { button_run_clicked(input_line.getText()); }});
95 button_clear.addActionListener(
new ActionListener()
96 {
public void actionPerformed (ActionEvent e)
97 { button_clear_clicked(); }});
99 button_save.addActionListener(
new ActionListener()
100 {
public void actionPerformed (ActionEvent e)
101 { button_save_clicked(input_line.getText()); }});
103 button_load.addActionListener(
new ActionListener()
104 {
public void actionPerformed (ActionEvent e)
105 { button_load_clicked(); }});
107 input_line.addActionListener(
new ActionListener()
108 {
public void actionPerformed (ActionEvent e)
109 { button_run_clicked(input_line.getText()); }});
111 this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
114 void draw_point(
point p)
118 Graphics window_gc = getGraphics();
119 buffer_gc.setColor(
c);
120 buffer_gc.fillRect((
int)(p.x*XLEN),(
int)(p.y*YLEN),4,4);
121 window_gc.setColor(
c);
122 window_gc.fillRect((
int)(p.x*XLEN),(
int)(p.y*YLEN),4,4);
130 buffer_gc.setColor(colors[0]);
131 buffer_gc.fillRect(0,0,XLEN,YLEN);
136 void draw_all_points()
143 void button_change_clicked()
149 private static double atof(String s)
151 return Double.valueOf(
s).doubleValue();
154 private static int atoi(String s)
156 return Integer.parseInt(
s);
159 void button_run_clicked(String
args)
184 StringTokenizer st =
new StringTokenizer(
args);
185 String[] argv =
new String[st.countTokens()];
186 for(
int i=0;i<argv.length;i++)
187 argv[i] = st.nextToken();
189 for(
int i=0;i<argv.length;i++)
191 if(argv[i].charAt(0) !=
'-')
break;
194 System.err.print(
"unknown option\n");
197 switch(argv[i-1].charAt(1))
253 System.err.print(
"unknown option\n");
270 for(
int i=0;i<
prob.
l;i++)
284 int[] j =
new int[
XLEN];
286 Graphics window_gc = getGraphics();
287 for (
int i = 0; i <
XLEN; i++)
289 x[0].
value = (double) i / XLEN;
290 j[i] = (int)(YLEN*
svm.svm_predict(
model,
x));
293 buffer_gc.setColor(colors[0]);
294 buffer_gc.drawLine(0,0,0,YLEN-1);
295 window_gc.setColor(colors[0]);
296 window_gc.drawLine(0,0,0,YLEN-1);
298 int p = (int)(
param.
p * YLEN);
299 for(
int i=1;i<
XLEN;i++)
301 buffer_gc.setColor(colors[0]);
302 buffer_gc.drawLine(i,0,i,YLEN-1);
303 window_gc.setColor(colors[0]);
304 window_gc.drawLine(i,0,i,YLEN-1);
306 buffer_gc.setColor(colors[5]);
307 window_gc.setColor(colors[5]);
308 buffer_gc.drawLine(i-1,j[i-1],i,j[i]);
309 window_gc.drawLine(i-1,j[i-1],i,j[i]);
313 buffer_gc.setColor(colors[2]);
314 window_gc.setColor(colors[2]);
315 buffer_gc.drawLine(i-1,j[i-1]+p,i,j[i]+p);
316 window_gc.drawLine(i-1,j[i-1]+p,i,j[i]+p);
318 buffer_gc.setColor(colors[2]);
319 window_gc.setColor(colors[2]);
320 buffer_gc.drawLine(i-1,j[i-1]-p,i,j[i]-p);
321 window_gc.drawLine(i-1,j[i-1]-p,i,j[i]-p);
329 for(
int i=0;i<
prob.
l;i++)
349 Graphics window_gc = getGraphics();
350 for (
int i = 0; i <
XLEN; i++)
351 for (
int j = 0; j <
YLEN ; j++) {
352 x[0].
value = (double) i / XLEN;
353 x[1].
value = (double) j / YLEN;
356 buffer_gc.setColor(colors[(
int)
d]);
357 window_gc.setColor(colors[(
int)
d]);
358 buffer_gc.drawLine(i,j,i,j);
359 window_gc.drawLine(i,j,i,j);
366 void button_clear_clicked()
371 void button_save_clicked(String
args)
373 FileDialog dialog =
new FileDialog(
new Frame(),
"Save",FileDialog.SAVE);
374 dialog.setVisible(
true);
375 String filename = dialog.getDirectory() + dialog.getFile();
376 if (filename ==
null)
return;
378 DataOutputStream fp =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(filename)));
381 int svm_type_idx =
args.indexOf(
"-s ");
382 if(svm_type_idx != -1)
384 StringTokenizer svm_str_st =
new StringTokenizer(
args.substring(svm_type_idx+2).trim());
385 svm_type =
atoi(svm_str_st.nextToken());
394 fp.writeBytes(p.y+
" 1:"+p.x+
"\n");
402 fp.writeBytes(p.value+
" 1:"+p.x+
" 2:"+p.y+
"\n");
406 }
catch (IOException e) { System.err.print(e); }
409 void button_load_clicked()
411 FileDialog dialog =
new FileDialog(
new Frame(),
"Load",FileDialog.LOAD);
412 dialog.setVisible(
true);
413 String filename = dialog.getDirectory() + dialog.getFile();
414 if (filename ==
null)
return;
417 BufferedReader fp =
new BufferedReader(
new FileReader(filename));
419 while((
line = fp.readLine()) !=
null)
421 StringTokenizer st =
new StringTokenizer(
line,
" \t\n\r\f:");
422 if(st.countTokens() == 5)
424 byte value = (byte)
atoi(st.nextToken());
426 double x =
atof(st.nextToken());
428 double y =
atof(st.nextToken());
431 else if(st.countTokens() == 3)
433 double y =
atof(st.nextToken());
435 double x =
atof(st.nextToken());
441 }
catch (IOException e) { System.err.print(e); }
447 if(e.getID() == MouseEvent.MOUSE_PRESSED)
449 if(e.getX() >=
XLEN || e.getY() >=
YLEN)
return;
450 point p =
new point((
double)e.getX()/
XLEN,
451 (
double)e.getY()/
YLEN,
463 buffer_gc =
buffer.getGraphics();
464 buffer_gc.setColor(
colors[0]);
480 public static void main(String[] argv)
482 new AppletFrame(
"svm_toy",
new svm_toy(),500,500+50);
486 class AppletFrame
extends Frame {
487 AppletFrame(String title, Applet applet,
int width,
int height)
490 this.addWindowListener(
new WindowAdapter() {
491 public void windowClosing(WindowEvent e) {
496 applet.setSize(width,height);
500 this.setVisible(
true);