Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Converting message with .mp3 string attached to an Icon

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting message with .mp3 string attached to an Icon

    I need to add a fragment of code to the code below so that when a user sends a message with the ".mp3"string attached to it, the string should be convert into an icon . Any ideas ?

    * GUIChatFrame.java
    *
    package guiclient;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.net.URL;
    import javax.swing.*;
    import javax.swing.text.*;
     
    /**
     
     */
    public class GUIChatFrame extends JFrame implements ActionListener
    {
       private ChatClient client;
       private ChatRoom room;
       /**
        * Creates a new instance of GUIChatFrame
        */
       public GUIChatFrame(ChatRoom room, ChatClient client)
       {
          this.room = room;
          this.client = client;
          createWindow();
       }
     
       // elements of the GUI
       private JTextPane chatRoomText;   // text pane to display all chatter's messages
       private JTextArea intext;         // text are for adding the current chatter's message
       private JScrollPane chatRoom;     // the chat display is scrollable
       private JButton quitButton;       // leave the chat room
       private JButton sendButton;        // send the message
     
       private void createWindow() throws HeadlessException
       {
          final int CLIENT_WINDOW_WIDTH = 400;
          final int CLIENT_WINDOW_HEIGHT = 450;
          //JFrame frame = new JFrame("M362 Chat Server Demo");
          //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          chatRoomText = new JTextPane();
          chatRoomText.setSize(400, 400);
          JScrollPane chatRoom =
                new JScrollPane(chatRoomText,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
          chatRoomText.setEditable(false);
          intext = new JTextArea(6,30);
          intext.setEditable(true);
     
          createStyles();
          JScrollPane inputPane = new JScrollPane(intext,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          sendButton = new JButton("Send");
          sendButton.addActionListener(this);
     
          quitButton = new JButton("Quit");
          quitButton.addActionListener(this);
     
          JPanel buttonPanel = new JPanel();
          buttonPanel.setLayout(new GridLayout(0, 1));
          buttonPanel.add(sendButton);
          buttonPanel.add(quitButton);
     
          JPanel inputArea = new JPanel();
          inputArea.setLayout(new BorderLayout());
          inputArea.add(inputPane, BorderLayout.CENTER);
          inputArea.add(buttonPanel, BorderLayout.EAST);
          Container content = getContentPane();
          content.add(chatRoom, BorderLayout.CENTER);
          content.add(inputArea, BorderLayout.SOUTH);
          setSize(CLIENT_WINDOW_WIDTH, CLIENT_WINDOW_HEIGHT);
     
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
     
       // button event handling
       public void actionPerformed(ActionEvent ae)
       {
          Object buttonClicked = ae.getSource();
          if(buttonClicked.equals(sendButton))
          {
             JTextArea i = getInputArea();
             room.send(i.getText());
     
             i.setText("");
          }
          else if(buttonClicked.equals(quitButton))
          {
             room.send("BYE");
             System.exit(0);
          }
       }
     
       public JTextPane getChatRoom()
       {
          return chatRoomText;
       }
     
       public JTextArea getInputArea()
       {
          return intext;
       }
     
       DefaultStyledDocument doc;
     
       // predefined styles
       StyleContext styles = new StyleContext();
       Style smile, normal, boldstyle;
       Style sad;
       void createStyles()
       {
          smile = styles.addStyle("smile", null);
     
          URL url = this.getClass().getClassLoader().getResource("guiclient/smile.jpg");
          StyleConstants.setIcon(smile, new ImageIcon(url));
     
          //additional style
    sad = styles.addStyle("sad", null);
    url = this.getClass().getClassLoader().getResource("guiclient/sad.jpg");
    StyleConstants.setIcon(sad, new ImageIcon(url));
     
          normal = styles.addStyle("normal", null);
          boldstyle = styles.addStyle("bold", null);
          StyleConstants.setBold(boldstyle, true);
          doc = new DefaultStyledDocument();
       }
     
      void setOutputText(String message)
    {
       try
       {
          doc.insertString(doc.getLength(), "\n", normal);
     
          // emphasize the chatter's name
     
          if (message.contains(":"))
          {
             String chatter = message.substring(0, message.indexOf(":"));
             doc.insertString(doc.getLength(), chatter, boldstyle);
             message = message.substring(message.indexOf(":"));
          }
     
          // convert smiley and sad face symbols into icons
          String sm = ":-)";
          String sd = ":-(";
     
          //temporary variables to gradually chop up the input message
          String pre1 = "";
          String pre2 = "";
     
          while ( message.contains(sm) || message.contains(sd))
          {
             if (message.contains(sm))
             {
                pre1 = message.substring(0, message.indexOf(sm));
             }
             if (message.contains(sd))
             {
                pre2 = message.substring(0, message.indexOf(sd));
             }
     
             if (pre2=="" || // there are no sad faces in this message OR
                 (pre2 != "" && pre1 != "" &&
                 pre1.length() < pre2.length()) ) // found a smiley before a sad face
             {
                message = message.substring(message.indexOf(sm) + sm.length());
                doc.insertString(doc.getLength(), pre1, normal);
                doc.insertString(doc.getLength(), sm, smile);
                System.out.println(pre1 + sm);
             }
             else if (pre1=="" || // there are no smileys in this message
                 (pre1 !="" && pre2 != "" &&
                 pre2.length() < pre1.length())) // there is a sad face before a smiley
                 {
                    message = message.substring(message.indexOf(sd) + sd.length());
                    doc.insertString(doc.getLength(), pre2, normal);
                    doc.insertString(doc.getLength(), sd, sad);
                    System.out.println(pre2 + sd);
                 }
     
            pre1="";
            pre2="";
         } // end while
     
          // print the remaining message
          if(message.length()>0)
          {
             doc.insertString(doc.getLength(), message, normal);
          }
     
          getChatRoom().setDocument(doc);
       }
       catch (Exception ex)
       {
          ex.printStackTrace();
       }
    }
    }
    Last edited by JavaPF; June 19th, 2009 at 03:01 AM. Reason: Please use [code] [/code] tags


  2. #2
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    Hey, after long hours of thinking i finally got the code right !!!

    Thanks to you all though

  3. #3
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: Help Please

    Quote Originally Posted by YannStacy View Post
    Hey, after long hours of thinking i finally got the code right !!!

    Thanks to you all though
    You can share it though if you want.