0

I'm trying to write a program that will allow me to put text over an image, and save the edited image. Right now I'm getting an error that says:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

When I run the code it shows the text box, and a white background without my image. Any help with this would be appreciated. Right now i'm just focused on getting the text field over the image. Thank you in advance! Here's the code:

import java.awt.*; 
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.TreeSet;

public class Try1 extends JFrame {

    public Try1() {
        initializeUI();
    }        
    BufferedImage img;

    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null); 
    }

    public void LoadImage() {
        try {
            img = ImageIO.read(new File("savedimage.jpg"));
        }
    catch (IOException e){}
    }

    private void initializeUI() {
        JPanel panel = new JPanel(null);
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField textField = new JTextField(20);
        textField.setBounds(50, 50, 100, 20);
        panel.add(textField);
        setContentPane(panel);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Try1().setVisible(true);
            }
        });
        JFrame f = new JFrame("Load Image Sample");
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.add(new Try1());
        f.pack();
        f.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

3 Answers3

1

A better approach in general can be seen in LabelRenderTest.

You only need to use HTML formatting in the label if multi-line text is required. Use plain text for a single line message.

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

public class LabelRenderTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {

            String title = "<html><body style='width: 200px; padding: 5px;'>"
                + "<h1>Do U C Me?</h1>"
                + "Here is a long string that will wrap.  "
                + "The effect we want is a multi-line label.";

                JFrame f = new JFrame("Label Render Test");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                BufferedImage image = new BufferedImage(
                    400,
                    300,
                    BufferedImage.TYPE_INT_RGB);
                Graphics2D imageGraphics = image.createGraphics();
                GradientPaint gp = new GradientPaint(
                    20f,
                    20f,
                    Color.red,
                    380f,
                    280f,
                    Color.orange);
                imageGraphics.setPaint(gp);
                imageGraphics.fillRect(0, 0, 400, 300);

                JLabel textLabel = new JLabel(title);
                textLabel.setSize(textLabel.getPreferredSize());

                Dimension d = textLabel.getPreferredSize();
                BufferedImage bi = new BufferedImage(
                    d.width,
                    d.height,
                    BufferedImage.TYPE_INT_ARGB);
                Graphics g = bi.createGraphics();
                g.setColor(new Color(255, 255, 255, 128));
                g.fillRoundRect(
                    0,
                    0,
                    bi.getWidth(f),
                    bi.getHeight(f),
                    15,
                    10);
                g.setColor(Color.black);
                textLabel.paint(g);
                Graphics g2 = image.getGraphics();
                g2.drawImage(bi, 20, 20, f);

                ImageIcon ii = new ImageIcon(image);
                JLabel imageLabel = new JLabel(ii);

                f.getContentPane().add(imageLabel);
                f.pack();
                f.setLocationByPlatform(true);

                f.setVisible(true);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Following JFrame is useless. Because Try1 is itself a JFrame.

JFrame f = new JFrame("Load Image Sample");

basically just use Try1 instead of other Jframe.

f = new Try1();
f.pack();
f.setVisible(true);

But more importantly, you should not override paint, overide paintComponent instead. See Difference between paint() and paintcomponent()?.

Community
  • 1
  • 1
Deniz
  • 1,575
  • 1
  • 16
  • 27
  • Thank you for your help thus far, but I would like clarification please. I tried to set 'f' = new Try1(); but is giving me "error: cannot find symbol." JFrame f = new JFrame("Load Image Sample"); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); That is everything I deleted. This is what is there right now: f = new ImageTest(); f.pack(); f.setVisible(true); If I missed something, please let me know. Thank you again. – Doug Wilson Jul 25 '13 at 00:10
0

This is your problem

  JFrame f = new JFrame("Load Image Sample");


f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
  1. This is not a window. It is a JFrame, so it is unwise to put a WindowAdapter on it
  2. Your class is extending a JFrame, so take out the JFrame, and just do your

     new Try1();
     f.pack();
     f.setVisible(true);
    
user2277872
  • 2,963
  • 1
  • 21
  • 22