We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Putting a new background over current background
Page Index Toggle Pages: 1
Putting a new background over current background (Read 689 times)
Putting a new background over current background
Apr 2nd, 2007, 7:21pm
 
my title kinda says what im trying to do. Basically i want my background of a picture, when clicked to bring in the new picture and make it the background. I have so far got the new picture, to load, however, it does not stay as the background, you can only see a glimpse of ti when you press the mouse button.

Thanks alot. Here's my code, if you want to try it out.

PImage bg,a;

int rectX, rectY;      // Position of square button
int circleX, circleY;  // Position of circle button
int rectSize = 50;     // Diameter of rect
int circleSize = 53;   // Diameter of circle
color rectColor, circleColor, baseColor;
color rectHighlight, circleHighlight;
color currentColor;
boolean rectOver = false;
boolean circleOver = false;

void setup()
{
 size(282, 500);
 frameRate(30);
 rectColor = color(0);
 rectHighlight = color(51);
 circleColor = color(255);
 circleHighlight = color(204);
 baseColor = color(102);
 currentColor = baseColor;
 circleX = width/2+circleSize/2+10;
 circleY = height/2;
 rectX = width/2-rectSize-10;
 rectY = height/2-rectSize/2;
 ellipseMode(CENTER);
 bg = loadImage("me.jpg");
 a = loadImage("paul 2.jpg");
}

void draw()
{
 background(bg);
 update(mouseX, mouseY);

 if(rectOver) {
   fill(rectHighlight);
 } else {
   fill(rectColor);
 }
 stroke(255);
 rect(rectX, rectY, rectSize, rectSize);
 
 if(circleOver) {
   fill(circleHighlight);
 } else {
   fill(circleColor);
 }
 stroke(0);
 ellipse(circleX, circleY, circleSize, circleSize);
}
void mousePressed(){
  image(a,0,0);
}

void update(int x, int y)
{
 if( overCircle(circleX, circleY, circleSize) ) {
   circleOver = true;
   rectOver = false;
 } else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
   rectOver = true;
   circleOver = false;
 } else {
   circleOver = rectOver = false;
 }
}

boolean overRect(int x, int y, int width, int height)
{
 if (mouseX >= x && mouseX <= x+width &&
     mouseY >= y && mouseY <= y+height) {
   return true;
 } else {
   return false;
 }
}

boolean overCircle(int x, int y, int diameter)
{
 float disX = x - mouseX;
 float disY = y - mouseY;
 if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
   return true;
 } else {
   return false;
 }
}
Re: Putting a new background over current backgrou
Reply #1 - Apr 2nd, 2007, 7:42pm
 
Code:

PImage image1;
PImage image2;
PImage imageBackground;
void setup () {
// load images ...
imageBackground = image1;
}
void draw ()
{
background( imageBackground );
}
void mousePressed() {
imageBackground = image2;
}


F
Re: Putting a new background over current backgrou
Reply #2 - Apr 3rd, 2007, 3:19pm
 
ok thanks, but when i click on the rectangle i get a message saying nullPointerException. do i use the term imageBackground, or is that representing something?

thanks again for the help
Re: Putting a new background over current backgrou
Reply #3 - Apr 3rd, 2007, 3:51pm
 
the idea is to have one variable to hold (point to) the current image to be drawn and two for the actual loading. that way you can just swap images.

imageBackground is just a variable name .. use whatever suits you.
same goes for image1 and image2 .. i used them instead of you "bg" and "a".

the NullPointerException happens if there is nothing loaded into that variable.

to give you a better example with your code:
Code:

PImage bg,a,b;

...


a = loadImage("paul 2.jpg");
b = loadImage("me.jpg");
bg = b;

...


void mousePressed(){
if ( bg == a ) bg = b;
else if ( bg == b ) bg = a;
}

...


F
Re: Putting a new background over current backgrou
Reply #4 - Apr 3rd, 2007, 4:21pm
 
ok thats great thanks, but what if i wanted multiple backgrounds, from clicking on the rectangle to get the third picture, then fourth and so on.

Basically im creating a simple game where you click on the rectangle to take to you to a different area of a house and you have to get through it.

would the last post you made have to look like this?

void mousePressed()
{
 if (rectOver){
   ( bg == a ) bg = b;
  if (rectOver){
    ( bg == b ) bg = a;
}
}
Re: Putting a new background over current backgrou
Reply #5 - Apr 3rd, 2007, 4:58pm
 
no.

you could put the images into an array and increment a counter to go to the next image. see my post here on how to load a sequence into an array and walk thru that.

F
Re: Putting a new background over current backgrou
Reply #6 - Apr 3rd, 2007, 6:03pm
 
ok thanks alot for your help.
Page Index Toggle Pages: 1