Change text color in drawstring ()

I am trying to add emphasis to one job in drawing a line im 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 no luck ... it just outputs HTML

Can someone point me in the right direction?

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

  • You have added to Graphics2D but are not using it (does not apply to the 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 color, just call setColor(Color c) on the Graphic object:

 g2.setColor(Color.GREEN); 

However, this will cause the entire line to be drawn in green, if you want only the parts to be green, use JLabel to support HTML (before 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, the notification is in its own line, because of the paragraph tag, rather use the font tag to get it on one line:

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(); } }); } } 
+11
source

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

+3
source

Use JLabel for stylized text. See LabelRenderTest for how to draw and use it in paint.

BUWfe.png

Using Graphics / AWT Methods

The line means NOTICE should be green, and the rest should be the default (black). We will need to call drawString(String) twice with the colors of the two parts of the string, compensating for the last line with the width of the first. Look for things like FontMetrics or GlyphVector to get width. This answer uses the GlyphVector to get the outline of the letters.

yvnRi.png

+3
source

If you just create a simple label with a focus on the word, you can simply assign the HTML directly to 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> snippet at the beginning of the line for JLabel , it will use the HTML renderer to draw it.

However, as @AndrewThompson noted, <p> will make the colored text jump to a new line, perhaps <span> would be more appropriate ...

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

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

+1
source

You can add g.setColor (Color.Chosen Color) and then write the line with g.drawString () maybe

-2
source

All Articles