Graphics2D drawString函数未绘制

时间:2019-02-08 04:39:55

标签: java swing graphics2d

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

我尝试将字体更改为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方法返回0,我尝试了不同的字符串,并且发生了相同的结果。

1 个答案:

答案 0 :(得分: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);
}