import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.text.*; import java.lang.* ; class fs extends Frame implements ActionListener { public Toolkit tk; public Image img; public TextField tf_date; public PrintCanvas canvas; public Label lbl_pageno; public TextField tf_pageno; public Button but_display; public Button but_print; public Label lbl_message; public TextArea ta_message; public Button but_help; public Button but_exit; public Vector vec; public int i; public fs(String filename) { // initial layout setTitle("Flight Schedule"); setResizable(false); setForeground(Color.black); // setBackground(Color.lightGray); setBackground(new Color(255,255,210) ); setLayout(null); // instantiate tk = getToolkit(); img = tk.getImage("air.gif"); tf_date = new TextField(""); canvas = new PrintCanvas(filename); lbl_pageno = new Label("Enter Page No:",Label.LEFT); tf_pageno = new TextField("1",10); but_display = new Button("Display"); but_print = new Button("Print"); lbl_message = new Label("MESSAGE / ACTION / ERROR",Label.LEFT); ta_message = new TextArea(); but_help = new Button("Help"); but_exit = new Button("Exit"); // set date tf_date.setText( Validate.getDate() ); // add() add(canvas); add(tf_date); add(lbl_pageno); add(tf_pageno); add(but_display); add(but_print); add(lbl_message); add(ta_message); add(but_help); add(but_exit); // addActionListener but_display.addActionListener(this); but_print.addActionListener(this); but_help.addActionListener(this); but_exit.addActionListener(this); // setBounds setBounds(0,0,800,600); tf_date. setBounds(580,100,215, 25); canvas. setBounds( 0, 0,450,600); lbl_pageno. setBounds(560,140, 80, 25); tf_pageno. setBounds(660,140, 30, 25); but_display.setBounds(560,170, 60, 25); but_print. setBounds(640,170, 60, 25); lbl_message.setBounds(470,215,350, 25); ta_message. setBounds(470,240,350,100); but_help. setBounds(560,360, 60, 25); but_exit. setBounds(640,360, 60, 25); pack(); // final settings canvas.setBackground(Color.white); but_help.setVisible(false); tf_date.setEnabled(false); ta_message.setEditable(false); this.requestFocus(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == but_display) { try { // check whether it is integer Integer.parseInt(tf_pageno.getText()); // check whether that file exists String f; File fname; f = tf_pageno.getText() + ".txt"; fname = new File(f) ; if ( !fname.exists() ) { ta_message.setText ("ERROR: File " + f + " does not exist" + "\n"); ta_message.append ("SOLUTION: " + "For more details kindly refer Help"); return; } else { ta_message.setText (""); } // readfile , with write file flag false canvas.readFile(f,vec,"f"); // display on screen canvas.repaint(); } catch(Exception er) { ta_message.setText ("ERROR: Incorrect data entered" + "\n"); ta_message.append ("EXCEPTION:" + "[" + er + "]" + "\n"); ta_message.append ("SOLUTION: " + "For more details kindly refer Help"); } } // end of display if (e.getSource() == but_print) { PrintJob pjob = getToolkit().getPrintJob(this,"Printing Flight Schedule",null); if (pjob != null) { Graphics pg = pjob.getGraphics(); if (pg != null) { canvas.printAll(pg); pg.dispose(); // flush page } pjob.end(); } } // end of print if (e.getSource() == but_help) { // } // end of help if (e.getSource() == but_exit) { this.dispose(); } // end of exit } // end of action performed public void paint(Graphics g) { g.drawImage(img,450,45,350,40,this); } // end of paint // main public static void main(String[] args) { if ( args.length == 1) { fs fs = new fs(args[0]); fs.show(); } else { System.out.println("Usage: java fs "); } } // end of main } // end of class // ************ new class *************** // PrintCanvas class class PrintCanvas extends Canvas { public Vector vec; public int i; public String ss1,ss2,ss3,ss4,ss5,ss6,ss7,ss8,ss9; public int pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9; PrintCanvas(String filename) { // initila values of positions pos1 = 0; pos2 = 10; pos3 = 19; pos4 = 24; pos5 = 37; pos6 = 44; pos7 = 57; pos8 = 64; pos9 = 73; // read the entire file with writeflag set to "t" readFile(filename,vec,"t"); // read 1st file & display on screen readFile("1.txt",vec,"f"); // display repaint(); } // end of constructor // method to read file public void readFile(String filename,Vector v,String flag) { try { int f = 1; v = new Vector(); String s; // stores each line read from the BufferedReader String tmp; // stores the name of the country last read from the file/buffered reader int lineno; FileInputStream fis = new FileInputStream(filename); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); tmp = ""; lineno = 1; if (flag == "t") { while ( (s = br.readLine()) != null) { if ( !s.equals("") ) { // country name + (contd.) to be added in case of pg. c/f if ( s.substring(0,4).equals("FROM") ) { tmp = s; tmp = tmp + " - (CONTD.)"; // System.out.println(tmp); } // end if (lineno >= 25) // 55 { if ( ((s.substring(0,4).equals("FROM"))) || ((s.indexOf("[") != -1) && (!s.substring(0,4).equals("FROM"))) ) { writeFile(f,v); // System.out.println(lineno); v = new Vector(); f++; lineno = 0; // country name + (contd.) added in c/f page/file if ( (s.indexOf("[") != -1) && (!s.substring(0,4).equals("FROM")) ) { v.addElement(tmp); v.addElement(""); lineno = lineno + 2; }// end of adding country name } } } v.addElement(s); lineno++; } writeFile(f,v); vec = v; br.close(); fis.close(); } else { while ( (s = br.readLine()) != null) { v.addElement(s); lineno++; } vec = v; br.close(); fis.close(); } // System.out.println(lineno); } catch(Exception er) { //ta_message.setText("ERROR: I/O error / File does not exist" + "\n"); //ta_message.append ("EXCEPTION:" + "[" + er + "]" + "\n"); //ta_message.append ("SOLUTION: " + "For more details kindly refer Help"); } } // end of readfile method // method to write file public void writeFile(int f,Vector v) throws Exception { String outfilename ; outfilename = String.valueOf(f); outfilename = outfilename + ".txt"; FileOutputStream fos = new FileOutputStream(outfilename); OutputStreamWriter osw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(osw); v.trimToSize(); int count = v.size(); for (i = 0; i < count ; i++) { String ss = (String)v.elementAt(i); ss = ss.trim(); bw.write(ss); bw.newLine(); } bw.flush(); fos.close(); } // end of write file method // method paint public void paint(Graphics g) { Font bold = new Font("Arial",Font.BOLD,12); Font plain = new Font("Arial",Font.PLAIN,12); String tmp = ""; int x=10; int y=40; vec.trimToSize(); int count = vec.size(); for (i = 0; i < count ; i++) { String s = (String)vec.elementAt(i); s = s.trim(); if ( !s.equals("") ) { if (s.length() >= 60) { if (s.length() >= 68) { ss1 = s.substring( 0, 9); ss2 = s.substring( 9,17); ss3 = s.substring(17,24); ss4 = s.substring(24,32); ss5 = s.substring(32,40); ss6 = s.substring(40,52); ss7 = s.substring(52,60); ss8 = s.substring(60,68); ss9 = s.substring(68); } else { ss1 = s.substring( 0, 9); ss2 = s.substring( 9,17); ss3 = s.substring(17,24); ss4 = s.substring(24,32); ss5 = s.substring(32,40); ss6 = s.substring(40,52); ss7 = s.substring(52,60); ss8 = s.substring(60); ss9 = ""; } } else { s = s ; ss1 = ss2 = ss3 = ss4 = ss5 = ss6 = ss7 = ss8 = ss9 = ""; } // determine whether it contains "[" and/or "FROM" if ( ( s.indexOf("[") != -1 ) && ( s.indexOf("FROM") != -1) ) { g.setFont(plain); g.setColor(Color.red); g.drawString(s,x,y); } else if( ( s.indexOf("[") != -1 ) && ( s.indexOf("FROM") == -1) ) { g.setFont(bold); g.setColor(Color.black); g.drawString(s,x,y); } else { g.setColor(Color.black); // determine whether it contains "*" if ( s.indexOf("*") != -1 ) // indicates change of flight { tmp = s; g.setFont(bold); g.drawString(ss1,x+0 ,y); g.setFont(plain); g.drawString(ss2,x+50 ,y); g.setFont(plain); g.drawString(ss3,x+100,y); g.setFont(bold); g.drawString(ss4,x+150,y); g.setFont(plain); g.drawString(ss5,x+200,y); g.setFont(plain); g.drawString(ss6,x+250,y); g.setFont(plain); g.drawString(ss7,x+300,y); g.setFont(plain); g.drawString(ss8,x+350,y); g.setFont(plain); g.drawString(ss9,x+380,y); } else // it does not contain "*" { if ( tmp != "" ) // indicates continuation of previous line { g.setFont(plain); g.drawString(ss1,x+0 ,y); g.setFont(plain); g.drawString(ss2,x+50 ,y); g.setFont(plain); g.drawString(ss3,x+100,y); g.setFont(plain); g.drawString(ss4,x+150,y); g.setFont(bold); g.drawString(ss5,x+200,y); g.setFont(plain); g.drawString(ss6,x+250,y); g.setFont(plain); g.drawString(ss7,x+300,y); g.setFont(plain); g.drawString(ss8,x+350,y); g.setFont(plain); g.drawString(ss9,x+380,y); } else // no continuation { g.setFont(bold); g.drawString(ss1,x+0 ,y); g.setFont(plain); g.drawString(ss2,x+50 ,y); g.setFont(plain); g.drawString(ss3,x+100,y); g.setFont(bold); g.drawString(ss4,x+150,y); g.setFont(bold); g.drawString(ss5,x+200,y); g.setFont(plain); g.drawString(ss6,x+250,y); g.setFont(plain); g.drawString(ss7,x+300,y); g.setFont(plain); g.drawString(ss8,x+350,y); g.setFont(plain); g.drawString(ss9,x+380,y); } tmp = ""; // initialise } } } // end y=y+20; } } // end of paint } // end of PrintCanvas class