Theodor
Theodor

Reputation: 5656

Visualize Java program using JavaScript

I have a program allowing script files to "compete" with each other in a games such as Tic Tac Toe .

The visualization is made in the console and looks something like this:

|  XO |
| OX O|
|  X  |
- X is the winner! Meh..

Not very exciting.

I was thinking of making a Swing visualisation, but after seeing a presentation on the Raphaël JavaScript vector graphics library I decided that I'd be cool to make the visualization browser based, even though every thing will be running on the same computer.

Do I need a full fledged web server to accommodate for this? How does the JavaScript communicate with the Java program?

Upvotes: 0

Views: 371

Answers (5)

Sam Barnum
Sam Barnum

Reputation: 10724

Javascript can communicate with a Java applet on the page (at least in theory). A Java applet is going to throw up a warning when it loads, and requires that Java be installed on the machine.

Upvotes: 1

CuNimb
CuNimb

Reputation: 164

Building a Swing interface would be the most straightforward solution, but this would probably be the messiest one as well.

The web browser solution is probably the most satisfying if you already have a web server going, but has a lot of overhead to set up and properly understand. Then again, you have layers of different technology to play with and get confused in (java, JSP, HTML, javascript, css, etc).

These days, with HTML5 and available javascript libraries, the web interface is in my opinion the best choice for most interfaces, so you might as well set up your machine and have it available for the next project.

Any communication between the server (Java) and the browser will take place with a HTTP request from the browser.

This may happen in two ways:

a) By pressing a submit button on a browser HTML page and rendering a HTML page on the server as a response (usually through server side scripting like JSP, although you could generate the entire page through java code)

b) By using Ajax in javascript to make an asynchronous call to the server, which will respond with data that you can then interpret and render with javascript (probably the best solution for what you are trying to do). There are many ready javascript libraries to help you with this, including jQuery.

In method b, you would essentially be waiting on the server side for a post from the browser and then you would be responding with a page written in XML, json or even pure text. Your javascript code would then interpret the data and render it on the browser HTML page (which you will have loaded at the starting point for the application).

Upvotes: 1

Brad
Brad

Reputation: 15879

If you're forced to run Java for the game engine I would suggest using Jetty to give you the ability to service HTTP request from a browser. It's all embeddd in your application and there's no faffing with having to package your app as a WAR and deploy it everytime you make a change.

[edit] Just read about Tiny Java Web Sever which could also be an alternative to Jetty if you need a smaller footprint.

Upvotes: 1

Victor
Victor

Reputation: 17107

Yes a webserver will be necessary. Javascript will probably run as part of an html form. If any Java side processing is required, then web server is necessary. Since it is not possible for the html/javascript to communicate with a standalone java program. But Why need a java server side program at all? Can the logic not be written in java script totally?

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075009

Do I need a full fledged web server to accommodate for this? How does the JavaScript communicate with the Java program?

If you're going to host the JavaScript and display in a browser, then yes, the browser would need to be able to request the data from the Java via an HTTP request. Although browsers can load data via file:/// URIs, it's (quite) awkward to handle those requests dynamically.

It's trivial to install a web server, though. Tomcat is easy to install, and there are several other options if you prefer, such as TJWS (no relation), Winstone, LiteWebServer, Jetty...

Upvotes: 1

Related Questions