pApplet problem...
in
Contributed Library Questions
•
1 year ago
Hi all I wonder if anyone can help me. I am writing a program which interfaces with a scanner. I would like to have a secondary window which displays the images as they are scanned. I am using the control p5 library, specifically, the pApplet as a secondary window. the trouble I'm having is that the pApplet stuff is setup right before Void Setup. That's all very well, but I have asked "theApplet" to load up an image using variables which are yet to be established. for example the file name and file extension are given by a textfield. Is there a simpler method of going about what I am trying to achieve or can I pull it off this way?
- // controlWindowCanvas class
- class MyCanvas extends ControlWindowCanvas {
- public PImage P;
- public MyCanvas() {
- P = loadImage("C:/Users/arthur prior/Desktop/OUTPUT/" +MARGINmyTextfield.getText() + "." + FILETYPE.captionLabel());
- }
- public void draw(PApplet theApplet) {
- theApplet.image(P,0,0);
- }
- }
- import processing.opengl.*;
- import javax.imageio.ImageIO;
- import java.awt.Toolkit;
- import java.awt.image.BufferedImage;
- import processing.serial.*;
- import controlP5.*;
- ControlP5 controlP5;
- Serial port;
- Textfield MARGINmyTextfield;
- ListBox RESOLUTION;
- ListBox FILETYPE;
- ControlWindow controlWindow;
- ControlWindowCanvas cc;
- // controlWindowCanvas class
- class MyCanvas extends ControlWindowCanvas {
- public PImage P;
- public MyCanvas() {
- P = loadImage("C:/Users/arthur prior/Desktop/OUTPUT/" +MARGINmyTextfield.getText() + "." + FILETYPE.captionLabel());
- }
- public void draw(PApplet theApplet) {
- theApplet.image(P,0,0);
- }
- }
- void setup() {
- size(420, 594);
- frame.setLocation(0,0); // set to the position of the second display
- controlP5 = new ControlP5(this); // initialize the GUI controls
- println("Available serial ports:");
- println(Serial.list());
- port = new Serial(this, "COM8", 9600);
- MARGINmyTextfield = controlP5.addTextfield ("MARGIN",100,100,125,15); // addTextfield(name,x,y,width,height)
- MARGINmyTextfield.setAutoClear (false);
- MARGINmyTextfield.keepFocus (true);
- controlWindow = controlP5.addControlWindow("controlP5window",100,100,420, 594,30);
- controlWindow.setUpdateMode(ControlWindow.NORMAL);
- cc = new MyCanvas();
- cc.pre();
- controlWindow.addCanvas(cc);
- controlP5.addButton ("UPPE", 1,165,180,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("DOUNE", 1,165,220,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("RIHTE", 1,230,200,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("LIBBE", 1,100,200,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("RTN", 1,100,300,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("SCAN", 1,230,300,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("CLEAR", 1,230,100,40,16); // buton(name,value,x,y,width,height)
- RESOLUTION = controlP5.addListBox("RESOLUTION",100,425,80,75);
- RESOLUTION.setItemHeight(15);
- RESOLUTION.setBarHeight(15);
- RESOLUTION.actAsPulldownMenu(true);
- RESOLUTION.captionLabel().set("RESOLUTION (DPI)");
- RESOLUTION.captionLabel().style().marginTop = 3;
- RESOLUTION.valueLabel().style().marginTop = 3; // the +/- sign
- RESOLUTION.addItem("72", 72);
- RESOLUTION.addItem("150",150);
- RESOLUTION.addItem("240",240);
- RESOLUTION.addItem("300",300);
- RESOLUTION.addItem("360",360);
- RESOLUTION.addItem("400",400);
- RESOLUTION.addItem("600", 600);
- RESOLUTION.addItem("720",720);
- RESOLUTION.addItem("800",800);
- RESOLUTION.addItem("1200",1200);
- RESOLUTION.addItem("2400",2400);
- RESOLUTION.addItem("3200",3200);
- RESOLUTION.addItem("4800",4800);
- RESOLUTION.addItem("6400",6400);
- RESOLUTION.addItem("9600",9600);
- RESOLUTION.addItem("12800",12800);
- RESOLUTION.setColorBackground(color(255,128));
- RESOLUTION.setColorActive(color(0,0,255,128));
- //////////////////////////////////////////////
- FILETYPE = controlP5.addListBox("FILETYPE",192,425,80,120);
- FILETYPE.setItemHeight(15);
- FILETYPE.setBarHeight(15);
- FILETYPE.actAsPulldownMenu(true);
- FILETYPE.captionLabel().set("FILE TYPE");
- FILETYPE.captionLabel().style().marginTop = 3;
- FILETYPE.valueLabel().style().marginTop = 3; // the +/- sign
- FILETYPE.addItem("jpg",1);
- FILETYPE.addItem("tiff",2);
- FILETYPE.addItem("png",3);
- FILETYPE.addItem("bmp",4);
- FILETYPE.setColorBackground(color(255,128));
- FILETYPE.setColorActive(color(0,0,255,128));
- }
- void draw() {
- }
- void controlEvent(ControlEvent theEvent) {
- println(theEvent.controller().name());
- }
- void UPPE(int theValue){
- port.write('<'+"U"+MARGINmyTextfield.getText()+'>');
- println("sent"+"U"+MARGINmyTextfield.getText()+'>');
- }
- void DOUNE(int theValue) {
- port.write('<'+"D"+MARGINmyTextfield.getText()+'>');
- println("sent"+"D"+MARGINmyTextfield.getText());
- }
- void RIHTE(int theValue) {
- port.write('<'+"R"+MARGINmyTextfield.getText()+'>');
- println("sent"+"R"+MARGINmyTextfield.getText());
- }
- void LIBBE(int theValue) {
- port.write('<'+"L"+MARGINmyTextfield.getText()+'>');
- println("sent"+"L"+MARGINmyTextfield.getText());
- }
- void RTN(int theValue) {
- port.write('<'+"H"+MARGINmyTextfield.getText()+'>');
- println("sent"+"H"+MARGINmyTextfield.getText());
- }
- void CLEAR(int theValue) {
- MARGINmyTextfield.clear();
- println("sent CLEAR"); // print it to the debug screen as well
- }
- void SCAN() {
- try {
- TwainSource source = TwainManager.getDefaultSource();
- source.setVisible(false);
- double resolution = Double.parseDouble(RESOLUTION.stringValue());
- System.err.println("PARSE RESOLUTION : "+resolution);
- source.setResolution(resolution);
- System.err.println("RESOLUTION DPI : "+source.getResolution());
- MorenaImage morenaImage = new MorenaImage(source);
- Image image=Toolkit.getDefaultToolkit().createImage(morenaImage);
- BufferedImage bimg=new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
- bimg.createGraphics().drawImage(image, 0, 0, null);
- ImageIO.write(bimg, "jpg" , new File ("C:/Users/arthur prior/Desktop/OUTPUT/" +MARGINmyTextfield.getText() + "." + FILETYPE.captionLabel()));
- } catch (Exception ex) {
- }
- }
- void serialEvent(Serial aPort){
- String myString = aPort.readStringUntil('\n');
- if (myString != null){
- myString = trim(myString);
- println(myString);
- }
- }
The error message I get is as follows:
Exception in thread "Animation Thread" java.lang.NullPointerException
at VISTAVISION_V8$MyCanvas.<init>(VISTAVISION_V8.java:120)
at VISTAVISION_V8.setup(VISTAVISION_V8.java:146)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
Thankyou for your time! Best, Arthur
1