簡體   English   中英

從網頁調用java方法生成處理草圖並在網頁上顯示此草圖

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

我在 Eclipse 中編寫了一個處理程序,它生成一個圖像並將其保存到我計算機上的特定位置。

有沒有辦法從javascript文件運行這個程序,這樣當用戶單擊網頁上的按鈕時,會調用處理程序中的相應方法,生成圖像,最后將圖像顯示在網頁上。

我在這方面沒有很多經驗,但我想我可以讓處理方法在保存圖像后發出 POST 請求,以便將其發送到相關服務器?

我在這里遇到的主要問題是如何從網頁實際調用該方法。 使用 Processing.js 或將我的 java 代碼轉換為 javascript 並不是一個真正的選擇,因為我需要將 ANTLR 導入到我的 Processing 程序中,據我所知只能與 Java 一起使用。

為了清楚.java ,我的程序是一個.java文件,它通過導入processing.core以處理草圖的形式生成圖像。

我對學習新技術等持開放態度。

任何幫助是極大的贊賞。

您需要從 JavaScript 中調用 HTTP 請求,該請求將在您的后端觸發一個函數。 這可以通過Fetch API來實現,它是一個用於獲取和發送資源的接口。

由於您無法在 Web 上運行 Java,因此您需要依賴 JavaScript 客戶端和 Java 服務器之間的某種形式的通信。 您可以來回發送數據以更新兩端。

下面我創建了如何看待你的結局的例子。 你有你的按鈕,與服務器對話的函數和點擊按鈕時觸發的事件監聽器,將按鈕連接到函數。

<!-- 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.
  });
});

但是你的問題沒有明確的答案,因為它非常廣泛。 我希望至少這會讓您朝着正確的方向前進,了解它的工作原理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM