【问题标题】:Java - Convert character or string into a shape / areaJava - 将字符或字符串转换为形状/区域
【发布时间】:2012-05-24 21:02:46
【问题描述】:

我希望能够将任何字符或字符串转换为形状或区域,以便我可以以任何我喜欢的大小、样式、效果等来绘制该字符。

更具体地说,我将使用视差来绘制它,以便仅在某个角度清楚地定义它(这就是为什么我不能使用 html 或任何类似性质的东西)。我已经设置了视差,我只需要形状。

简而言之,public Shape convert(char c){...}

或者,如果您能想到另一种实现此目的的方法,请告诉我。


只是给你一个粗略的想法:

【问题讨论】:

    标签: java string graphics shape graphics2d


    【解决方案1】:

    根据previous answer,您似乎可以按如下方式进行管理(假设此方法是在JPanel 的子类上定义的):

    public Shape convert(char c) {
        Font f = getFont();
        // Optionally change font characteristics here
        // f = f.deriveFont(Font.BOLD, 70);
    
        FontRenderContext frc = getFontMetrics(f).getFontRenderContext();
        GlyphVector v = f.createGlyphVector(frc, new char[] { c });
        return v.getOutline();
    }
    

    将方法声明为采用 String 参数可能更容易,因为您可以将 String 作为第二个参数传递给 createGlyphVector() - 另外,如果您需要做更多操作,这可能比单独转换每个字符更有效不止一个。

    【讨论】:

      猜你喜欢
      • 2011-01-28
      • 2015-05-15
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2013-01-29
      • 2013-03-02
      • 2011-01-08
      相关资源
      最近更新 更多