gpt4 book ai didi

java - 使用 SwingFXUtils 将 BufferedImage(awt) 转换为 Image(JavaFx)

转载 作者:行者123 更新时间:2023-11-29 08:46:34 25 4
gpt4 key购买 nike

要将 BuffredImage 转换为 javaFX ImageView,我们使用 SwingFXUtils.toFXImage这是外部图书馆:http://forge.scilab.org/index.php/p/jlatexmath/downloads/694/

我试过了,但它对我不起作用,我有一个适用于 Swing 的代码:

import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXIcon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

public class LatexExample extends JFrame implements ActionListener {

private JTextArea latexSource;
private JButton btnRender;
private JPanel drawingArea;

public LatexExample() {
this.setTitle("JLatexMath Example");
this.setSize(500, 500);
Container content = this.getContentPane();
content.setLayout(new GridLayout(2, 1));
this.latexSource = new JTextArea();

JPanel editorArea = new JPanel();
editorArea.setLayout(new BorderLayout());
editorArea.add(new JScrollPane(this.latexSource),BorderLayout.CENTER);
editorArea.add(btnRender = new JButton("Render"),BorderLayout.SOUTH);

content.add(editorArea);
content.add(this.drawingArea = new JPanel());
this.btnRender.addActionListener(this);

this.latexSource.setText("x=\\frac{-b \\pm \\sqrt {b^2-4ac}}{2a}");
}

public void render() {
try {
// get the text
String latex = this.latexSource.getText();

// create a formula
TeXFormula formula = new TeXFormula(latex);

// render the formla to an icon of the same size as the formula.
TeXIcon icon = formula
.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);

// insert a border
icon.setInsets(new Insets(5, 5, 5, 5));

// now create an actual image of the rendered equation
BufferedImage image = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
JLabel jl = new JLabel();
jl.setForeground(new Color(0, 0, 0));
icon.paintIcon(jl, g2, 0, 0);
// at this point the image is created, you could also save it with ImageIO

// now draw it to the screen
Graphics g = this.drawingArea.getGraphics();
g.drawImage(image,0,0,null);
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
JOptionPane.INFORMATION_MESSAGE);
}

}

public static void main(String[] args) {
LatexExample frame = new LatexExample();
frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
if( e.getSource()==this.btnRender ) {
render();
}

}
}

我在 javaFX 中尝试了同样的方法,但它不起作用!

String Latex = "\\frac {5} {a}";


TeXFormula formula = new TeXFormula(Latex);
TeXIcon icon = formula
.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);

// insert a border
icon.setInsets(new Insets(5, 5, 5, 5));

// now create an actual image of the rendered equation
BufferedImage Buffimage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
WritableImage FXimage = new WritableImage(Buffimage.getWidth(), Buffimage.getHeight());
//imageview variable name is image.
image.setImage(SwingFXUtils.toFXImage(Buffimage,FXimage));

我必须修改什么才能将此图片转换为 javaFX 图片?

最佳答案

如果不尝试,我会说您只需指定 BufferedImage 的高度、宽度和类型,但不要像在 AWT 代码中那样使用该 block 设置内容:

        Graphics2D g2 = image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
JLabel jl = new JLabel();
jl.setForeground(new Color(0, 0, 0));
icon.paintIcon(jl, g2, 0, 0);
// at this point the image is created, you could also save it with ImageIO

// now draw it to the screen
Graphics g = this.drawingArea.getGraphics();
g.drawImage(image,0,0,null);

您有评论,说明图像是在 paintIcon 之后创建的。

关于java - 使用 SwingFXUtils 将 BufferedImage(awt) 转换为 Image(JavaFx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25018884/

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