I was trying to achieve open and save functions frankly I did not understand selectinput and selectoutput
but thanks to it being made easier by G4P I did it
as for undo and redo I used an PImage arraylist to get an instant of the canvas after each action
all work,
I have two small question
I wanna know
if undo redo can be made better since the sizeof the arraylist is how many actions have been taken
and that can get quite big
the other is if I can change the default image format to save in.
the default is tiff.
during saving you can specify format such as myimage.jpg
and it will save in jpg
- import g4p_controls.*;
- GImageButton btnInput, btnOutput;
- int lineToolX, lineToolY;
- PImage save;
- PImage screen;
- ArrayList<PImage> screens;
- int counter=1;
- int redocounter=0;
- void setup() {
- size(400,400);
- background(255);
- frameRate(200);
- loadPixels();
- screens=new ArrayList();
- //PApplet.useNativeSelect=true;
- String[] files;
- files = new String[] {
- "openB.png", "openA.png"
- };
- btnInput = new GImageButton(this, 0,0,files);
- files = new String[] {
- "saveB.png", "saveA.png"
- };
- btnOutput = new GImageButton(this, 0, 57, files);
- }
- void draw()
- {
- rect(0,57*2,57,57*2);
- fill(200);
- //used as undo button
- rect(0,57*2,57,57);
- /*if(mousePressed&&mouseX<57&&mouseY>(57*2)&&mouseY<(57*3))
- {
- --counter;
- set(57,57,screens.get(counter));
- }*/
- fill(100);
- //used as undo button
- rect(0,57*3,57,57);
- if(mousePressed&&mouseX<57&&mouseY>(57*2)&&mouseY<(57*3))
- {}
- if(mousePressed&&mouseX>57){
- stroke(0);
- strokeWeight(3);
- noFill();
- line(pmouseX,pmouseY, mouseX, mouseY);
- }
- }
- void mousePressed()
- {
- if(mouseX<57&&mouseY>(57*2)&&mouseY<(57*3)&&mousePressed)
- {
- counter++;
- if((screens.size()-counter)>=0){
- set(57,57,screens.get(screens.size()-counter));
- println(screens.size()+" "+counter+" "+(screens.size()-counter));}
- }
- if(mouseX<57&&mouseY>(57*3)&&mouseY<(57*4)&&mousePressed)
- {
- if(counter>1){
- counter--;
- if((screens.size())!=counter){
- set(57,57,screens.get(screens.size()-counter));
- println(screens.size()+" "+counter+" "+(screens.size()-counter));} }
- }
- lineToolX = mouseX;
- lineToolY = mouseY;
- }
- void mouseReleased() {
- if(mouseX>57&&mouseY>57){
- redocounter++;
- screen=get(57,57,width,height);
- screens.add(screen);
- println(screens.size()+" "+counter);}
- loadPixels();
- }
- // G4P code for folder and file dialogs
- public void handleButtonEvents(GImageButton button, GEvent event) {
- String fname;
- // File input selection
- if (button == btnInput) {
- // Use file filter if possible
- fname = G4P.selectInput("Open", "png,gif,jpg,jpeg", "Image files");
- save = loadImage(fname);
- set(0, 0, save);
- loadPixels();
- }
- // File output selection
- else if (button == btnOutput) {
- fname = G4P.selectOutput("Save as");
- save = get();
- save.save(fname);
- //lblFile.setText(fname);
- }
- }
1