I am developing a chat application in java swing in which I would like to add smileys, can anyone help me with this?
Autoreplace smiles text with appropriate images in JEditorPane
To support the autoreplacing we need a
JEditorPane
withStyledEditorKit
(or extension class) to provide images in text. We just add aDocumentListener
to process text insert events. After inserting we check whether the changed text contains smiles string - the ":)". If it contains we replace the smile text with an appropriate image.The example provides only one smile support - the ":)" string but can be easy extended.
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.image.BufferedImage;
import java.awt.*;
public class AutoreplaceSmiles extends JEditorPane {
static ImageIcon SMILE_IMG=createImage();
public static void main(String[] args) {
JFrame frame = new JFrame("Autoreplace :) with Smiles images example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final AutoreplaceSmiles app = new AutoreplaceSmiles();
app.setEditorKit(new StyledEditorKit());
app.initListener();
JScrollPane scroll = new JScrollPane(app);
frame.getContentPane().add(scroll);
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public AutoreplaceSmiles() {
super();
}
private void initListener() {
getDocument().addDocumentListener(new DocumentListener(){
public void insertUpdate(DocumentEvent event) {
final DocumentEvent e=event;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (e.getDocument() instanceof StyledDocument) {
try {
StyledDocument doc=(StyledDocument)e.getDocument();
int start= Utilities.getRowStart(AutoreplaceSmiles.this,Math.max(0,e.getOffset()-1));
int end=Utilities.getWordStart(AutoreplaceSmiles.this,e.getOffset()+e.getLength());
String text=doc.getText(start, end-start);
int i=text.indexOf(":)");
while(i>=0) {
final SimpleAttributeSet attrs=new SimpleAttributeSet(
doc.getCharacterElement(start+i).getAttributes());
if (StyleConstants.getIcon(attrs)==null) {
StyleConstants.setIcon(attrs, SMILE_IMG);
doc.remove(start+i, 2);
doc.insertString(start+i,":)", attrs);
}
i=text.indexOf(":)", i+2);
}
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
});
}
public void removeUpdate(DocumentEvent e) {
}
public void changedUpdate(DocumentEvent e) {
}
});
}
static ImageIcon createImage() {
BufferedImage res=new BufferedImage(17, 17, BufferedImage.TYPE_INT_ARGB);
Graphics g=res.getGraphics();
((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.yellow);
g.fillOval(0,0,16,16);
g.setColor(Color.black);
g.drawOval(0,0,16,16);
g.drawLine(4,5, 6,5);
g.drawLine(4,6, 6,6);
g.drawLine(11,5, 9,5);
g.drawLine(11,6, 9,6);
g.drawLine(4,10, 8,12);
g.drawLine(8,12, 12,10);
g.dispose();
return new ImageIcon(res);
}
}
Source: http://java-sl.com/tip_autoreplace_smiles.html
Here is some simple code that I found a long time ago on the web. I don't really like that is uses a caret listener. You should probably use a DocumentListener or a DocumentFilter. But it will give you an idea of how you can use a custom Icon to represent a smiley.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With