簡體   English   中英

圖形庫中的 drawString 性能不佳

[英]Poor performance with drawString from Graphics library

我正在制作一個 2D 紙牌游戲。 在其中,我有一個自定義字體,使用 Graphics 庫中的 drawString 以自定義顏色和大小繪制它。 問題是這似乎非常未經優化。 我似乎無法找出原因。 屏幕上有 5 張卡片,每張卡片使用這種方法 4 次,我的速度從 3,800 fps 下降到 350 fps。 這是我繪制文本的方式:

public static void drawString(Graphics g, String text, int xPos, int yPos, boolean center, Color c, Font font) {
    g.setColor(c);
    g.setFont(font);
    int x = xPos;
    int y = yPos;

    if (center) {
        FontMetrics fm = g.getFontMetrics(font);
        x = xPos - fm.stringWidth(text) / 2;
        y = (yPos - fm.getHeight() / 2) + fm.getAscent();
    }
    g.drawString(text, x, y);
} 

經過一番谷歌搜索后,這就是我的做法。 此圖像是在將卡片 UI 元素添加到屏幕時創建的。 然后用於僅覆蓋卡的圖像。 x 和 y 縮放在單獨的 class 中完成。 原始評論中提到的代碼在 Text class 中,以及其他 drawRestrictedText 方法:

公共 BufferedImage createTextOverlayImage(){

    BufferedImage overlay = new BufferedImage(imageWidth, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = overlay.createGraphics();

    Text.drawString(g, name,
            (imageWidth / 2) + wd.getScaleX(18),
            wd.getScaleY(21),
            true,
            Color.black, Assets.font20Bold);

    Text.drawString(g, cost + "",
            wd.getScaleX(28),
            wd.getScaleY(28),
            true,
            Color.blue, Assets.font28Bold);

    Text.drawRestrictedString(g, keyWords,
            wd.getScaleX(20),
            wd.getScaleY(162),
            imageWidth,
            Color.magenta,
            Assets.font18Bold);

    Text.drawRestrictedString(g, description,
             wd.getScaleX(20),
             wd.getScaleY(185),
            imageWidth - wd.getScaleX(35),
            Color.black,
            Assets.font18);

    return overlay;
} 

這是我用作參考的網站: http://www.java2s.com/Tutorials/Java/Graphics_How_to/Text/Write_text_onto_image.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM