import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Enumeration;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.*;
public class TextPaneImage implements ActionListener {
JFileChooser fileChooser = new JFileChooser("images");
JTextPane textPane;
double scale = 0.5;
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if(ac.equals("OPEN"))
openDialog();
if(ac.equals("REMOVE"))
removeIcon();
if(ac.equals("RESIZE")) {
ImageIcon icon = removeIcon();
if(icon != null)
insertIcon(scale(icon));
}
}
private void openDialog() {
if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if(isSupportedImage(file)) {
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch(IOException e) {
System.out.println("Read error for " + file.getName() +
": " + e.getMessage());
}
//System.out.println("fileName = " + file.getName());
ImageIcon icon = new ImageIcon(image, file.getName());
//JOptionPane.showMessageDialog(textPane, icon, "", -1);
insertIcon(icon);
}
textPane.requestFocusInWindow();
}
}
private boolean isSupportedImage(File file) {
String ext = getExtension(file);
return ImageIO.getImageReadersBySuffix(ext).hasNext();
}
private String getExtension(File file) {
String s = file.getPath();
int dot = s.lastIndexOf(".");
return s.substring(dot+1);
}
private void insertIcon(ImageIcon icon) {
StyledDocument doc = textPane.getStyledDocument();
String name = "icon:" + icon.getDescription();
Style style = doc.addStyle(name, null);
StyleConstants.setIcon(style, icon);
try {
doc.insertString(doc.getLength(), " ", style);
} catch(BadLocationException e) {
System.out.printf("bad location error: %s%n", e.getMessage());
}
}
private ImageIcon removeIcon() {
int pos = textPane.getCaretPosition();
DefaultStyledDocument doc =
(DefaultStyledDocument)textPane.getStyledDocument();
Element charElement = doc.getCharacterElement(pos);
//System.out.printf("charElement(%d):%n", pos);
//showAttributes(charElement.getAttributes());
//System.out.println("---------------");
Icon icon = getIcon(charElement.getAttributes());
if(icon == null)
return null;
String description = ((ImageIcon)icon).getDescription();
//System.out.printf("icon description = %s%n", description);
int start = charElement.getStartOffset();
int end = charElement.getEndOffset();
try {
doc.remove(start, end-start);
} catch(BadLocationException e) {
System.out.println("bad location for start = " + start +
" end = " + end + ": " + e.getMessage());
}
doc.removeStyle("icon:" + description);
return (ImageIcon)icon;
}
private void showAttributes(AttributeSet attrs) {
Enumeration e = attrs.getAttributeNames();
while(e.hasMoreElements()) {
Object name = e.nextElement();
Object value = attrs.getAttribute(name);
//System.out.printf("name: %s value: %s%n", name, value);
}
}
private Icon getIcon(AttributeSet attrs) {
Enumeration e = attrs.getAttributeNames();
while(e.hasMoreElements()) {
Object name = e.nextElement();
//System.out.println("name = " + name);
if(name.toString().equals("icon"))
return (Icon)attrs.getAttribute(name);
}
return null;
}
private ImageIcon scale(ImageIcon icon) {
int w = (int)(scale*icon.getIconWidth());
int h = (int)(scale*icon.getIconHeight());
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.drawImage(icon.getImage(), 0, 0, w, h, null);
g2.dispose();
return new ImageIcon(image);
}
private JScrollPane getTextComponent() {
textPane = new JTextPane();
return new JScrollPane(textPane);
}
private JPanel getUIPanel() {
String[] ids = { "open", "remove", "resize" };
JPanel panel = new JPanel();
for(int j = 0; j < ids.length; j++) {
JButton button = new JButton(ids[j]);
button.setActionCommand(ids[j].toUpperCase());
button.addActionListener(this);
panel.add(button);
}
return panel;
}
public static void main(String[] args) {
TextPaneImage test = new TextPaneImage();
JFrame f = new JFrame("Set caret to remove/resize");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getTextComponent());
f.getContentPane().add(test.getUIPanel(), "Last");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}