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

时间:2014-07-29 15:15:23

标签: java swing javafx-2 bufferedimage javafx-8

要将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图片吗?

1 个答案:

答案 0 :(得分:0)

如果没有尝试,我会说你只是指定BufferedImage的高度,宽度和类型,但不要像使用该块的AWT代码那样设置内容:

        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之后创建的。