0

I would like to get user defined text on image, like if I'll make two text fields, one is for name and second for date so when I input someone's name and date, after inputting if I clicked OK then it'll display in that image.

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();
    }                 
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
dhvl5
  • 66
  • 9
  • 1) `panel1.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless .. – Andrew Thompson Feb 07 '17 at 22:12
  • .. it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Feb 07 '17 at 22:12

1 Answers1

2

Here is sample:

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);
}
}

And here is usage

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

Or you can us libs for that. For example: http://www.gif4j.com

Shamil
  • 95
  • 2
  • 7
  • i don't want watermark... first of all read question correctly and than reply – dhvl5 Feb 08 '17 at 11:29
  • So,, remove the alp component from the color & the line `System.out.println("The tex watermark is added to the image.");` & the job is done! Don't expect to be spoon fed exact solutions here, this is not a help desk (AKA 'batteries not included'). BTW - I was waiting to see if you had the sense to reply to my comments, but you're apparently ignoring me, so I'll close this tab in my browser now. – Andrew Thompson Feb 09 '17 at 07:39