Graphics2D drawString 函数未绘制

Graphics2D drawString funcion is not drawing

我正在设计一个自定义组件,所有颜色和形状都绘制得很完美,但字符串不是。

我尝试将字体更改为 Arial(我正在使用的字体来自自定义文件)、颜色并尝试使用 java.awt.Graphics 而不是 Graphics2D 进行绘制,但注意到效果很好

package lore.calculator;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JComponent;

public class Screen extends JComponent implements ComponentListener {
private static final long serialVersionUID = 1L;

private String value;
private String header;
private Font textFont;

public Screen() {

    try {
        InputStream file = Screen.class.getResourceAsStream("\fonts\digital-7.ttf");
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

        textFont = Font.createFont(Font.TRUETYPE_FONT, file);
        ge.registerFont(textFont);
    } catch (IOException | FontFormatException ex) {
        ex.printStackTrace();
    }

    value = new String("0");
    header = new String();


}

public void setValue(String value) {
    this.value = value;
    repaint();
}

public void setHeader(String header) {
    this.header = header;
    repaint();
}

public String getValue() {
    return (value);
}

public String getHeader() {
    return (header);
}

public void paint(Graphics gr) {
    Graphics2D g = (Graphics2D) gr;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Dibujar en fondo

    Stroke borderStroke = new BasicStroke(2);
    Color backgroundColor = new Color(142, 165, 143);

    g.setColor(backgroundColor);
    g.fillRect(0, 0, getWidth(), getHeight());

    g.setStroke(borderStroke);
    g.setColor(Color.BLACK);
    g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);

    // Dibujar el valor principal

    Font valueFont = new Font("Digital-7", 14, Font.PLAIN);
    FontMetrics valueMetrics = g.getFontMetrics(valueFont);

    int valueWidth = valueMetrics.stringWidth(value);
    int valueHeight = valueMetrics.getHeight();

    int valueX = getWidth() - valueWidth - 8;
    int valueY = getHeight() - valueHeight - 8;

    g.setFont(valueFont);
    g.setColor(Color.black);
    g.drawString(value, valueX, valueY);
}

@Override
public void componentHidden(ComponentEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void componentMoved(ComponentEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void componentResized(ComponentEvent arg0) {
    repaint();
}

@Override
public void componentShown(ComponentEvent arg0) {
    // TODO Auto-generated method stub

}

}

此外,getStringWidth 和 getHeight 方法 returns 0,我尝试了不同的字符串,结果相同。

这应该可以,请注意注释:

@Override
public void paintComponent(Graphics gr) {//override paint component, not paint  

    super.paintComponent(gr);//always call super.paintComponent
    Graphics2D g = (Graphics2D) gr;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Dibujar en fondo

    Stroke borderStroke = new BasicStroke(2);
    Color backgroundColor = new Color(142, 165, 143);

    g.setColor(backgroundColor);
    g.fillRect(0, 0, getWidth(), getHeight());

    g.setStroke(borderStroke);
    g.setColor(Color.BLACK);
    g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);

    // Dibujar el valor principal
    //Font valueFont = new Font("Digital-7", 14, Font.PLAIN);  //wrong 
    Font valueFont = new Font("Digital-7", Font.PLAIN, 14);
    FontMetrics valueMetrics = g.getFontMetrics(valueFont);

    int valueWidth = valueMetrics.stringWidth(value);
    int valueHeight = valueMetrics.getHeight();

    int valueX = getWidth() - valueWidth - 8;
    int valueY = getHeight() - valueHeight - 8;

    g.setFont(valueFont);
    g.setColor(Color.black);
    g.drawString(value, valueX, valueY);
}