Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Console-like web applet [closed]

Hey, I've been developing an application in the windows console with Java, and want to put it online in all of its console-graphics-glory.

Is there a simple web applet API I can use to port my app over?

I'm just using basic System.out and System.in functionality, but I'm happy to rebuild my I/O wrappers.

I think something along these lines would be a great asset to any beginning Java developers who want to put their work online.

like image 851
Dean Rather Avatar asked Sep 26 '08 07:09

Dean Rather


2 Answers

Sure, just make into an applet, put a small swing UI on it with a JFrame with two components - one for writing output to, and one for entering inputs from. Embed the applet in the page.

like image 178
Lars Westergren Avatar answered Oct 15 '22 09:10

Lars Westergren


I did as Lars suggested and wrote my own.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.Font;

public class Applet extends JFrame {
    static final long serialVersionUID = 1;

    /** Text area for console output. */
    protected JTextArea textArea;

    /** Text box for user input. */
    protected JTextField textBox;

    /** "GO" button, in case they don't know to hit enter. */
    protected JButton goButton;

    protected PrintStream printStream;
    protected BufferedReader bufferedReader;

    /**
     * This function is called when they hit ENTER or click GO.
     */
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            goButton.setEnabled(false);
            SwingUtilities.invokeLater(
                new Thread() {
                    public void run() {
                        String userInput = textBox.getText();
                        printStream.println("> "+userInput);
                        Input.inString = userInput;
                        textBox.setText("");
                        goButton.setEnabled(true);
                    }
                }   
            );
        }
    };

    public void println(final String string) {
        SwingUtilities.invokeLater(
            new Thread() {
                public void run() {
                    printStream.println(string);
                }
            }   
        );
    }

    public void printmsg(final String string) {
        SwingUtilities.invokeLater(
            new Thread() {
                public void run() {
                    printStream.print(string);
                }
            }   
        );
    }

    public Applet() throws IOException {
        super("My Applet Title");

        Container contentPane = getContentPane();

        textArea = new JTextArea(30, 60);
        JScrollPane jScrollPane = new JScrollPane(textArea);
        final JScrollBar jScrollBar = jScrollPane.getVerticalScrollBar();
        contentPane.add(BorderLayout.NORTH, jScrollPane);
        textArea.setFocusable(false);
        textArea.setAutoscrolls(true);
        textArea.setFont(new Font("Comic Sans MS", Font.TRUETYPE_FONT, 14));

        // TODO This might be overkill
        new Thread() {
            public void run() {
                while(true) {
                    jScrollBar.setValue(jScrollBar.getMaximum());
                    try{
                        Thread.sleep(100);
                    } catch (Exception e) {}
                }
            }
        }.start();

        JPanel panel;
        contentPane.add(BorderLayout.CENTER, panel = new JPanel());

        panel.add(textBox = new JTextField(55));
        textBox.addActionListener(actionListener);

        panel.add(goButton = new JButton("GO"));
        goButton.addActionListener(actionListener);

        pack();

        // End of GUI stuff

        PipedInputStream inputStream;
        PipedOutputStream outputStream;

        inputStream = new PipedInputStream();
        outputStream = new PipedOutputStream(inputStream);

        bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "ISO8859_1"));
        printStream = new PrintStream(outputStream);

        new Thread() {
            public void run() {
                try {
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        textArea.append(line+"\n");
                    }
                } catch (IOException ioException) {
                    textArea.append("ERROR");
                }
            }
        }.start();
    }
}

This below code was in a separate class, "Input", which has a static "inString" string.

    public static String getString() {
        inString = "";

        // Wait for input
        while (inString == "") {
            try{
                Thread.sleep(100);
            } catch (Exception e) {}
        }

        return inString;
    }

Through-out the lifespan of the project I will probably maintain this code some more, but at this point - it works :)

like image 43
Dean Rather Avatar answered Oct 15 '22 09:10

Dean Rather