Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Color of text in Java

I am trying to create a separate CustomFont class in which I can different attributes of text. So I created a new class extended Font and inside created a private class Drawing which extends JComponent. I change the color and other characteristics of font and text inside paintComponent method.

The problem is paintComponent method is not getting called. I am sure I am making some mistake.

Here is the code:

import javax.swing.JComponent;

public class CustomFont extends Font {
    private String string;
    private int FontStyle;

    public CustomFont(String text, int style) {
        super("Serif", style, 15);
        FontStyle = style;
        string = text;  

        Drawing draw = new Drawing();
        draw.repaint();
    }

    private class Drawing extends JComponent {
        public void paintComponent(Graphics g) {
            Font font = new Font("Serif", Font.BOLD, 15);
            g.setFont(font);
            g.setColor(Color.YELLOW);
            g.drawString(string, getX(), getY());
        }
    }
}
like image 424
Alfred Avatar asked Oct 05 '22 11:10

Alfred


2 Answers

Adding to my comment:

1) You dont honor the paint chain by calling super.XXX implementation of paintComponent(..) method which you should and it should be the first call in the overridden method or else anomalies could occur:

@Override 
protected void paintComponent(Graphics g) {
      super.paintComponent(g);

      Font font = new Font("Serif", Font.BOLD, 15);
      g.setFont(font);
      g.setColor(Color.YELLOW);
      g.drawString(string, 0, 0);
}

on the above code notice the @Override annotation so I am sure I am overriding the correct method. And also getX() and getY() has been replaced with 0,0, As getX and getY referred to the components position however when we call drawString we supply it with paramteres of where to draw within the container (and it must be within the bounds/size f container of course.

2) You should override getPreferredSize when drawing to graphics object and return Dimensions which fit your components drawings/contents or else visually there wont be anything visible as components size will be 0,0:

private class Drawing extends JComponent {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);//you would infact caluclate text size using FontMetrics#getStringWidth(String s)
    }
}

And just as a suggestion use some RenderHints and Graphics2D for nice looking text :) See here for more:

  • Displaying Antialiased Text by Using Rendering Hints
like image 105
David Kroukamp Avatar answered Oct 13 '22 04:10

David Kroukamp


There's a lot in this CustomFont class that doesn't really make any sense. For one, the lack of anything to actually do with a Font, for another the fact that you don't really use anything coded in it. The following would seem to be more along the lines of what you're looking for.

public class Drawing extends JComponent {
    String text;
    Font myFont;
    Color myTextColor;

    public Drawing(String textArg, Font f, Color textColor) {
        myFont = f;
        myTextColor = textColor;
        text = textArg;
    }

    public void paintComponent(Graphics g) {
        g.setFont(myFont);
        g.setColor(myTextColor);
        g.drawString(text, 0, getHeight() / 2);
    }
}

To be used as shown below...

public class Test {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Font font = new Font("Serif", Font.BOLD, 15);
        String text = "blah blah blah";
        Color textColor = Color.YELLOW;
        Drawing d = new Drawing(text, font, textColor);

        frame.add(d);
        frame.setSize(100,100);
        frame.setVisible(true);
    }
}

Drawing makes use of a Font, a Font does not make use of a Drawing. It doesn't really make sense to have Drawing defined inside a Font.

like image 1
Mike Avatar answered Oct 13 '22 06:10

Mike