Changing the colour of text in drawstring()

34,918

Solution 1

  • How does this compile: g.drawString("this is something I want people to <p color="#00FF00">NOTICE</p>", x, y); as ' " ' is a special character we must escape it with \

  • You cast to Graphics2D but dont use it (not relevant to problem but can cause anomalies).

It should be:

Graphics2D g2 = (Graphics2D) g;
g2.drawString("this is something I want people to <p color=\"#00FF00\">NOTICE</p>", x, y);

to add colour simply call setColor(Color c) on Graphics object:

g2.setColor(Color.GREEN);

However this will set the entire String to be drawn green, if you want only parts to be drawn green use JLabel for HTML support (up to HTML3.2):

JLabel label = new JLabel("<html>this is something I want people to <p color=\"#00FF00\">NOTICE</p></html>");

full example:

enter image description here

NB As you can see notice is on its own line thats because of the paragraph tag rather use font tag to get it on a single line like so:

enter image description here

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test {

    public Test() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("<html>this is something I want people to <p color=\"#00FF00\">NOTICE</p></html>");

        // JLabel label = new JLabel("<html>this is something I want people to <font color=\"#00FF00\">NOTICE</font></html>");//will be shown on single line

        frame.add(label);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
}

Solution 2

Use a JLabel for styled text. See LabelRenderTest for how it can be drawn to an image & used in paint.

Using Graphics/AWT methods

The string implies NOTICE should be green, but the rest default (black). We would need to call drawString(String) twice with the colors of the two parts of the string, offsetting the latter string by the width of the first. To get the width, see things like FontMetrics or a GlyphVector. This answer uses a GlyphVector to get an outline of the letters.

Solution 3

Use Graphics.setColor() to change the color of anything you do. Or use a JLabel to set the color on.

Solution 4

If you're just creating a simple label with emphasis on a word, you can just assign the HTML straight onto the JLabel like this...

JLabel label = new JLabel("<html>this is something I want people to <p color='#00FF00'>NOTICE</p>");

As long as you have the <html> piece at the beginning of the String for a JLabel, it will use the HTML renderer to paint it.

As pointed out by @AndrewThompson, however, the <p> will force the colored text on to a new line, so perhaps <span> would be more appropriate...

JLabel label = new JLabel("<html>this is something I want people to <span style='color:#00FF00;'>NOTICE</span>");

Solution 5

you can use g.setColor(Color.BLUE) before g.drawString(). (For example Color.BLUE)

Share:
34,918
Chris Headleand
Author by

Chris Headleand

Updated on February 04, 2020

Comments

  • Chris Headleand
    Chris Headleand almost 4 years

    I'm trying to add emphasis to one work in a string im drawing using swing.

    I was advised to use HTML with the following code:

    Graphics2D g2 = (Graphics2D) g;
    g.drawString("this is something I want people to <p color="#00FF00">NOTICE</p>", x, y);
    

    I tried this but had no luck... it just outputs the HTML

    Can anyone point me in the right direction?

  • Chris Headleand
    Chris Headleand almost 11 years
    Hi, thanks for this.. that looks a little over complicated to change the color of 1 word. Is this the only way to do this or are there any simpler options (I've only just started playing with swing)
  • Andrew Thompson
    Andrew Thompson almost 11 years
    The string implies NOTICE should be green, but the rest ..default. We would need to call drawString twice with the two colors, offsetting the latter string by the width of the first.
  • Chris Headleand
    Chris Headleand almost 11 years
    Hi Andrew, I'm moving from println() to GUI and finding the transition a little difficult. When I was asing is there was a simpler option I was wondering if there was something less complex as I have no idea whats going on in that massive list of code you sent me. Your "hire someone" comment was not only nonconstructive but also quite off putting for someone struggling to learn a new skill.
  • Andrew Thompson
    Andrew Thompson almost 11 years
    See the edit. I also recommend trying simpler things in GUIs. Custom painting (especially of strings) is an advanced topic.
  • Andrew Thompson
    Andrew Thompson almost 11 years
    Excellent edit. I only just noticed how odd that String is. It reads like a single sentence, yet the HTML makes the last word into a new paragraph.
  • umert
    umert almost 11 years
    you are right. As I know, you have only just solution for problem.
  • Andrew Thompson
    Andrew Thompson almost 11 years
    @DavidKroukamp It can be done without using a styled text component (see the 'AWT edit') but it would be a lot of stuffing around. I prefer to leverage the rendering ability of Swing to draw styled text.
  • wattostudios
    wattostudios almost 11 years
    It certainly is interesting @AndrewThompson, something that a human can understand simply, can be interpreted very differently by a computer that relies on specific syntax.
  • David Kroukamp
    David Kroukamp almost 11 years
    @AndrewThompson see my upadted answer for why NOTICE is on its own line
  • Andrew Thompson
    Andrew Thompson almost 11 years
    I just upgraded 'good edit' to 'excellent edit'. Using a span there makes more sense.
  • Andrew Thompson
    Andrew Thompson almost 11 years
    Good call on the escaped characters as well.
  • kleopatra
    kleopatra almost 10 years
    nothing new to at least one earlier answer, is there :-)
  • Kuldeep Vasani
    Kuldeep Vasani over 8 years
    it inverts the color of original image and add text into black. is any other setup needs to be done to use this ?