Jump to content

How do I format correctly?


Recommended Posts

Posted

Hey!
Maybe for some its stupid question, but personally I after using java for so long, I've never had to use nor graphics so deep or used color codes that much.

So my question:
Situation is 

        g.setColor(Color.BLUE);

        g.drawString("blabla" + someStringVariable), 20, 50);

So how can I keep that blabla or whatever as blue and change variable color to I don't know, to yellow? Like how can I make it happen, or is there any chance that I can change colors in drawString with colorcodes?

Dunno, my english isn't perfect, but I'm giving my best :D 

 

Posted

If I understand your problem correctly you want to do something like blabla someStringVariable in one drawString call?

I don't think it's possible to do it in one drawString call, but this works, taken from https://stackoverflow.com/questions/23591034/how-to-use-multiple-colors-when-using-drawstring-in-java-awt-graphics
 

public class Test extends Script {

    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 = renderer.getPreferredSize();

    @Override
    public int onLoop() throws InterruptedException {
        return 10000;
    }
    @Override
    public void onPaint(Graphics2D g) {
        renderer.setForeground(Color.BLUE);
        crp.paintComponent(g, renderer, bot.getBotPanel(), 10, 10, dim.width, dim.height);
    }

}

 

  • Like 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...