I'm using Processing 3.0.1 which is the latest version.
I successed to display the image when I pressed a certain key.
For example, when I press key 'a', then the image will be displayed.
I also want to make the image disappear when I press another key.
However, I can't find a way to do this.
If anyone knows how to do this, please help me
Here is the code
PImage Onepiece1, Onepiece2;
void setup(){
size(600,600);
Onepiece1 = loadImage("Onepiece1.jpg");
Onepiece2 = loadImage("Onepiece2.jpg");
}
void draw(){
}
void showimage1(){
image(Onepiece1,10,10);
}
void keyPressed(){
if(key == 'a'){
showimage1();
}
else if(key == 'b'){
// I want to make the image disappear when I press 'b'
}
println(key);
}
For this simple scenario #Majlik suggestion is ok, but usually it's not a good idea to draw in callback functions like keyPressed(). Instead use a flag to drive drawings in draw(), like:
Untested
PImage Onepiece1, Onepiece2;
boolean showimage = false;
void setup(){
size(600,600);
Onepiece1 = loadImage("Onepiece1.jpg");
Onepiece2 = loadImage("Onepiece2.jpg");
background (255);
}
void draw(){
background (255);
if(showImage){
showImage1();
}
}
void showimage1(){
image(Onepiece1,10,10);
}
void keyPressed(){
if(key == 'a'){
showImage = true;
}
else if(key == 'b'){
showImage = false;
}
println(key);
}
For the simple scenario like this you just need to redraw image with anything. Best practice is to use background() function. So your reaction to pressing b could look like:
if(key == 'b'){
background(99);
}
Also it is good practice to have specified same background color at the beginning inside setup() function:
void setup(){
size(600,600);
...
background(99);
}
Related
For a project, I need to do a processing code able to switch between two completely different states. There is a default state and it switches to the special state when the mouse is pressed. The special state have an initialization phase. It also have an end and when it reaches it, the program switches back to the normal state. The way I would implement this is the following :
boolean isNormal;
void setup(){
//setup things...
isNormal = true;
}
void draw(){
if(isNormal){
normal();
}
else{
special();
if (endReached){
isNormal = true;
}
}
}
void mousePressed(){
specialSetup();
isNormal = false;
}
void normal(){
//normal routine...
}
void special(){
//special routine...
}
Is there a better way to do it (more efficient, cleaner, ...) ?
QUESTION
I've noticed that draw() cycle is interrupted by events elaboration.
In the following example the circle animation will stop at mouse click until the elaborating_function() ends.
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
}
void draw(){
background(#818B95);
//simple animation
fill(0,116,217);
circle(circle_x, 200, 50);
circle_x += animation_speed;
if(circle_x>800){ circle_x = 0; }
}
void mouseClicked() {
elaborating_function();
}
void elaborating_function(){
println("elaboration start");
delay(1000);
println("elaboration end");
}
Of course, a simple solution to run the elaboration without stopping the animation could be to thread("elaborating_function");
But my question is: if it is possible to run the draw cycle into an independent thread instead?
SOLUTION
I've found a possible solution inverting my problem and creating an "independent cycle" parallel to the draw one. Within this cycle is possible to run any function and it will not interfere with the draw execution. Every event triggered by the user needs only to set a specific variable in order to activate (once or more time) the function within the cycle.
int circle_x = 0;
int animation_speed = 5;
boolean call_elaborating_function = false;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
IndependentCycle independentCycle = new IndependentCycle();
independentCycle.setFrequency(1);
new Thread(independentCycle).start();
}
void draw(){
background(#818B95);
//simple animation
fill(0,116,217);
circle(circle_x, 200, 50);
circle_x += animation_speed;
if(circle_x>800){ circle_x = 0; }
}
public class IndependentCycle implements Runnable{
private int frequency; //execution per seconds
public IndependentCycle(){
frequency = 0;
}
public void setFrequency(int frequency){
this.frequency = 1000/frequency;
println(this.frequency);
}
public void run(){
while(true){
print(".");
delay(this.frequency);
//DO STUFF HERE
//WITH IF EVENT == ture IN ORDER TO RUN JUST ONCE !!
if(call_elaborating_function){
call_elaborating_function = false;
elaborating_function();
}
}
}
}
void mouseClicked() {
call_elaborating_function = true;
}
void elaborating_function(){
println("elaboration start");
delay(1000);
println("elaboration end");
}
As far as I know Processing has it's own AnimationThread.
Your proposed solution to thread elaborating_function() is great.
You could have a basic class that implements Runnable if you need a bit more control. With this thread running in parallel, Processing's main animation thread should run along side it just fine without pausing rendering.
This options sounds much simpler than trying to mess with Processing's AnimationThread and potentially have to deal with unexpected behaviour.
What is the actual goal you're trying achieve ?
I am trying to make a calculator in processing. My thought process for how this is going to work is to make a function for if a certain number is pressed on the keyboard. So I would create some sort of for loop or array even that would sense when a number is pressed, and then return true if it goes through the if statements, however, in order for this to work, I would need to put a variable in the place of a specific key on the keyboard. Is this possible?
Code (so far):
void setup() {
size(800,600);
}
void draw() {
background(0);
Nums.create();
}
class Nums {
void create() {
for (int i = 0; i < 9; i++) {
zero(i);
}
}
boolean zero(int amnt) {
if (keyPressed) {
if (key == amnt) {
return true;
} else {
return false;
}
}
}
}
They keyPressed variable and the functions like keyPressed() and keyReleased() are specifically for keyboard input. They don't, and shouldn't, be concerned with on-screen buttons that you click with the mouse.
Instead, you should probably use the mousePressed() function to detect when the mouse is clicked, and then use if statements to figure out which button was clicked. You can use point-rectangle collision detection to detect which button was clicked. There's also a button sketch in the examples that come with the Processing editor.
I'm trying to build a ping pong game with Processing language. For this two-player game I have two controllers at each end of the 'table'. I coded the movements of the players (up and down) by binding them to the keys:
- w and s for player 1
- o and l for player 2
Although this works when I press them one at a time, I cannot figure out how to make them move simultaneously, as in pressing both w and o at the same time.
Here is my code:
int x=535;
int y=350;
int dx=5;
int dy=5;
int pX=10;
int pY=520;
int pX1=1870;
int pY1=520;
int pS=5;
void setup() {
size(1920,1080);
}
void draw() {
background(0);
rect(960,0,5,1080);
rect(pX,pY,40,150);
rect(pX1,pY1,40,150);
ellipse(x,y,50,50);
x=x+dx;
y=y+dy;
bounce();
move();
move1();
}
void bounce(){
if(x>=1920 || x<=0){
dx=-dx;
}
if(y>1080 || y<0){
dy=-dy;
}
}
void move(){
if(keyPressed){
if(key == 's'){
pY+=pS;
}else if (key == 'w'){
pY-=pS;
}
}
}
void move1() {
if(keyPressed){
if(key == 'l'){
pY1+=pS;
}else if (key == 'o'){
pY1-=pS;
}
}
}
What you want to do is create a boolean value for each key you care about. Then in the keyPressed() function, you set the corresponding variable to true, and in the keyReleased() function, you set the corresponding variable to false. Then in your draw() function, you check the variables to determine which keys are pressed.
Shameless self-promotion: I wrote a tutorial on getting user input available here. Check out the Handling Multiple Keys section.
I'm currently having a problem in my asyntask. When the progress bar has start and once I rotate the screen, the progress bar disappear and the activity restart. I am trying to use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
but What if my AsyncTask is not in my activity... Its in another class file common to many activity class. Then how can I get setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); Thanks for anyone who will help.
this works in my app. I put this line in the manifest.
**android:configChanges="orientation|screenSize"**
Here is my solution to this problem:
#Override
protected void onPreExecute() {
Device.lockOrientation((Activity)context);
...
}
#Override
protected void onPostExecute(List<Hydrant> hydrants) {
Device.releaseOrientation((Activity)context);
...
}
The Device implementation:
public class Device {
public static void lockOrientation(Activity activity) {
Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int tempOrientation = activity.getResources().getConfiguration().orientation;
int orientation = 0;
switch(tempOrientation)
{
case Configuration.ORIENTATION_LANDSCAPE:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Configuration.ORIENTATION_PORTRAIT:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
activity.setRequestedOrientation(orientation);
}
public static void releaseOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
As AsyncTasks are only meant to run for a few seconds, this is an acceptable solution to me as this rarely will affect a user. Especially with something as a ProgressDialog giving a nice indication of how long she will have to wait.