Jump to content

How do I format correctly?


Raunoo321

Recommended Posts

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 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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