2011-03-16 35 views
10

Estoy desarrollando una aplicación de chat en java swing en la que me gustaría agregar caritas, ¿alguien puede ayudarme con esto?Cómo agregar emoticonos en java swing?

+0

Tendrá que ser más específico que eso ... a menos que esté satisfecho con soluciones como 'System.out.print (":-)");'. –

+0

okie, smileys colectivos como en los mensajeros de gtalk o yahoo? – harishtps

Respuesta

6
+0

Lo siento StanislavL, no recuerdo de dónde copié ese código. Agregué la referencia del sitio web a mi copia de la fuente. – camickr

+0

Gracias. Creo que tu código es ligeramente diferente. Se establece otra imagen y otro disparador. Sería mejor crear la imagen una vez y pasar la referencia en lugar de volver a crearla. Pero definitivamente en ambos sentidos funciona :) – StanislavL

3

puede copiar estos caracteres Unicode y utilizarlos: ☺ ☻

En las cadenas de Java, estos serán "\u263a" y "\u263b".

+0

¿No hay jar's separados para eso? – harishtps

+0

@harishtps: No JAR; solo una fuente con el glifo requerido. Aquí hay un [ejemplo] (http://sites.google.com/site/drjohnbmatthews/buttons). – trashgod

7

Aquí hay un código simple que encontré hace mucho tiempo en la web. Realmente no me gusta que se use un oyente caret. Probablemente deberías usar un DocumentListener o un DocumentFilter. Pero le dará una idea de cómo puede usar un icono personalizado para representar un emoticón.

import java.awt.*; 
import java.awt.image.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import javax.swing.text.*; 

public class Smiley 
    extends JFrame { 
    //autoreplacing :) with picture 
    JTextPane p = new JTextPane(); 
    public Smiley() throws Exception { 
     p.setEditorKit(new StyledEditorKit()); 
     getContentPane().add(p, BorderLayout.CENTER); 
     SimpleAttributeSet attrs = new SimpleAttributeSet(); 
     StyleConstants.setIcon(attrs, getImage()); 
     p.addCaretListener(new CaretListener() { 
      public void caretUpdate(CaretEvent e) { 
       SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         try { 
          StyledDocument doc = (StyledDocument) p.getDocument(); 
          String text = doc.getText(0, p.getDocument().getLength()); 
          int index = text.indexOf(":)"); 
          int start = 0; 
          while (index > -1) { 
           Element el = doc.getCharacterElement(index); 
           if (StyleConstants.getIcon(el.getAttributes()) == null) { 
            doc.remove(index, 2); 
            SimpleAttributeSet attrs = new SimpleAttributeSet(); 
            StyleConstants.setIcon(attrs, getImage()); 
            doc.insertString(index, ":)", attrs); 
           } 
           start = index + 2; 
           index = text.indexOf(":)", start); 
          } 
         } 
         catch (Exception ex) { 
          ex.printStackTrace(); 
         } 
        } 
       }); 
      } 
     }); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setSize(400, 400); 
    } 

    public static void main(String[] args) throws Exception { 
     Smiley test11 = new Smiley(); 
     test11.show(); 
    } 

    protected ImageIcon getImage() { 
     BufferedImage bi = new BufferedImage(15, 15, BufferedImage.TYPE_INT_ARGB); 
     Graphics g = bi.getGraphics(); 
     g.setColor(Color.red); 
     g.drawOval(0, 0, 14, 14); 
     g.drawLine(4, 9, 9, 9); 
     g.drawOval(4, 4, 1, 1); 
     g.drawOval(10, 4, 1, 1); 
     return new ImageIcon(bi); 
    } 
} 
+0

Debe aceptar la respuesta de StanislavL ya que él es el autor original del código. – camickr

-1
import java.awt.*; 
public class SmileyFace { 

    public static void main(String[] args){ 


     Frame f = new Frame("Smile Face"); 
     f.setSize(500, 500); 
     f.setVisible(true); 

     Graphics g; 
     g = f.getGraphics(); 


     while (true) 
     { 
      g.setColor(Color.black); 
      g.drawOval(100, 100, 100, 100); 
      g.setColor(Color.blue); 
      g.fillOval(120, 130, 20, 20); 
      g.fillOval(160, 130, 20, 20); 
      g.setColor(Color.blue); 
      g.setColor(Color.red); 
      g.drawLine(130, 170, 135, 175); 
      g.drawLine(135, 175, 163, 175); 
      g.drawLine(163, 175, 168, 170); 
      g.setColor(Color.green); 
      g.drawString("Hello", 210, 190); 



     } 


    } 
} 
+0

lindo ............ –