2

Okay, so here is the problem.

g.setColor(Color.WHITE);
g.drawString("all your base belong to us",x,y);

The follow code makes it so that the string displayed is white and fully white.

My aim is to make a certain section of the string, say for example, I want the word "base" in that string to be a different color, yellow in this case.

The code that I would most likely use would be:

g.drawString("all your #ffd700base belong to us",x,y);

That code attempts to set the text to be yellow from 'base' all the way to the end of the sentence.

Though the output of that is:

http://i.stack.imgur.com/lB2WC.png

Ignore the background, just look at the string. The "#ffd700" becomes a part of the string which is then displayed.

This doesn't work, I cannot find a solution that does.

Alexey Malev
  • 6,095
  • 4
  • 30
  • 50
  • I think you can't do it in 1 line. From the javadoc `g.drawString` `Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.` so you need to set a color, write 1 piece of text, set the color `#ffd700`, draw another piece of text and so on... – Totò May 11 '14 at 09:41
  • Aww, that sucks because then I have to create a new Graphics object, set the x and y to be a little bit more than the previous message. Anyways, I'll be on the look out if there are any more solutions. – user3625168 May 11 '14 at 09:48

1 Answers1

0

Same problem is solved here. Please have a look at below posts:

Sample code after some changes in code mentioned at above link:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.CellRendererPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PaintComponentTest extends JPanel {

    private static final String s = "<html>all your <font color=\"#ffd700\">base</font> belong to us</html>";
    private JLabel renderer = new JLabel(s);
    private CellRendererPane crp = new CellRendererPane();
    private Dimension dim;

    public PaintComponentTest() {
        this.setBackground(Color.lightGray);
        dim = renderer.getPreferredSize();
        this.add(crp);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        renderer.setForeground(Color.WHITE);
        crp.paintComponent(g, renderer, this, 10, 10, dim.width, dim.height);
    }

    private void display() {
        JFrame f = new JFrame("PaintComponentTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(this);
        f.pack();
        f.setSize(200, 70);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new PaintComponentTest().display();
            }
        });
    }
}

screenshot:

enter image description here

Community
  • 1
  • 1
Braj
  • 44,339
  • 5
  • 51
  • 69
  • 1
    This doesn't work, even when I cast Graphics to graphics2d and create an object called g2 and then use html, it uses the html. – user3625168 May 11 '14 at 09:55