Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the colour of text in drawstring()

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?

like image 918
Chris Headleand Avatar asked Dec 10 '12 13:12

Chris Headleand


People also ask

What is DrawString () method?

DrawString(String, Font, Brush, Single, Single, StringFormat) Draws the specified text string at the specified location with the specified Brush and Font objects using the formatting attributes of the specified StringFormat.

What is DrawString Java?

drawString(String str, int x, int y) Draws the text given by the specified string, using this graphics context's current font and color. void.


2 Answers

  • 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();
            }
        });
    }
}
like image 156
David Kroukamp Avatar answered Oct 07 '22 01:10

David Kroukamp


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.

like image 34
Andrew Thompson Avatar answered Oct 07 '22 00:10

Andrew Thompson