Saturday, January 30, 2016

Writing Text To An Image

Three components will be displayed on the screen: textfield, label, and button. The textfield holds text that will be added to an image. The label (with image icon) holds the image. When the button is clicked, the text in the textfield will be added to the image.

Notes: This program does not change the image file name, so please make a backup copy of your original image if you try it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.awt.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class TextToImage extends JFrame{
    private BufferedImage bufimage=null;
    private ImageIcon icon=null;
    private JTextField tfield=null;
    private JLabel label=null;
    
    public static void main(String[] args) throws Exception{
        TextToImage obj=new TextToImage();
    }
    
    public TextToImage(){
        super("Text To Image");
        
        setLayout(new BorderLayout());
        
        tfield=new JTextField(20);
        add(tfield,BorderLayout.NORTH);
        
        label=new JLabel();
        label.setIcon(getImage());
        add(label,BorderLayout.CENTER);
        
        JButton button=new JButton("Add Text");
        button.addActionListener(new ButtonHandler());
        add(button,BorderLayout.SOUTH);
        
        setSize(icon.getIconWidth(),icon.getIconHeight()+100);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            Graphics g=bufimage.getGraphics();
            g.setColor(Color.WHITE);
            g.setFont(new Font("Arial Black",Font.BOLD,20));
            g.drawString(tfield.getText(),200,200);
            g.dispose();
            try{
                ImageIO.write(bufimage,"jpg",new File("R2D2.jpg"));
            }
            catch(Exception ex){
            }
            
            label.setIcon(getImage());
        }
    }
    
    public ImageIcon getImage(){
        try{
            bufimage=ImageIO.read(new File("R2D2.jpg"));
        }catch(Exception e){
        }
        icon=new ImageIcon(bufimage);
        return icon;
    }
}


public ImageIcon getImage(){
     try{
          bufimage=ImageIO.read(new File("R2D2.jpg"));
     }catch(Exception e){
     }
     icon=new ImageIcon(bufimage);
     return icon;
}

This function returns an ImageIcon. It is called twice to display the original and the image that has been changed on the screen.

bufimage=ImageIO.read(new File("R2D2.jpg"));

The image is loaded via the ImageIO class. The image will then be manipulated using BufferedImage class.

Graphics g=bufimage.getGraphics();
g.setColor(Color.WHITE);
g.setFont(new Font("Arial Black",Font.BOLD,20));
g.drawString(tfield.getText(),200,200);
g.dispose();
try{
	ImageIO.write(bufimage,"jpg",new File("R2D2.jpg"));
}

This segment of code manipulates the original image. It is the core of the program.

Graphics g=bufimage.getGraphics();

The graphics context is obtained with the getGraphics() method. The next two lines of code set the color and font of the text.

g.drawString(tfield.getText(),200,200);

The value of the text field is retrieved and then added to the image at position x=200, y=200.

g.dispose();

System resources that are used by the graphics context are released.

No comments:

Post a Comment