gpt4 book ai didi

java - 将透明文本字段放在图像上

转载 作者:行者123 更新时间:2023-12-01 14:18:46 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,允许我将文本放在图像上,并保存编辑后的图像。现在我收到一条错误消息:

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

当我运行代码时,它显示文本框和没有我的图像的白色背景。任何对此的帮助将不胜感激。现在我只专注于获取图像上的文本字段。先感谢您!代码如下:

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

最佳答案

通常可以在 LabelRenderTest 中看到更好的方法。

如果需要多行文本,您只需在标签中使用 HTML 格式。对单行消息使用纯文本。

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

关于java - 将透明文本字段放在图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17845264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com