有什么方法可以将特定的图像分配给String,以便输出是这样的(即将String与JLabel结合使用):

最佳答案

基本过程是创建文本轮廓并使其呈现...简单;)

这使用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();
        }
    }
}

09-27 14:55