简体   繁体   English

从网页调用java方法生成处理草图并在网页上显示此草图

[英]Calling a java method from web page to generate a Processing sketch and display this sketch on the web page

I have written a Processing program in Eclipse which generates an image and saves it to a specific location on my computer.我在 Eclipse 中编写了一个处理程序,它生成一个图像并将其保存到我计算机上的特定位置。

Is there any way to run this program from a javascript file such that when a user clicks a button on a web page, the appropriate method in the Processing program is called, the image is generated and finally the image is displayed on the web page.有没有办法从javascript文件运行这个程序,这样当用户单击网页上的按钮时,会调用处理程序中的相应方法,生成图像,最后将图像显示在网页上。

I don't have a lot of experience in this area but I imagine I could have the Processing method make a POST request once the image has been saved so that it can be sent to the relevant server?我在这方面没有很多经验,但我想我可以让处理方法在保存图像后发出 POST 请求,以便将其发送到相关服务器?

The main issue I'm having trouble with here is how to actually invoke the method from a web page.我在这里遇到的主要问题是如何从网页实际调用该方法。 Using Processing.js or converting my java code to javascript isn't really an option as I need to import ANTLR into my Processing program which as far as I know can only be used with Java.使用 Processing.js 或将我的 java 代码转换为 javascript 并不是一个真正的选择,因为我需要将 ANTLR 导入到我的 Processing 程序中,据我所知只能与 Java 一起使用。

Just to be clear my program is a .java file which generates an image in the form of a Processing sketch by importing processing.core .为了清楚.java ,我的程序是一个.java文件,它通过导入processing.core以处理草图的形式生成图像。

I'm open to learning new technologies etc.我对学习新技术等持开放态度。

Any help is greatly appreciated.任何帮助是极大的赞赏。

You'll want to call a HTTP request from within JavaScript that will trigger a function on your backend.您需要从 JavaScript 中调用 HTTP 请求,该请求将在您的后端触发一个函数。 This can be achieved with the Fetch API which is an interface to fetch and send resources.这可以通过Fetch API来实现,它是一个用于获取和发送资源的接口。

Since you can't run Java on the web you'll need to rely on some form of communication between the JavaScript client and the Java server.由于您无法在 Web 上运行 Java,因此您需要依赖 JavaScript 客户端和 Java 服务器之间的某种形式的通信。 You are able to send data back and forth to update both ends.您可以来回发送数据以更新两端。

Below I've created examples of how this could look at your end.下面我创建了如何看待你的结局的例子。 You have your button, the function which talks to the server and the event listener that triggers whenever the button is clicked, connecting the button to the function.你有你的按钮,与服务器对话的函数和点击按钮时触发的事件监听器,将按钮连接到函数。

<!-- Create a button in html -->
<button class="process">Process me</button>
// Write a function that sends a HTTP request to your server.
// Can be either POST or GET. Change the 'yoururl' part to your own endpoint.
// The rest of the function will check if the request has been successful and 
// returns a string with the received data from the server, if there is any.
function process() {
  return fetch('yoururl/process', {
    method: 'POST',
  }).then(response => {
    if (response.status === 200) {
      return response.text();
    }
  }).catch(error => {
    console.log(error);
  });
}
// Select the button from the HTML.
const button = document.querySelector('.process');

// Fire a function when the button is clicked.
button.addEventListener('click', function(event) {
  process().then(text => {
    console.log(text); // Show the text that has been received from the server.
  });
});

But your question does not have a clear answer as it is quite broad.但是你的问题没有明确的答案,因为它非常广泛。 I hope that at least this will get you in the right direction as to how it could work.我希望至少这会让您朝着正确的方向前进,了解它的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM