import java.awt.*; import java.awt.event.*; import java.io.*; import java.awt.Graphics; import java.awt.Font; import java.awt.event.*; public class RainbowCanvas extends Frame { Image offScreen = null; Frame f; RainbowPanel rainbowPanel; Panel panel; public RainbowCanvas() { f = new Frame("RainbowCanvas"); f.setLayout(new FlowLayout()); rainbowPanel = new RainbowPanel(); panel = new Panel(); panel.add(rainbowPanel); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { Frame frm = (Frame)event.getSource(); frm.setVisible(false); rainbowPanel.killRainbowTextThread(); frm.dispose(); System.out.println("Rainbow Text closed"); } }); f.add(panel); f.setSize(600,600); f.pack(); f.show(); } }//End of Class class RainbowPanel extends Panel implements ActionListener,TextListener,ItemListener { QuickLabelPanel panel; Panel panel2; RainbowText canvas; Label textInputLabel,fontLabel,fontStyleLabel,fontSizeLabel; Label backgroundColorLabel,speedLabel,outputLabel; Choice fontChoice,fontStyleChoice,fontSizeChoice,backgroundColorChoice; TextField inputTextfield,speedTextfield,outputTextfield; Button stop,start,save; FileDialog dialog; public RainbowPanel() { setLayout(new BorderLayout(2,2)); panel = new QuickLabelPanel("Rainbow Text"); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); c.insets = new Insets(2,5,2,5); // Input String Label c.gridx = 1; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; textInputLabel = new Label("Input String:"); gridbag.setConstraints(textInputLabel,c); panel.add(textInputLabel); //Input String c.gridx = 1; c.gridy = 2; c.gridwidth = 5; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; inputTextfield = new TextField("Hello... Gifin ^_^"); inputTextfield.addTextListener(this); gridbag.setConstraints(inputTextfield,c); panel.add(inputTextfield); // Font Label c.gridx = 1; c.gridy = 3; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; fontLabel = new Label("Font:"); gridbag.setConstraints(fontLabel,c); panel.add(fontLabel); //Font Choice c.gridx = 1; c.gridy = 4; c.gridwidth = 3; c.gridheight = 1; c.anchor = GridBagConstraints.NORTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; fontChoice = new Choice(); { String fontNames[] = getToolkit().getFontList(); for (int i=0; i < fontNames.length; ++i) fontChoice.add(fontNames[i]); } fontChoice.setBackground(Color.white); gridbag.setConstraints(fontChoice,c); fontChoice.addItemListener(this); panel.add(fontChoice); // Font Style Label c.gridx = 4; c.gridy = 3; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; fontStyleLabel = new Label("Style:"); gridbag.setConstraints(fontStyleLabel,c); panel.add(fontStyleLabel); //Font Style Choice c.gridx = 4; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.NORTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; fontStyleChoice = new Choice(); fontStyleChoice.add("Plain"); fontStyleChoice.add("Bold"); fontStyleChoice.add("Italic"); fontStyleChoice.add("Bold & Italic"); fontStyleChoice.setBackground(Color.white); gridbag.setConstraints(fontStyleChoice,c); fontStyleChoice.addItemListener(this); panel.add(fontStyleChoice); // Font Size Label c.gridx = 5; c.gridy = 3; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; fontSizeLabel = new Label("Size:"); gridbag.setConstraints(fontSizeLabel,c); panel.add(fontSizeLabel); //Font Size Choice c.gridx = 5; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.NORTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; fontSizeChoice = new Choice(); fontSizeChoice.add("12"); fontSizeChoice.add("14"); fontSizeChoice.add("16"); fontSizeChoice.add("18"); fontSizeChoice.add("24"); fontSizeChoice.add("36"); fontSizeChoice.setBackground(Color.white); gridbag.setConstraints(fontSizeChoice,c); fontSizeChoice.addItemListener(this); panel.add(fontSizeChoice); // Background Color Label c.gridx = 1; c.gridy = 5; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; backgroundColorLabel = new Label("Background Color:"); gridbag.setConstraints(backgroundColorLabel,c); panel.add(backgroundColorLabel); // Background Color Choice c.gridx = 1; c.gridy = 6; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.NORTHWEST; c.fill = GridBagConstraints.NONE;//HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; backgroundColorChoice = new Choice(); backgroundColorChoice.add("Black"); backgroundColorChoice.add("Blue"); backgroundColorChoice.add("Cyan"); backgroundColorChoice.add("Dark Gray"); backgroundColorChoice.add("Gray"); backgroundColorChoice.add("Green"); backgroundColorChoice.add("Light Gray"); backgroundColorChoice.add("Magenta"); backgroundColorChoice.add("Orange"); backgroundColorChoice.add("Pink"); backgroundColorChoice.add("Red"); backgroundColorChoice.add("White"); backgroundColorChoice.add("Yellow"); backgroundColorChoice.setBackground(Color.white); gridbag.setConstraints(backgroundColorChoice,c); backgroundColorChoice.addItemListener(this); panel.add(backgroundColorChoice); // Speed Label c.gridx = 4; c.gridy = 5; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; speedLabel = new Label("Speed: ms"); gridbag.setConstraints(speedLabel,c); panel.add(speedLabel); // Speed String c.gridx = 4; c.gridy = 6; c.gridwidth = 5; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.NONE; c.weightx = 1.0; c.weighty = 0.0; speedTextfield = new TextField("100"); gridbag.setConstraints(speedTextfield,c); //speedTextfield.setEditable(false); speedTextfield.addTextListener(this); panel.add(speedTextfield); //Start Button c.gridx = 4; c.gridy = 6; c.gridwidth = 4; c.gridheight = 1; c.anchor = GridBagConstraints.CENTER; c.fill = GridBagConstraints.NONE;//HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; start = new Button("Start"); start.addActionListener(this); gridbag.setConstraints(start,c); panel.add(start); // Stop Button c.gridx = 5; c.gridy = 6; c.gridwidth = 4; c.gridheight = 1; c.anchor = GridBagConstraints.CENTER; c.fill = GridBagConstraints.NONE;//HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; stop = new Button("Stop"); stop.addActionListener(this); gridbag.setConstraints(stop,c); panel.add(stop); // Output file Label c.gridx = 1; c.gridy = 7; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; outputLabel = new Label("Output file:"); gridbag.setConstraints(outputLabel,c); panel.add(outputLabel); //Output file Textfield c.gridx = 1; c.gridy = 8; c.gridwidth = 4; c.gridheight = 1; c.anchor = GridBagConstraints.SOUTHWEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; outputTextfield = new TextField("Gifin.gif"); gridbag.setConstraints(outputTextfield,c); panel.add(outputTextfield); //Save Button c.gridx = 5; c.gridy = 8; c.gridwidth = 1; c.gridheight = 1; c.anchor = GridBagConstraints.WEST; c.fill = GridBagConstraints.NONE; c.weightx = 1.0; c.weighty = 0.0; save = new Button("Save..."); save.addActionListener(this); gridbag.setConstraints(save,c); panel.add(save); //Canvas for RainbowText canvas = new RainbowText(); panel2 = new Panel(); panel2.add(canvas); System.out.println("Pass RainbowText Constructor"); add(panel,"North"); add(panel2,"South"); canvas.start(); } public void killRainbowTextThread() { this.canvas.stop(); } public void actionPerformed(ActionEvent buttonClick) { if (buttonClick.getSource() == start) { canvas.start(); System.out.print("Rainbow Canvas start\n"); } if (buttonClick.getSource() == stop) { canvas.stop(); System.out.print("Rainbow Canvas stop\n"); } if (buttonClick.getSource() == save) { try { Frame frameDialog = new Frame(); dialog = new FileDialog(frameDialog,"Save As",FileDialog.SAVE); dialog.setFile("*.gif"); dialog.show(); String filename = dialog.getDirectory() + dialog.getFile(); //System.out.println("Filename result =" + filename); if ((dialog.getFile()) != null ) { outputTextfield.setText(filename); canvas.setFilename(filename); canvas.setFlagGenerate(true,true); } else {} //System.out.println("Get save button event"); }catch (Exception e) { System.out.println("IOException thrown:\n" + e); } } else {} }//End of actonPerformed public void itemStateChanged(ItemEvent event) { if((fontChoice == event.getItemSelectable()) || (fontStyleChoice == event.getItemSelectable())|| (fontSizeChoice == event.getItemSelectable())) { canvas.setFont(fontChoice.getSelectedItem(),fontStyleChoice.getSelectedIndex(),Integer.parseInt(fontSizeChoice.getSelectedItem())); //System.out.println("Get fontChoice Event"); System.out.println("Font Value =" + Integer.parseInt(fontSizeChoice.getSelectedItem()) ); } else if (backgroundColorChoice == event.getItemSelectable()) { canvas.setBackgroundColor(backgroundColorChoice.getSelectedIndex()); //System.out.println("Color Index =" + backgroundColorChoice.getSelectedIndex()); } else {} }//End of itemStateChanged public void textValueChanged(TextEvent event) { if(inputTextfield == event.getSource()) { TextComponent tc = (TextComponent)event.getSource(); String str = tc.getText(); canvas.setStringInput(str); //System.out.println("Get stringTextfield event"); } else if (speedTextfield == event.getSource()) { try { TextComponent tc = (TextComponent)event.getSource(); String str = tc.getText(); if ( str == null) canvas.setSleepTime(100); else canvas.setSleepTime(Integer.parseInt(str)); System.out.println("Change speed to " + Integer.parseInt(str) + " ms"); } catch (Exception e) { } } else {} }//End of textValueChanged }//End of Class class RainbowText extends Canvas implements Runnable { Thread runner = null; Font f; FontMetrics fm; String textStr = null; String fontName; int fontStyle; int fontSize; int sleepTime; int strlen; int charOffsets[]; int yOffset; int phase = 0; char theChars[]; Color bgColor; Color colors[]; String fileName = "Gifin.gif"; boolean blockFlag = false; boolean fileFlag = false; int imageCount = 1; Image offScreen = null; Graphics offScreenG; OutputStream output; boolean stopped = false; public RainbowText() { bgColor = Color.black; setBackground(bgColor); textStr = "Hello... Gifin ^_^"; fontName = "TimesRoman"; fontStyle = Font.PLAIN; fontSize = 36; sleepTime = 100; f=new Font(fontName,fontStyle,fontSize); fm=getFontMetrics(f); setSize(40+fm.stringWidth(textStr),10+fm.getHeight()); }//End of Constructor public void update(Graphics g) { float h; int xPos=20; int x, y; phase--; if (phase < 0) phase=strlen-1; //System.out.println("Width =" + this.getSize().width + "Height =" + this.getSize().height); f=new Font(fontName,fontStyle,fontSize); fm=getFontMetrics(f); setSize(40+fm.stringWidth(textStr),10+fm.getHeight()); yOffset = fm.getAscent()+5; strlen = textStr.length(); theChars = new char [strlen]; charOffsets = new int [strlen]; textStr.getChars(0,strlen,theChars,0); colors = new Color[strlen]; for (int i = 0; i < strlen; i++) { h = ((float)i)/((float)strlen); colors[i] = new Color(Color.HSBtoRGB(h,1.0f,1.0f)); charOffsets[i] = xPos; xPos+=fm.charWidth(theChars[i]); } offScreen = createImage(this.getSize().width,this.getSize().height); offScreenG = offScreen.getGraphics(); offScreenG.setColor(bgColor); offScreenG.fillRect(0,0,this.getSize().width,this.getSize().height); offScreenG.setFont(f); for(int i=0;i