Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在图像上放置用户定义的文本?_Java_Image_Swing_Text_Graphics - Fatal编程技术网

Java 如何在图像上放置用户定义的文本?

Java 如何在图像上放置用户定义的文本?,java,image,swing,text,graphics,Java,Image,Swing,Text,Graphics,我想在图像上获得用户定义的文本,比如我将创建两个文本字段,一个用于名称,第二个用于日期,所以当我输入某人的姓名和日期时,在输入后,如果我单击OK,它将显示在该图像中 import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import

我想在图像上获得用户定义的文本,比如我将创建两个文本字段,一个用于名称,第二个用于日期,所以当我输入某人的姓名和日期时,在输入后,如果我单击OK,它将显示在该图像中

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class billFrame extends JFrame 
{
    public billFrame()
    {
        JFrame f1 = new JFrame("Billng Application");
        f1.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f1.setSize(500,500);
        f1.setBounds(30, 50, 500, 700);
        f1.setExtendedState(JFrame.MAXIMIZED_BOTH);

        ImageIcon icon = new 
        ImageIcon("C:\\Users\\Dhaval\\Downloads\\shrihari.png");
        Image image = icon.getImage();
        JPanel panel1; 
        panel1 = new JPanel() 
        {
            @Override
            protected void paintComponent(Graphics g) 
            {
                super.paintComponent(g);
                g.drawImage(image, 1400, 0, 500, 700, this);
            }

            @Override
            public Dimension getPreferredSize()
            {
                return new Dimension(320, 200);
            }
        };
        f1.add(panel1);
        panel1.setVisible(true);
        panel1.setLayout(null);

        JLabel name = new JLabel("Name :");
        name.setVisible(true);
        name.setLocation(100,100);
        name.setSize(100,100);
        panel1.add(name);

        JTextField namet = new JTextField();
        namet.setVisible(true);
        namet.setLocation(150, 137);
        namet.setSize(200,30);
        panel1.add(namet);
        f1.setVisible(true);
    }
    @SuppressWarnings("unchecked")                             
    public static void main(String args[]) 
    {
        billFrame bf = new billFrame();
    }                 
}
以下是示例:

static void addTextWatermark(String text, File sourceImageFile, File destImageFile) {
try {
    BufferedImage sourceImage = ImageIO.read(sourceImageFile);
    Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();

    // initializes necessary graphic properties
    AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
    g2d.setComposite(alphaChannel);
    g2d.setColor(Color.BLUE);
    g2d.setFont(new Font("Arial", Font.BOLD, 64));
    FontMetrics fontMetrics = g2d.getFontMetrics();
    Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);

    // calculates the coordinate where the String is painted
    int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
    int centerY = sourceImage.getHeight() / 2;

    // paints the textual watermark
    g2d.drawString(text, centerX, centerY);

    ImageIO.write(sourceImage, "png", destImageFile);
    g2d.dispose();

    System.out.println("The tex watermark is added to the image.");

} catch (IOException ex) {
    System.err.println(ex);
}
}
这里是用法

File sourceImageFile = new     File("name.png");
File destImageFile = new    File("anothername.png");
addTextWatermark("Text", sourceImageFile, destImageFile);
或者你也可以给我们免费的。例如:

以下是示例:

static void addTextWatermark(String text, File sourceImageFile, File destImageFile) {
try {
    BufferedImage sourceImage = ImageIO.read(sourceImageFile);
    Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();

    // initializes necessary graphic properties
    AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
    g2d.setComposite(alphaChannel);
    g2d.setColor(Color.BLUE);
    g2d.setFont(new Font("Arial", Font.BOLD, 64));
    FontMetrics fontMetrics = g2d.getFontMetrics();
    Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);

    // calculates the coordinate where the String is painted
    int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
    int centerY = sourceImage.getHeight() / 2;

    // paints the textual watermark
    g2d.drawString(text, centerX, centerY);

    ImageIO.write(sourceImage, "png", destImageFile);
    g2d.dispose();

    System.out.println("The tex watermark is added to the image.");

} catch (IOException ex) {
    System.err.println(ex);
}
}
这里是用法

File sourceImageFile = new     File("name.png");
File destImageFile = new    File("anothername.png");
addTextWatermark("Text", sourceImageFile, destImageFile);

或者你也可以给我们免费的。例如:

1)
panel1.setLayout(null)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 请学习常见的Java命名法(命名约定-例如,
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是一个
大写常量
),并一致使用它。3) 例如,获取图像的一种方法是热链接到中看到的图像。4) 应用程序资源在部署时将成为嵌入式资源,因此现在就开始访问它们是明智的。必须通过URL而不是文件访问。有关如何形成URL的信息,请参见。1)
panel1.setLayout(null)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 请学习常见的Java命名法(命名约定-例如,
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是一个
大写常量
),并一致使用它。3) 例如,获取图像的一种方法是热链接到中看到的图像。4) 应用程序资源在部署时将成为嵌入式资源,因此现在就开始访问它们是明智的。必须通过URL而不是文件访问。有关如何形成URL的信息,请参阅。我不想要水印。。。首先,正确阅读问题,然后回答,从颜色行
System.out.println中删除alp组件(“将tex水印添加到图像中”)&工作完成了!不要期望在这里得到精确的解决方案,这里不是服务台(也称为“不包括电池”)。顺便说一句,我一直在等待你是否有意识回复我的评论,但你显然忽视了我,所以我现在将关闭浏览器中的此选项卡。我不希望水印。。。首先,正确阅读问题,然后回答,从颜色行
System.out.println中删除alp组件(“将tex水印添加到图像中”)&工作完成了!不要期望在这里得到精确的解决方案,这里不是服务台(也称为“不包括电池”)。顺便说一句,我一直在等着看你是否有意识回复我的评论,但是你显然忽略了我,所以我现在就关闭浏览器中的这个标签。