質問

I have a question in using drawString(). I'm creating a game like zType and I have this problem with drawString(). How can I change the font color of a character in a drawString() while it's moving?

役に立ちましたか?

解決

Now that you've made it clear that you want to draw only some characters of a string in a particular color (which is an important detail that should be mentioned in your question), you can, as others have mentioned, calculate the font metrics yourself for those characters.

But there can be more to drawing a single contiguous string than just FontMetrics, such as kerning and even connecting letters in certain scripts and fonts. Instead, I would use an AttributedCharacterIterator.

The easiest way to get an AttributedCharacterIterator is to create an AttributedString:

AttributedString a = new AttributedString(text);

// We want first two characters drawn in red.
a.addAttribute(TextAttribute.FOREGROUND, Color.RED, 0, 2);

graphics.drawString(a.getIterator(), x, y);

他のヒント

Call Graphics.setColor() as shown below,

public void paint (Graphics g) {
   g.setColor(Color.RED);
   g.drawString("Hello World!!", 50, 100);
}

If you want only the first character to be in different color, then you should be doing something like this,

public void paint(Graphics g) {
   Color prev = g.getColor();
   g.setColor(Color.RED);
   g.drawString("H", 50, 100);
   FontMetrics metrics = g.getFontMetrics();
   int width = metrics.stringWidth("H");
   g.setColor(prev);
   g.drawString("ello World!!", 50 + width, 100);
}

You need to use FontMetrics to get the width of the H and add that width to the x point of ello

    Font font = new Font("impact", Font.PLAIN, 50);
    FontMetrics fm = g.getFontMetrics(font);
    int widthH = fm.stringWidth("H");
    g.setFont(font);

    g.setColor(Color.BLUE);
    g.drawString("H", 100, 100);

    g.setColor(Color.RED);
    g.drawString("ello", 100 + widthH, 100);

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestHColor extends JPanel {


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Font font = new Font("impact", Font.PLAIN, 50);
        FontMetrics fm = g.getFontMetrics(font);
        int widthH = fm.stringWidth("H");
        g.setFont(font);

        g.setColor(Color.BLUE);
        g.drawString("H", 100, 100);

        g.setColor(Color.RED);
        g.drawString("ello", 100 + widthH, 100);
    }

    @Override 
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JFrame frame = new JFrame("Hello String");
                frame.add(new TestHColor());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top