简体   繁体   English

Graphics2D drawString函数未绘制

[英]Graphics2D drawString funcion is not drawing

I'm designing a custom component and all the colors and shapes are drawn perfectly but the strings are not. 我正在设计一个自定义组件,所有颜色和形状都绘制得很完美,但字符串却没有。

I tried changing the font to Arial (The one I'm using is from a custom file), the color and trying to draw it with java.awt.Graphics instead of Graphics2D but noting worked 我尝试将字体更改为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

}

}

Also, the getStringWidth and getHeight methods returns 0, I tried different strings and the same result happened. 另外,getStringWidth和getHeight方法返回0,我尝试了不同的字符串,并且发生了相同的结果。

This should work, note the comments : 这应该工作,请注意以下注释:

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM