Java - 在图像的中心绘制文本

Ghz*_*Ncl 5 java graphics swing graphics2d imageicon

我需要在图像的中心写文字.要写的文字并不总是一样的.

我正在使用的代码在这里:

// Here I first draw the image
g.drawImage(img, 22, 15, 280, 225, null);
// I get the text 
String text = photoText.getText();
// Set the text color to black
g.setColor(Color.black);
// I draw the string 
g.drawString(text, 79.5F, 220.0F);
Run Code Online (Sandbox Code Playgroud)

问题是文本不在图像的中心,我该怎么办?

我只需要在水平中心绘制文字.

tra*_*god 7

使用JLabel较少的工作,但是FontMetrics,显示在这里,将让您直接管理的几何形状.


Hov*_*els 5

一种可能的解决方案:在JPanel中绘制图像,确保将面板的preferredsize设置为图像的大小,让JPanel使用GridBagLayout,并将文本放在添加到JPanel的JLabel中,而不使用GridBagConstraints.这是将JLabel置于JPanel中心的一种方法.


cam*_*ckr 5

简单的方法是使用带有图标和文本的JLabel.然后将水平/垂直文本位置设置为CENTER,文本将绘制在图像的中心.

从您的代码看起来您​​正试图在图像底部附近绘制文本.在这种情况下,您可以使用带有Icon作为容器的JLabel.然后,您可以将布局设置为类似BoxLayout的内容,并添加另一个带有文本的标签.

两种方法都不需要自定义绘画.

import java.awt.*;
import javax.swing.*;

public class LabelImageText extends JPanel
{
    public LabelImageText()
    {
        JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
        label1.setText( "Easy Way" );
        label1.setHorizontalTextPosition(JLabel.CENTER);
        label1.setVerticalTextPosition(JLabel.CENTER);
        add( label1 );

        JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
        label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
        add( label2 );

        JLabel text = new JLabel( "More Control" );
        text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        label2.add( Box.createVerticalGlue() );
        label2.add( text );
        label2.add( Box.createVerticalStrut(10) );
    }


    public static class ColorIcon implements Icon
    {
        private Color color;
        private int width;
        private int height;

        public ColorIcon(Color color, int width, int height)
        {
            this.color = color;
            this.width = width;
            this.height = height;
        }

        public int getIconWidth()
        {
            return width;
        }

        public int getIconHeight()
        {
            return height;
        }

        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            g.setColor(color);
            g.fillRect(x, y, width, height);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("LabelImageText");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new LabelImageText() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我的意思是我必须在图像的中心写一个文本,然后保存图像

您可以使用" 屏幕图像"创建任何组件的图像.这假设您在GUI上显示图像和文本.

或者,如果您正在谈论只是在图像中添加文本然后保存图像,那么您将需要创建一个BufferedImage并在其上绘制图像,然后在其上绘制文本.您将需要使用Trashgod提到的FontMetrics类.我的建议无济于事.