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 & HelpPrograms › Re: Dispaly second window with image background
Page Index Toggle Pages: 1
Re: Dispaly second window with image background (Read 1265 times)
Re: Dispaly second window with image background
Mar 18th, 2009, 7:17pm
 
Hello :]
I would like to ask You for help with simple thing.
I need to write a Processing program which works like that:

Start the program displaying one main window.
Than, it waits for mousePressed on this window.
Than, load any image by String loadPath = selectInput();
After select image file and click OPEN button img = loadImage(loadPath);

OK that's easy

next step is a big problem for me
I need to open second window !
It's background is an loaded image file (img);
Size of the second window is the size of an image (img)

HOW TO DO THIS ?
Please HELP :)
Re: Dispaly second window with image background
Reply #1 - Mar 18th, 2009, 10:10pm
 
I know I should help you along the way to be able to do this yourself, especially since i've answered this question before.. but i was bored, so here you go;
-edit-
changed a bit.. updated code below
Code:

import javax.swing.JFrame;

PImage img;
JFrame new_window;
MApplet imageviewer;

// the size the outer-frame takes (in windows 7.. not sure about other os)
int frameWidth = 16;
int frameHeight = 38;

void setup()
{
background(0);
stroke(255);
}

void draw()
{
line(random(width),random(height),random(width),random(height));
}

void mousePressed()
{
loadNewImage();
}

// I chose to just make my own filter-function, because of the bug on windowsmachines with FileDialog and FilenameFilter
// see http://developer.java.sun.com/developer/bugParade/bugs/4031440.html for more info
// should use JFileChooser
public boolean isImage(String name) {
return (name.endsWith(".jpg") ||
name.endsWith(".png") ||
name.endsWith(".gif") ||
name.endsWith(".jpeg"));
}

void loadNewImage()
{
// pick image to display in a new window
FileDialog fd = new FileDialog(this.frame,"Open image",FileDialog.LOAD);
fd.setLocation(50, 50);
fd.show();
String dir = fd.getDirectory();
String file = fd.getFile();
fd.dispose();

if( dir != null && file != null && isImage(file)){
// load image
img = loadImage(dir+file);

// open the window / change the content
if(new_window == null){
new_window = new JFrame(file);
// create the image viewer applet
imageviewer = new MApplet();
// add the image viewer applet
new_window.getContentPane().add(imageviewer, BorderLayout.CENTER);
// set it to not be resizable
//new_window.setResizable(false);
// show it
new_window.setVisible(true);
// init
imageviewer.init();
}
// size it
new_window.setSize(img.width + frameWidth, img.height + frameHeight);
}
}

public class MApplet extends PApplet{
public void setup()
{
background(255);
}
public void draw()
{
image(img,0,0);
}
}



-seltar
Re: Dispaly second window with image background
Reply #2 - Mar 19th, 2009, 12:01am
 
Great !
this is it

Thanks SELTAR Smiley

But now I have another problem.
I'm trying to create a simple user interface with controlP5 library.
It's easy to put the button on the first window.
But how to chanche the image placed on second window by clicking that button on the first screen ?

I've written function which do negative of an image.
When I'm calling this function by pressing button on the first screen, the image on the second window doesn't change (still the same source loaded image).

void negative() {
 a=img;
 loadPixels();
 for (int i = 0; i < pixels.length; i++ ) {
   float r = red (a.pixels[i]);
   float g = green (a.pixels[i]);
   float b = blue (a.pixels[i]);
   r = 255 - r;
   g = 255 - g;
   b = 255 - b;
   color c = color(r,g,b);
   pixels[i] = c;
 }
 updatePixels();
 imageviewer.image(a,0,0);
 imageviewer.redraw();
}

Function NEGATIVE show image (a) on second window but it is not modyfied. WHY the source image is displaying ?
how to modify this image ?




PLEASE HELP if you understan me :]
sory for my English. it is not perfect.

Page Index Toggle Pages: 1