paula
YaBB Newbies
Offline
Posts: 9
Re: Displaying several websites in a sketch
Reply #7 - Sep 5th , 2008, 2:53pm
Ok, I am getting further and further... When I run the sample program two sketch windows open, one for the sketch itself I guess, and one that is created by the sketch in this line: public void initPanel(int w,int h) { frame=new Frame("JIDCsample.pde"); The sketch's basic window is empty, the new JIDCsample.pde window is a horizontal rectangle which says 'JDICsample.pde - embedded web browser' and indeed, when I scale that window bigger it displays the Processing website! But I am not there yet. I would like the browser window to be really _in_ the sketch window, because I would like to load multiple at the same time, spreading them over the sketch's field. Load multiple at the same time will also be a future hurdle. And the size of the browser window should have a default of 400x300 or something like that. Do you think that this is all possible? Herewith I also post my current code. Anything that I have changed/cleaned I have commented, using my name. // JDICsample.pde // Marius Watz - http://workshop.evolutionzone.com import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.URL; import java.net.MalformedURLException; import org.jdesktop.jdic.browser.*; aBrowser browser; long last; // Set to true to demonstrate setContent instead of // loading URLs. boolean doRandomHTML=false; String rndWords[]; int rndWordNum=0; void setup() { size(200,200); browser=new aBrowser(); browser.initPanel(1024,768); if(!doRandomHTML) browser.setURL("http://processing.org/"); else initRandomText(); } //////////////////////////////////////////// // Random HTML void draw() { if(doRandomHTML && (millis()-last>3000)) { String c1,c2,str; c1=colorToHex(random(200,255),random(150,200),0); c2=colorToHex(random(50,100),random(50,100),0); println(c1+" "+c2); //paula. cleaned up the following lines of the draw str="<HTML><HEAD>"+""; str+="<DIV style=WIDTH: 760px>"; for(int i=0; i<30; i++) {str+=""+getRandLine()+"";} str+="</DIV>"; browser.setContent(str); last=millis(); } } String getRandLine() { String s=""; int num=(int)random(30,50); for(int i=0; i<num; i++) s+=rndWords[(int)random(rndWordNum)]+" "; return s; } void initRandomText() { String lorem="Lorem ipsum dolor sit amet, pellentesque dolor"+ "a vestibulum, hendrerit augue lectus in libero dictumst et,"+ "condimentum gravida vestibulum litora semper. Lectus donec "+ "neque nunc cras molestie est, vel et. Pede inventore vestibulum "+ "justo est non nulla, lacus reiciendis rutrum phasellus nunc leo"+ "natoque. Urna ac id justo luctus lorem, ante viverra nam nam "+ "accusamus, aliquam metus vitae etiam sollicitudin erat ligula. "+ "Sodales tortor amet felis, sit risus mus vel sodales, cursus "+ "auctor, augue semper nam, diam eget. Vulputate ac viverra ante "+ "ipsum tristique ullamcorper, lacus nostra pharetra libero provident."; rndWords=splitTokens(lorem); rndWordNum=rndWords.length; } public static String colorToHex(float r,float g,float b) { String s=""; if(r<16) s+="0"+Integer.toHexString((int)r); else s+=Integer.toHexString((int)r); if(g<16) s+="0"+Integer.toHexString((int)g); else s+=Integer.toHexString((int)g); if(b<16) s+="0"+Integer.toHexString((int)b); else s+=Integer.toHexString((int)b); s=s.toUpperCase(); return s; } //////////////////////////////////////////// // Convenience class for dealing with // the embedded browser engine. public class aBrowser { Frame frame; Panel panel; WebBrowser webBrowser; public aBrowser() { // Set engine to IE BrowserEngineManager mng=BrowserEngineManager.instance(); mng.setActiveEngine(BrowserEngineManager.IE); webBrowser = new WebBrowser(); } public void initPanel(int w,int h) { frame=new Frame("JIDCsample.pde"); frame.setLocation(50,50); frame.setLayout(new BorderLayout()); //frame.setUndecorated(true); // Handle window close requests frame.addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) {System.exit(0);} }); panel = new Panel(); panel.setLayout(new BorderLayout()); panel.setSize(new Dimension(w, h)); //paula. the above method used to be panel.setPreferredSize, // but that was not recognized as a valid method panel.add(webBrowser, BorderLayout.CENTER); frame.add(panel,BorderLayout.CENTER); Label status=new Label( "JDICsample.pde - embedded web browser."); status.setBackground(new Color(100,100,100)); status.setForeground(new Color(255,255,255)); status.setFont(new Font("Arial",Font.PLAIN,15)); status.setSize(600, 20); frame.add(status,BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } public void setContent(String htmlContent) { webBrowser.setContent(htmlContent); } public void setURL(String url) { try { webBrowser.setURL(new URL(url)); // Print out debug messages in the command line. webBrowser.setDebug(false); } catch (MalformedURLException e) { System.out.println(e.getMessage()); return; } } }