|
| 1 | +package math.plot; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Dimension; |
| 5 | +import java.awt.FlowLayout; |
| 6 | +import java.awt.Font; |
| 7 | +import java.awt.FontMetrics; |
| 8 | +import java.awt.Graphics; |
| 9 | +import java.awt.Graphics2D; |
| 10 | +import java.awt.RenderingHints; |
| 11 | + |
| 12 | +import javax.script.ScriptEngine; |
| 13 | +import javax.script.ScriptEngineManager; |
| 14 | +import javax.script.ScriptException; |
| 15 | +import javax.swing.BorderFactory; |
| 16 | +import javax.swing.BoxLayout; |
| 17 | +import javax.swing.JButton; |
| 18 | +import javax.swing.JLabel; |
| 19 | +import javax.swing.JPanel; |
| 20 | +import javax.swing.JScrollPane; |
| 21 | +import javax.swing.JTextArea; |
| 22 | +import javax.swing.JTextField; |
| 23 | + |
| 24 | +public class ScriptConsole extends JPanel { |
| 25 | + private JTextArea txtIn; |
| 26 | +// private JTextArea txtOut; |
| 27 | + private JLabel lblOut; |
| 28 | + private JButton btnRun; |
| 29 | + private ScriptEngine engine; |
| 30 | + private final String defaultEngine = "rhino"; |
| 31 | + |
| 32 | + |
| 33 | + public ScriptConsole() { |
| 34 | + initEngine(defaultEngine); |
| 35 | + |
| 36 | + txtIn = new JTextArea(4, 40); |
| 37 | +// txtOut = new JTextArea(4, 40); |
| 38 | + lblOut = new JLabel() { |
| 39 | + private String input = ""; |
| 40 | + private String output = ""; |
| 41 | +// private String textOut; |
| 42 | + |
| 43 | + @Override |
| 44 | + public void paint(Graphics g) { |
| 45 | +// super.paint(g); |
| 46 | + Graphics2D g2 = (Graphics2D) g; |
| 47 | + RenderingHints rh = new RenderingHints( |
| 48 | + RenderingHints.KEY_TEXT_ANTIALIASING, |
| 49 | + RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
| 50 | + g2.addRenderingHints(rh); |
| 51 | + FontMetrics fm = g2.getFontMetrics(); |
| 52 | + int textH = fm.getHeight(); |
| 53 | + |
| 54 | + g2.setColor(Color.BLACK); |
| 55 | + g2.fillRect(0, 0, 1000, 200); |
| 56 | + if (!input.equals("")) { |
| 57 | + g2.setColor(Color.WHITE); |
| 58 | + g2.drawString(">> " + input, 20, 20); |
| 59 | + g2.setColor(Color.GREEN); |
| 60 | + g2.drawString(output, 20, 25 + textH); |
| 61 | + g2.setColor(Color.WHITE); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void setText(String input) { |
| 67 | + this.input = input; |
| 68 | + try { |
| 69 | + this.output = engine.eval(input).toString(); |
| 70 | + } catch (ScriptException e) { |
| 71 | + this.output = "Error!"; |
| 72 | + } |
| 73 | + this.repaint(); |
| 74 | + } |
| 75 | + }; |
| 76 | + btnRun = new JButton("Run"); |
| 77 | + btnRun.addActionListener( |
| 78 | + e -> { |
| 79 | +// lblOut.setText("<html><span style='color:green'> >> " + input + "</span><br>" + obj.toString() + "</html>"); |
| 80 | + lblOut.setText(txtIn.getText()); |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | +// txtIn.setHorizontalAlignment(JTextField.CENTER); |
| 85 | + lblOut.setPreferredSize(new Dimension(1000, 60)); |
| 86 | +// lblOut.setFont(lblOut.getFont().deriveFont(18.0f)); |
| 87 | +// lblOut.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1)); |
| 88 | + |
| 89 | + this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); |
| 90 | + JPanel pnlInput = new JPanel(); |
| 91 | + pnlInput.setLayout(new FlowLayout(FlowLayout.CENTER)); |
| 92 | + JScrollPane sIn = new JScrollPane( |
| 93 | + txtIn, |
| 94 | + JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, |
| 95 | + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 96 | + pnlInput.add(sIn); |
| 97 | + pnlInput.add(btnRun); |
| 98 | + JPanel pnlOutput = new JPanel(); |
| 99 | + pnlOutput.setLayout(new FlowLayout(FlowLayout.CENTER)); |
| 100 | + pnlOutput.add(lblOut); |
| 101 | + this.add(pnlInput); |
| 102 | + this.add(pnlOutput); |
| 103 | + } |
| 104 | + |
| 105 | + public void initEngine(String engineName) { |
| 106 | + ScriptEngineManager m = new ScriptEngineManager(); |
| 107 | + engine = m.getEngineByName(engineName); |
| 108 | + } |
| 109 | +} |
0 commit comments