将图像分配给字符串

Posted

技术标签:

【中文标题】将图像分配给字符串【英文标题】:Assigning a image to a String 【发布时间】:2013-05-19 16:59:52 【问题描述】:

有什么方法可以将特定图像分配给字符串,以便输出是这样的(我使用带有 JLabel 的字符串):

【问题讨论】:

使用什么技术? Swing/Graphics2D? 【参考方案1】:

基本过程是创建文本的轮廓并渲染它......简单;)

这里使用TextLayout类生成一个Shape的文字,可以填充和绘制。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TextOutline 

    public static void main(String[] args) 
        new TextOutline();
    

    public TextOutline() 
        EventQueue.invokeLater(new Runnable() 
            @Override
            public void run() 
                try 
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                 catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 
                

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FontPaint());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            
        );
    

    class FontPaint extends JPanel 

        public FontPaint() 
            setFont(getFont().deriveFont(Font.BOLD, 48f));
        

        @Override
        public Dimension getPreferredSize() 
            return new Dimension(300, 200);
        

        @Override
        public void paintComponent(Graphics g) 
            super.paintComponent(g);

            int w = getWidth();
            int h = getHeight();

            Graphics2D g2d = (Graphics2D) g;
            FontRenderContext fontRendContext = g2d.getFontRenderContext();

            // Not required, but it will make it look nice
            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

            String st = "Hello World.";

            // Create an layout of the text
            TextLayout text = new TextLayout(st, getFont(), fontRendContext);
            // Generate a shape of the layout
            Shape shape = text.getOutline(null);
            // Align the shape to the center
            Rectangle rect = shape.getBounds();
            AffineTransform affineTransform = new AffineTransform();
            affineTransform = g2d.getTransform();
            affineTransform.translate(w / 2 - (rect.width / 2), h / 2
                    + (rect.height / 2));
            g2d.transform(affineTransform);

            // Fill in blue
            g2d.setColor(Color.BLUE);
            g2d.fill(shape);
            // Outline in red
            g2d.setColor(Color.red);
            g2d.draw(shape);
            g2d.dispose();
        
    

【讨论】:

+1 另请参阅this answer 以使用GlyphVector @AndrewThompson 啊,GlyphVector!这就是我一直在寻找的

以上是关于将图像分配给字符串的主要内容,如果未能解决你的问题,请参考以下文章

Sencha Touch:显示数据库中的图像

如何将图像存储在 SQLite 数据库中

为啥我可以将字符分配给字符串对象而不是字符串对象的向量?

如何将字节数组转换为blob

为啥我不能将 string::back 分配给字符串?

如果一个为空白,如何将变量分配给两个字符串之一?