Open console instead of window

Hi!

Is it possible to open a console window instead of default window? C++ style, where you can input text / values into it? How does that affect the setup() and draw()? How simple can you make it? I am planning to teach a friend simple coding and wanted to do the random number guessing game, but it would require on-screen buttons which are a pain to explain to a beginner.

Thank you!

Btw, do you have any interesting projects you did as a beginner? I need a large pool of simple projects (up to towers of hanoi difficulty), any suggestions would be nice. Thank you again and have a nice day!

Is it necessary, then, to use Processing? You and your friend could install Python directly on a machine, and use that to get started with simple coding.

See the official Python web site.

Also see Number guessing game in Python 3 and C.

1 Like

Yeah it is best if it is processing. He is relatively new to coding and he is in a course that requires java. So if he becomes familiar with processing, which is basically simplified java, it would be really helpful.

In any case, how can I open the terminal? I still think it would be interesting to know.

Thank you and have a nice day.

1 Like

Just prior to my previous post, I tried to execute a simplified version of a number guessing game in Processing Python Mode. Since that environment uses Python 2, I used the raw_input function to prompt and attempt to collect user input. It raised an EOFError, probably because the Processing Development Environment attempts to keep going without waiting for the user’s input. That’s why I then suggested working outside the Processing Development Environment.

You could try such a strategy in Processing Java Mode to find out whether a similar error occurs. If it does, the best option might be to work with Java independently of Processing. … or perhaps another member of the Processing community may have found a means of overcoming this problem, and could report it here.

EDIT (July 4, 2021):

Well, I tried this in Processing Java Mode, and it would not accept input.

import java.util.Scanner;
Scanner num_inp = new Scanner(System.in);
System.out.println("Enter a number:");
String num_str = num_inp.nextLine();  // get input
System.out.println("You entered " + num_str);  // display the input

:frowning:

2 Likes

Hello,

I tried it in Eclipse and works for this example:

:)

2 Likes

Excellent! Thanks for saving the day, @glv. :smiley:

1 Like

Hello,

Eclipse can be a challenge and I still prefer the simplified interface of the Processing IDE when I code for fun.

This may help anyone that wants to take an adventure and use Eclipse:

:)

2 Likes

Hello,

There is always javax.swing.JOptionPane for simple IO:

import javax.swing.JOptionPane;

int col;

void setup() {
  }

void draw() {
  background(col); 
  }

void keyPressed() {
  thread("MessageDialog_thread");
  } 

void MessageDialog_thread() {
  String strInp = JOptionPane.showInputDialog(null,"Enter a color 0 to 255:");
  col = int(strInp);
  JOptionPane.showMessageDialog(null, col, "Output", JOptionPane.PLAIN_MESSAGE);
  }

:)

2 Likes

I’m not sure if the following two threads are relevant (helpful) but there may be something here:

https://forum.processing.org/two/discussion/8887/prevent-processing-from-opening-a-frame

or here:

Plus there’s this (which you’re probably aware of :slight_smile:) if you want to move over to p5js. I include only for the purpose of others searching the same question at some future date:

:nerd_face:

2 Likes

Thank you for showing me that! I was messing around with it and made a simple project!
I call it: “quasi console”! It functions like a non erasable text software with commands.

image

1 Like

You are looking for a REPL (Read Eval Print Loop) feature. A lot or languages have it (Make a search).
Have a look on the java9 REPL for your project .

1 Like

here is a solution for drawing whilst hiding the sketch window. This seems to create a new thread so I don’t know what limitations it may have.

import java.awt.Frame;
import processing.awt.PSurfaceAWT;
import processing.awt.PSurfaceAWT.SmoothCanvas;
PWindow win1,win2;

public void settings() {
  size(1400, 700);
  
};

void setup() { 
  win1 = new PWindow(width/2-5,0,width/2,height,"first Window");
  win2 = new PWindow(0,0,width/2,height,"Second Window");
  surface.setLocation(-5, 0);
};

void draw() {
  background(255, 0, 0);
  fill(255);
  rect(mouseX, mouseY, frameCount, 10);
  if(win2.canvas!=null)image(win2.canvas,width/2,0);
  if(win1.canvas!=null)image(win1.canvas,0,0);
};

void mousePressed() {
  println("mousePressed in primary window");
};  
class PWindow extends PApplet {
  int x,y,w,h;
  boolean setLocation,setTitle,makeResizable;
  String title;
  PImage canvas;
  color bg = color(random(255),random(255),random(255));
  
  PWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
    
  };
  
  PWindow(int x_,int y_) {
    super();
    x = x_;
    y = y_;
    setLocation = true;
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
    
  };
  
  PWindow(int x_,int y_,int ww, int hh) {
    super();
    x = x_;
    y = y_;
    w = ww;
    h = hh;
    
    setLocation = true;
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  };
  
  PWindow(int x_,int y_,int ww,int hh,String s) {
    super();
    x = x_;
    y = y_;
    w = ww;
    h = hh;
    setLocation = true;
    title = s;
    setTitle = true;
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
    
  };
  
  PWindow(int x_,int y_, String s, boolean k) {
    super();
    x = x_;
    y = y_;
    setLocation = true;
    title = s;
    setTitle = true;
    makeResizable = true;
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  };
  

  void settings() {
    if(w>0&&h>0)size(w, h);
    else size(500, 200);
  };

  void setup() {
    background(150);
    if(setLocation)surface.setLocation(x, y);
    if(setTitle)surface.setTitle(title);
    if(makeResizable)surface.setResizable(true);
    surface.setVisible (false);
  };

  void draw() {
    background(bg);
    ellipse(random(width), random(height), random(50), random(50));
    fill(255);
    text(frameRate,20,20);
    canvas = get();
  };

  void mousePressed() {
    println("mousePressed in secondary window");
    closeWindow();
  };
  
  void closeWindow(){
    Frame frame = ( (SmoothCanvas) ((PSurfaceAWT)surface).getNative()).getFrame();
    frame.dispose();
  };
};

I’m dont know if there is a more efficient alternative to the canvas.get() method.

1 Like