1

Is there a way I can replace a string with an image/sprite in JavaSwing? For example, for the string "r", I want to replace it with the image of the trainer from Pokemon Silver.

I'm doing this because the "r" represents a roomba and the GUI will display a map of characters mapped by the roomba. I simply want to replace the "r" with a sprite to make the GUI be more than just characters. Right now I am able to get the ImageIcon to display on the JTextPane, I just can't choose the location on there. If I'm able to choose the location on JTextPane and have it overlap where the "r" is, as the "r" remains stationary, that's also another solution.

Is this even possible or should I just live with the "r" being the roomba instead of the image?

The following is the code right now. I'm using Intellij and it's UI designer. One of my partners is currently working on the code to get it to print out the grid, but we've decided that "r" is going to be the roomba, so if I'm able to get the icon to replace the "r", I'm good to go.

public class ObstacleMap extends JFrame{
    private JComboBox controllerselect;
    private JPanel panel;
    private JButton button;
    private JLabel silvergif;
    private JLabel battle;
    private JTextPane textPane1;
    int manual = 1;

    public ObstacleMap() {
        setTitle("Control Center");
        setContentPane(panel);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        textPane1.setVisible(false);
        battle.setVisible(false);
        textPane1.setEditable (false); //Make text area uneditable

        //System.out.println now prints to GUI text area
        PrintStream ps = new PrintStream(
            new OutputStream() {
                public void write(int c){
                    textPane1.setText(textPane1.getText() + (char) c);
                }
            }
        );

        System.setOut(ps);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textPane1.setVisible(true);
                silvergif.setVisible(false);
                textPane1.insertIcon(new ImageIcon("src/trainer.gif"));
            }
        });

        controllerselect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (controllerselect.getSelectedItem() == "Autonomous"){
                    manual = 0;
                }
                else if (controllerselect.getSelectedItem() == "PS4 Controller"){
                    manual = 1;
                }
            }
        });
    }

    public static void main(String[] args){
        ObstacleMap o = new ObstacleMap();
    }
}

Here is a screenshot of the GUI:
Here is a screenshot of the GUI.

The icon should be placed in the bottom center area of the JTextPane, not in the top left corner.

Neuron
  • 5,141
  • 5
  • 38
  • 59
akick31
  • 11
  • 2
  • 2
    This is a pretty abstract description. All I can gather is that you’re using a JTextPane to position a character, and you want an icon in its place. I can tell you how to do that replacement, but I have no idea how you’re using a JTextPane to position your text/icon, so I don’t know if a simple Document replacement will work. Edit your question and show an example of how your code positions text/icons. – VGR Apr 18 '18 at 20:54
  • @VGR, my apologies. I included the code and a screenshot of the GUI. – akick31 Apr 18 '18 at 21:11
  • Do you want every "r" replaced with the icon, or do you want a single icon statically positioned at the bottom center? If the latter, what will happen when the appended characters fill up the visible part of the JTextPane? Will they simply overlap with the icon? – VGR Apr 18 '18 at 21:42
  • *"The icon should be placed in the bottom center area of the JTextPane"* - Not sure `JTextPane` can achieve this, `JEditorPane` should be able to, if you using a html based solution – MadProgrammer Apr 18 '18 at 21:49
  • You can insert images to a `JTextPane` using `inertIcon` or `insertComponent`, [for example](https://stackoverflow.com/questions/28269060/display-selected-image-in-a-jtextpane/28269263#28269263) or [example](https://stackoverflow.com/questions/22188986/java-swing-how-do-i-create-a-jtextpane-with-multiple-icons-on-the-same-line/22189237#22189237) - I still question the reasoning behind your choice though – MadProgrammer Apr 18 '18 at 21:51
  • I would also, **strongly** encourage you to have a look at [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MadProgrammer Apr 18 '18 at 21:53
  • *"The following is the code right now. I'm using Intellij and it's UI designer. One of my partners is currently working on the code to get it to print out the grid, but we've decided that "r" is going to be the roomba, so if I'm able to get the icon to replace the "r", I'm good to go"* - Okay, stop, you're approaching the problem all wrong. DON'T use `JTextPane` for this (or any other text component), instead, start with with a `GridBagLaout` or `GridLayout` and simply add a series of `JLabel`s to your container, this will act as your basic grid – MadProgrammer Apr 18 '18 at 21:54
  • From there, simply replace the `icon` property in each `JLabel` with the required image to represent you model (you may need a blank image of the same size of "blank" elements) – MadProgrammer Apr 18 '18 at 21:55
  • @VGR Either of those is fine, as the "r" should only occur with that one character. The other characters, when they fill up the JTextPane, should not overlap with the icon. – akick31 Apr 18 '18 at 22:29
  • @MadProgrammer the issue with that, is the way they're writing the code is that it scans the surrounding area and picks up objects and subsequently places them on the grid in the correct place. Is there a way to do this with the GridLayout and not the JTextPane? Basically my idea was to take the output that they have (it outputs to a console) and place it on the GUI and then replace the "r" with the icon. I see what you mean however, and I'll give it a shot. – akick31 Apr 18 '18 at 22:32
  • @akick31 Yes. You need to think of the "grid" as a virtual view of the world. The "grid" is just a visual representation of your model. Put simply, it's much easer to simply call `label[x][y].setIcon(...)` then trying to update a `Document` on the fly – MadProgrammer Apr 18 '18 at 22:33

0 Answers0