// // // // Gifin // // Written by // Pradubkiat Bouklee & Ming-shu Chung // EE Department, Columbia University // // // Language: Java 1.1 import java.awt.*; import java.io.*; import java.awt.Graphics; import java.awt.Font; import java.awt.event.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Gifin extends Frame implements ActionListener,ItemListener,TextListener { QuickLabelPanel panel1,panel2; TextField stringTextfield,outputTextfield; Label inputStringLabel,fontLabel,sizeLabel; Label horiLabel,vertiLabel,dirLabel,outputLabel; Choice sizeChoice; Choice horiChoice; Choice vertiChoice; Choice fontChoice; Choice dirChoice; WaveText canvas1; Button generate,start,stop; private MenuBar mbar; private MenuItem quit,rainbowText,about; Image offScreen = null; private MenuItemListener menuItemListener = new MenuItemListener(); public Gifin() { super("Gifin ^_^"); mbar = new MenuBar(); Menu fileMenu = new Menu("File",true); fileMenu.add(quit = new MenuItem("Quit")); mbar.add(fileMenu); Menu other = new Menu("Effects",true); other.add(rainbowText = new MenuItem("Rainbow Text")); mbar.add(other); Menu help = new Menu("Help",true); help.add(about = new MenuItem("About")); mbar.add(help); setMenuBar(mbar); quit.addActionListener(menuItemListener); rainbowText.addActionListener(menuItemListener); about.addActionListener(menuItemListener); setLayout(null); addNotify(); setSize(getInsets().left + getInsets().right + 636,getInsets().top + getInsets().bottom + 336); panel1 = new QuickLabelPanel(); panel1.setLayout(null); panel1.setBounds(getInsets().left + 10,getInsets().top + 12,326,216); add(panel1); stringTextfield = new TextField(); stringTextfield.setText("Welcome to Gifin ^_^"); stringTextfield.setBounds(24,36,288,25); stringTextfield.addTextListener(this); //Add TextListener panel1.add(stringTextfield); outputTextfield = new TextField(); outputTextfield.setText("Gifin.gif"); outputTextfield.setBounds(24,180,288,25); outputTextfield.addTextListener(this); //Add TextListener panel1.add(outputTextfield); inputStringLabel = new Label("Input String:"); inputStringLabel.setBounds(12,12,100,28); panel1.add(inputStringLabel); fontLabel = new Label("Font:"); fontLabel.setBounds(12,60,100,24); panel1.add(fontLabel); sizeLabel = new Label("Size:"); sizeLabel.setBounds(132,60,100,24); panel1.add(sizeLabel); horiLabel = new Label("Horizontal Radius:"); horiLabel.setBounds(12,108,120,21); panel1.add(horiLabel); vertiLabel = new Label("Vertical Radius:"); vertiLabel.setBounds(132,108,120,21); panel1.add(vertiLabel); dirLabel = new Label("Direction:"); dirLabel.setBounds(232,60,84,24); panel1.add(dirLabel); outputLabel = new Label("Output file:"); outputLabel.setBounds(12,156,100,24); panel1.add(outputLabel); sizeChoice = new Choice(); sizeChoice.addItem("12"); sizeChoice.addItem("14"); sizeChoice.addItem("16"); sizeChoice.addItem("18"); sizeChoice.addItem("24"); sizeChoice.addItem("36"); sizeChoice.addItemListener(this); //Add ItemListener panel1.add(sizeChoice); sizeChoice.setBounds(144,84,48,21); sizeChoice.setBackground(Color.white); horiChoice = new Choice(); horiChoice.addItem("10"); horiChoice.addItem("20"); horiChoice.addItem("25"); horiChoice.addItem("30"); horiChoice.addItemListener(this); //Add ItemListener panel1.add(horiChoice); horiChoice.setBounds(24,132,48,21); horiChoice.setBackground(Color.white); vertiChoice = new Choice(); vertiChoice.addItem("10"); vertiChoice.addItem("20"); vertiChoice.addItem("25"); vertiChoice.addItem("30"); vertiChoice.addItemListener(this); //Add ItemListener panel1.add(vertiChoice); vertiChoice.setBounds(144,132,48,21); vertiChoice.setBackground(Color.white); fontChoice = new Choice(); { String fontNames[] = getToolkit().getFontList(); for (int i=0; i < fontNames.length; ++i) fontChoice.add(fontNames[i]); } fontChoice.addItemListener(this); //Add ItemListener panel1.add(fontChoice); fontChoice.setBounds(24,84,100,21); fontChoice.setBackground(Color.white); dirChoice = new Choice(); dirChoice.addItem("Clockwise"); dirChoice.addItem("Counter"); dirChoice.addItemListener(this); //Add ItemListener panel1.add(dirChoice); dirChoice.setBounds(240,84,72,21); dirChoice.setBackground(Color.white); generate = new Button("Generate"); generate.setBounds(240,130,72,28); generate.setBackground(Color.pink); generate.addActionListener(this); //Add ActionListener panel1.add(generate); start = new Button("Start"); start.setBounds(getInsets().left + 396,getInsets().top + 200,72,28); start.addActionListener(this); //Add ActionListener add(start); stop = new Button("Stop"); stop.setBounds(getInsets().left + 516,getInsets().top + 200,72,28); stop.addActionListener(this); //Add ActionListener add(stop); canvas1 = new WaveText(offScreen); canvas1.start(); setLayout(new FlowLayout()); add("South",canvas1); setTitle("Gifin ^_^"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { dispose(); System.exit(0); } }); } public static void main(String args[]) throws Exception{ Gifin gifin = new Gifin(); // gifin.pack(); gifin.setVisible(true); System.out.println("Start Gifin"); return; }//End of main public void actionPerformed(ActionEvent buttonClick) { if (buttonClick.getSource() == start) { canvas1.start(); System.out.print("Canvas start\n"); } if (buttonClick.getSource() == stop) { canvas1.stop(); System.out.print("Canvas stop\n"); } if (buttonClick.getSource() == generate) { try { canvas1.setFlagGenerate(true,true); }catch (Exception e) { System.out.println("IOException thrown:\n" + e); } } else {} }//End of actionPerformed public void itemStateChanged(ItemEvent event) { if (dirChoice == event.getItemSelectable()) { if (dirChoice.getSelectedIndex() == 0) canvas1.setDirection(1); else { canvas1.setDirection(-1); } //System.out.println("GetSelectedIndex" + dirChoice.getSelectedIndex()); //System.out.println("Get dirChoice Event"); } else if(horiChoice == event.getItemSelectable()) { canvas1.setHorizontalRadius(Integer.parseInt(horiChoice.getSelectedItem())); //System.out.println("Get horiChoice event"); } else if(vertiChoice == event.getItemSelectable()) { canvas1.setVerticalRadius(Integer.parseInt(horiChoice.getSelectedItem())); //System.out.println("Get vertiChoice event"); } else if((fontChoice == event.getItemSelectable()) || (sizeChoice == event.getItemSelectable())) { canvas1.setFont(fontChoice.getSelectedItem(),Integer.parseInt(sizeChoice.getSelectedItem())); //System.out.println("Get fontChoice Event"); } else {} }//End of itemStateChanged public void textValueChanged(TextEvent event) { if(stringTextfield == event.getSource()) { TextComponent tc = (TextComponent)event.getSource(); String str = tc.getText(); canvas1.setStringInput(str); //System.out.println("Get stringTextfield event"); } else if (outputTextfield == event.getSource()) { TextComponent tc = (TextComponent)event.getSource(); String str = tc.getText(); canvas1.setFilename(str); //System.out.println("Filename changed to \"" + str + "\""); } else {} }//End of textValueChanged class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent event) { MenuItem item = (MenuItem)event.getSource(); System.out.println(item.getLabel()); if (item == about) { About about = new About(); } else if (item == rainbowText) { new RainbowCanvas(); } else if (item == quit) { dispose(); System.exit(0); } else { try{} catch (Exception e) {}} } }//End of MenuItemListener class class About { Button ok; Frame myFrame; Dialog dialog; public About() { myFrame = new Frame(); dialog = new Dialog(myFrame,"About Gifin",true); dialog.setLayout(new GridLayout(4,1)); dialog.add(new Label("Gifin ^_^",Label.CENTER)); dialog.add(new Label("by Pradubkiat Bouklee and Ming-Shu Chung",Label.CENTER)); dialog.add(new Label("pradu@ctr.columbia.edu",Label.CENTER)); Panel pp = new Panel(); ok = new Button("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { dialog.dispose(); myFrame.dispose(); } }); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { dialog.dispose(); System.out.println("About Dialog closing"); } public void windowclosed(WindowEvent event) { System.out.println("About Dialog Closed"); } }); pp.add(ok); dialog.add("South",pp); dialog.setSize(250,200); dialog.setResizable(false); dialog.show(); } }//End of About class }//End of Gifin class WaveText extends Canvas implements Runnable { Thread blinker = null; boolean blockFlag = false; boolean fileFlag = false; int imageCount = 1; int direction = 1; // 1 is clockwise, -1 is counterclockwise int horizontalRadius = 10; int verticalRadius = 10; int phase = 0; String str = null; String fileName = "Gifin.gif"; char theChars[]; Image offScreen = null; Graphics offScreenG; OutputStream output; public WaveText(Image offScreen) { String paramStr = null; str = "Welcome to Gifin ^_^"; setBackground(Color.black); String directionInString = "1"; //Set Direction direction = Integer.parseInt(directionInString); String horizonString = "10"; //Set Horizonal Radius horizontalRadius = Integer.parseInt(horizonString); String verticalString = "10"; //Set Vertical Radius verticalRadius = Integer.parseInt(verticalString); setFont(new Font("TimesRoman",Font.BOLD,36)); //Set Font if (str == null) { str = "Power of Java";} setSize(30+25*str.length()+2*horizontalRadius,80+2*verticalRadius); theChars = new char [str.length()]; str.getChars(0,str.length(),theChars,0); // System.out.println("Pass Parameter Setup"); this.offScreen = offScreen; // System.out.println("Pass this.offScreen = offScreen;"); // System.out.println("Width = " + this.getSize().width + " and "+"Height = "+this.getSize().height); blockFlag = false; fileFlag = false; // System.out.println("Pass WaveText Constructor"); }//End of WaveText Constructor public void setStringInput(String str) { this.str = str; } public void setFont(String str,int size) { this.setFont(new Font(str,Font.BOLD,size)); } public void setFlagGenerate(boolean blockFlag,boolean fileFlag) { this.blockFlag = blockFlag; this.fileFlag = fileFlag; } public void setDirection(int dir) { this.direction = dir; } public void setHorizontalRadius(int rad) { this.horizontalRadius = rad; } public void setVerticalRadius(int rad) { this.verticalRadius = rad; } public void setFilename(String str) { this.fileName = str; } public int getImageCount() { return imageCount; } public void update(Graphics g) { int x, y; double angle; setSize(30+25*str.length()+2*horizontalRadius,80+2*verticalRadius); theChars = new char [str.length()]; str.getChars(0,str.length(),theChars,0); offScreen = createImage(this.getSize().width,this.getSize().height); offScreenG = offScreen.getGraphics(); //System.out.println("Pass offScreenG = offScreen.getGraphic();"); offScreenG.setColor(Color.black); offScreenG.fillRect(0,0,this.getSize().width,this.getSize().height); phase+=direction; phase%=8; for(int i=0;i