Aj, mój błąd. jeszcze raz ten sam poprawnie wklejony kod.
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();
}
}
}