2013-12-22 34 views
6

如何在處理中創建單個草圖的多個窗口?在處理中創建單個草圖的多個窗口

實際上,我想在一個窗口中檢測並跟蹤特定顏色(通過網絡攝像頭),並將檢測到的座標顯示爲另一個窗口中的點。現在,我可以在同一窗口中顯示點檢測它。但我想分成兩個不同的窗口。

回答

9

您需要創建一個新的框架和新的PApplet ......這裏有一個樣本草圖:

import javax.swing.*; 
SecondApplet s; 
void setup() { 
    size(640, 480); 
    PFrame f = new PFrame(width, height); 
    frame.setTitle("first window"); 
    f.setTitle("second window"); 
    fill(0); 
} 
void draw() { 
    background(255); 
    ellipse(mouseX, mouseY, 10, 10); 
    s.setGhostCursor(mouseX, mouseY); 
} 
public class PFrame extends JFrame { 
    public PFrame(int width, int height) { 
    setBounds(100, 100, width, height); 
    s = new SecondApplet(); 
    add(s); 
    s.init(); 
    show(); 
    } 
} 
public class SecondApplet extends PApplet { 
    int ghostX, ghostY; 
    public void setup() { 
    background(0); 
    noStroke(); 
    } 

    public void draw() { 
    background(50); 
    fill(255); 
    ellipse(mouseX, mouseY, 10, 10); 
    fill(0); 
    ellipse(ghostX, ghostY, 10, 10); 
    } 
    public void setGhostCursor(int ghostX, int ghostY) { 
    this.ghostX = ghostX; 
    this.ghostY = ghostY; 
    } 
} 
+0

你介意看這個嗎? http://stackoverflow.com/questions/21437922/combining-processing-applications-into-1-big-executable我在這裏有一個類似的問題,我不知道這個解決方案是否可以適用於我的情況? –

3

一個選擇可能是兩次創建草圖你原來的窗口的大小,只是抵消檢測座標爲草圖尺寸的一半。

這裏是一個非常粗略的代碼片段(assumming BLOB將檢測到的顏色斑點):

int camWidth = 320; 
int camHeight = 240; 
Capture cam; 

void setup(){ 
    size(camWidth * 2,camHeight); 
    //init cam/opencv/etc. 
} 
void draw(){ 
    //update cam and get data 
    image(cam,0,0); 
    //draw 
    rect(camWidth+blob.x,blob.y,blob.width,blob.height); 
} 

說實話,它可能是更容易覆蓋跟蹤的信息。例如,如果您正在進行顏色追蹤,只顯示追蹤區域的邊界框的輪廓。

如果您確實想要顯示另一個窗口,則可以使用JPanel。 查看this answer瞭解正在運行的代碼示例。

+0

喬治的解決方案(大小加倍)可能會是我最喜歡的解決方案,因爲它始終保持整潔! –

1

我會推薦使用G4P,這是一個用於處理的GUI庫,它有一些內置的用於處理多個窗口的功能。我以前用過網絡攝像頭,並且運行良好。它帶有一個易於產生窗口的GWindow對象。網站上有一個short tutorial解釋基礎知識。

我已經包含了一些舊代碼,我會告訴你基本的想法。代碼中發生的事情是,我製作了兩個GWindows,並向它們發送一個PImage以顯示:一個獲取網絡攝像頭圖像,另一個獲取影像。你這樣做的方式是增加GWinData對象以包含你想傳遞給窗口的數據。我沒有爲每個窗口創建一個特定的對象,而只是用兩個PImage創建了一個對象。每個GWindow都有自己的繪製循環(在示例的底部),它從重寫的GWinData對象中加載PImage並顯示它。在主循環中,我讀取網絡攝像頭,然後處理它以創建兩個圖像,然後將它們存儲到GWinData對象中。

希望能給你足夠的開始。

import guicomponents.*; 
import processing.video.*; 

private GWindow window; 
private GWindow window2; 

Capture video; 

PImage sorted; 
PImage imgdif; // image with pixel thresholding 

MyWinData data; 

void setup(){ 

    size(640, 480,P2D); // Change size to 320 x 240 if too slow at 640 x 480 

    // Uses the default video input, see the reference if this causes an error 
    video = new Capture(this, 640, 480, 24); 
    numPixels = video.width * video.height; 

    data = new MyWinData(); 

    window = new GWindow(this, "TEST", 0,0, 640,480, true, P2D); 
    window.isAlwaysOnTop(); 
    window.addData(data); 
    window.addDrawHandler(this, "Window1draw"); 

    window2 = new GWindow(this, "TEST", 640,0 , 640,480, true, P2D); 
    window2.isAlwaysOnTop(); 
    window2.addData(data); 
    window2.addDrawHandler(this, "Window2draw"); 

    loadColors("64rev.csv"); 
    colorlength = mycolors.length; 
    distances = new float[colorlength]; 

    noCursor(); 
} 

void draw() 
{ 
    if (video.available()) 
    { 
     background(0); 
     video.read(); 

     image(video,0,0); 
     loadPixels(); 

     imgdif = get(); // clones the last image drawn to the screen v1.1  
     sorted = get(); 
     /// Removed a lot of code here that did the processing 

     // hand data to our data class to pass to other windows 
     data.sortedimage = sorted; 
     data.difimage = imgdif; 

    } 
} 

class MyWinData extends GWinData { 
    public PImage sortedimage; 
    public PImage difimage; 

    MyWinData(){ 
     sortedimage = createImage(640,480,RGB); 
     difimage = createImage(640,480,RGB); 
    } 
} 


public void Window1draw(GWinApplet a, GWinData d){ 
    MyWinData data = (MyWinData) d; 
    a.image(data.sortedimage, 0,0); 
} 


public void Window2draw(GWinApplet a, GWinData d){ 
    MyWinData data = (MyWinData) d; 
    a.image(data.difimage,0,0); 
} 
+0

你介意看這個嗎? http://stackoverflow.com/questions/21437922/combining-processing-applications-into-1-big-executable我在這裏有一個類似的問題,我不知道這個解決方案是否可以適用於我的情況? –