gpt4 book ai didi

java - java中的文本转图像

转载 作者:行者123 更新时间:2023-12-02 03:38:30 27 4
gpt4 key购买 nike

我正在尝试将 .txt 文件中的一行文本转换为 java 中的 .jpeg 图像。我编写了以下代码:

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

public class ProjTry extends JFrame
{
static BufferedImage img;
static Graphics g;
static String s;

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img,0,0,this);
}

/* public*/ ProjTry(String fname) //throws Exception
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(fname));
//EDIT
while ((s=br.readLine()) != null)
{
System.out.println(s);
}

}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}

}

public static void main(String args[])throws Exception
{
ProjTry tx=new ProjTry("E:\\Java\\file.txt");
JFrame j=new JFrame("Demo");
g=img.getGraphics();
g.drawString(s,0,0);
try
{
FileOutputStream fos=new FileOutputStream("E:\\Java\\t2img.jpg");
ImageIO.writeImage(img,"jpg",fos);
fos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

这里我的文件名为“file.txt”。然而编译器不断显示 2 个错误。我该怎么办?

错误::

第 17 行:找不到符号

     super(.)paintComponent(g);

符号:方法paintComponent(Graphics)

第 79 行:找不到符号

     ImageIO(.)writeImage(img,"jpg",fos);

符号:方法writeImage(BufferedImage,String,FileOutputStream)

位置:ImageIO 类

最佳答案

错误消息准确地告诉您出了什么问题 - 相信它们并进行检查,您将能够自行解决这些问题和类似问题。

public class ProjTry extends JFrame
{
static BufferedImage img;
static Graphics g;
static String s;

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img,0,0,this);
}

The method paintComponent(Graphics) is undefined for the type JFrame

JFrame API 将确认这是真的,当您看到此错误和类似错误时,应该查找 API。看起来您已经复制了 JPanel 的绘制代码并将其放入 JFrame 中,并且您不应该这样做。在扩展 JPanel 的类内部进行绘制,就像在您借用该类的代码中所做的那样,然后将该 JPanel 放入 JFrame 中。

此外,始终在您的重写方法(您认为重写父类方法的方法)前面加上@Override注释。如果您这样做:

@Override
public void paintComponent(Graphics g) {
// etc...

编译器会警告您,您实际上并没有重写 super 方法。

站点注释:paintComponent 方法应该是 protected 而不是 public

<小时/>
ImageIO.writeImage(img,"jpg",fos);

The method writeImage(BufferedImage, String, FileOutputStream) is undefined for the type ImageIO

ImageIO API 将再次确认这是真的,并且事实上此类没有 writeImage 方法。请改用 ImageIO.write(...) 方法,*正如 API 将向您展示的那样。或者更一般地说:仅使用 API 规定可用的方法。

例如——没有写入图像文件:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class DrawOnImg extends JPanel {
public static final String IMG_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/thumb/c/c9/Thomas_Hicks_-_Leopold_Grozelier_-_"
+ "Presidential_Candidate_Abraham_Lincoln_1860.jpg/"
+ "456px-Thomas_Hicks_-_Leopold_Grozelier_-_Presidential_Candidate_"
+ "Abraham_Lincoln_1860.jpg";
public static final String TXT_PATH = "http://history.eserver.org/"
+ "gettysburg-address.txt";
public static final String NEW_LINE = System.getProperty("line.separator");
private BufferedImage backgroundImg = null;

public DrawOnImg(BufferedImage img, String text) {
backgroundImg = img;

JTextArea textArea = new JTextArea(text);
textArea.setOpaque(false);
setLayout(new GridBagLayout());
add(textArea);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(backgroundImg, 0, 0, this);
}
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || backgroundImg == null) {
return super.getPreferredSize();
}
int w = backgroundImg.getWidth();
int h = backgroundImg.getHeight();
return new Dimension(w, h);
}

private static void createAndShowGui(BufferedImage img, String text) {
DrawOnImg mainPanel = new DrawOnImg(img, text);

JFrame frame = new JFrame("DrawOnImg");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
Scanner scanner = null;
try {
URL txtUrl = new URL(TXT_PATH);
scanner = new Scanner(txtUrl.openStream());
final String text = readText(scanner);

URL imgUrl = new URL(IMG_PATH);
final BufferedImage img = ImageIO.read(imgUrl);
SwingUtilities.invokeLater(() -> createAndShowGui(img, text));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
} finally {
if (scanner != null) {
scanner.close();
}
}

}

private static String readText(Scanner scanner) {
StringBuilder sb = new StringBuilder();
boolean save = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.startsWith("Four score")) {
save = true;
}
if (line.startsWith("-----")) {
save = false;
}
if (save && !line.isEmpty()) {
sb.append(line);
sb.append(NEW_LINE);
}
}
return sb.toString();
}
}

哪些输出:

enter image description here

关于java - java中的文本转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37146444/

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