// it was made by kelemen@inf.elte.hu // p6mm symmetry group wallpattern drawer import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.io.*; import javax.swing.*; import javax.swing.text.*; public class grafika_bead extends PApplet { // screen size int nx=780; int ny=480; HScrollbar threshold_bar; HScrollbar red_bar; HScrollbar green_bar; HScrollbar blue_bar; HScrollbar zoom_bar; HScrollbar drawmode_bar; boolean locked = false; RectButton rect1,rect2; RectButton colormode1,colormode2; int xmin=0; int ymin=0; int xmax=400; int ymax=400; // max iteration size int maxiter=40; // scale scalar // float scalar=1/20.0; // threshold float thre=0.9; // parameters float r=0.5; float s=0.2; float A=sqrt(3)/2; float B=1/sqrt(3); float C=2/sqrt(3); int cx,cy; int rc,gc,bc; float fxy,gxy,mx,my; float x1,y1,x2,y2,h1xy,h2xy,f1xy,t,hxy; int drawtype=3; int colortype=1; PFont font; PImage pattern; void setup() { size(nx, ny); colorMode(RGB, 255); background(128); // noLoop(); font = loadFont("dialog-18.vlw"); textFont(font, 18); threshold_bar = new HScrollbar(600, 60, 160, 15, 50,color(255,255,255)); red_bar = new HScrollbar(520, 240, 254, 15, 50,color(255,255,255)); green_bar = new HScrollbar(520, 300, 254, 15, 50,color(255,255,255)); blue_bar = new HScrollbar(520, 360, 254, 15, 50,color(255,255,255)); zoom_bar = new HScrollbar(600, 120, 160, 15, 100,color(255,255,255)); drawmode_bar = new HScrollbar(600, 180, 60, 30, 100,color(255,255,255)); color buttoncolor = color(15, 15, 152); color highlight = color(132, 130, 181); rect1 = new RectButton(520, 420, 75,50, buttoncolor, highlight,"preview"); rect2 = new RectButton(640, 420, 110,50, buttoncolor, highlight,"saving (JPG)"); buttoncolor = color(10,10,180); colormode1 = new RectButton(10, 450, 150,30, buttoncolor, highlight,"COLOR MODE 0"); colormode2 = new RectButton(180, 450, 150,30, buttoncolor, highlight,"COLOR MODE 1"); int[] pix = new int[xmax*ymax+1]; pattern = new PImage(pix,xmax,ymax,RGB); } void drawpattern(PImage pattern,int xmax,int ymax,float rscalar,float gscalar,float bscalar,float thre,float scalar) { pattern.loadPixels(); // scalar=1/20.0; for(int iy=0; iy=thre)||(t==0))) { x1=0.5*mx-A*my; y1=A*mx+0.5*my; x2=-0.5*mx-A*my; y2=A*mx-0.5*my; if (drawtype==1) { hxy=sin(mx*scalar)*sin(C*my*scalar); h1xy=sin(x1*scalar)*sin(C*y1*scalar); h2xy=sin(x2*scalar)*sin(C*y2*scalar); } if (drawtype==2) { hxy=sin(mx*scalar)*cos(C*my*scalar); h1xy=sin(x1*scalar)*cos(C*y1*scalar); h2xy=sin(x2*scalar)*cos(C*y2*scalar); } if (drawtype==3) { float zoom=1/scalar*10; hxy=cos(my*PI/zoom)*sin(x2*scalar)*cos(C*y2*scalar); h1xy=cos(y1*PI/zoom)*sin(C*y1*scalar); h2xy=cos(y2*PI/zoom)*sin(C*y2*scalar); } if (drawtype==4) { float zoom=scalar*0.05; hxy=cos(sq(my*zoom))*sin(x2*scalar); h1xy=cos(sq(y1*zoom))*sin(C*y1*scalar); h2xy=cos(sq(y2*zoom))*sin(C*y2*scalar); } if (drawtype==5) { float zoom=scalar*0.1; hxy=sin(sq(my*zoom))*cos(sq(x2*zoom)); h1xy=sin(sq(y1*zoom))*cos(sq(C*y1*zoom)); h2xy=sin(sq(y2*zoom))*cos(sq(C*y2*zoom)); } fxy=(r*2*hxy+s*2*h1xy+(s-r)*2*h2xy); f1xy=r*2*h1xy+s*2*h2xy-(s-r)*2*hxy; /* g(x,y)=f(x,y)/sqr(3) - 2*f(x',y')*sqr(3) */ gxy=(B*fxy-C*f1xy); t=fxy*fxy+gxy*gxy; if (sqrt(t)>= thre) { mx=mx-fxy; my=my-gxy; if (colortype==1) { rc=int(i*hxy*rscalar); gc=int(i*h1xy*gscalar); bc=int(i*h2xy*bscalar); } else { if (colortype==2) { rc=int(i*rscalar*0.6); gc=int(i*gscalar*0.6); bc=int(i*bscalar*0.6); } } color c=color(constrain(rc,0,255),constrain(gc,0,255),constrain(bc,0,255)); pattern.pixels[cx+cy*xmax]=c; } i=i+1; } } } pattern.updatePixels(); } float getThreshold() { return (threshold_bar.getPos()-652)/220; } float getRedscalar() { return (abs(red_bar.getPos()-553)); } float getGreenscalar() { return (abs(green_bar.getPos()-553)); } float getBluescalar() { return (abs(blue_bar.getPos()-553)); } float getZoomscalar() { return (abs(zoom_bar.getPos()-660))/460; } int getDrawModescalar() { return ((int)(abs(drawmode_bar.getPos()-1201)*0.07)+1); } void draw() { background(128); fill(128); noStroke(); rect(440,10,280,140); rect(440,230,280,160); // black background to the place of patterns fill(0); // stroke(0); rect(10, 10, 420, 420); image(pattern,20,20,400,400); fill(255); stroke(0); text("P6MM Symmetry Patterns", 440, 30); text("Threshold", 440, 65); text(getThreshold(), 650, 85); text("Zoom factor", 440, 125); text(getZoomscalar(), 650, 150); text("Red", 460, 245); text((int)getRedscalar(), 635, 270); text("Green", 460, 305); text((int)getGreenscalar(), 635, 330); text("Blue", 460, 365); text((int)getBluescalar(), 635, 390); text("Drawmode", 440, 183); text((int)getDrawModescalar(), 625, 215); update(); threshold_bar.draw(); red_bar.draw(); green_bar.draw(); blue_bar.draw(); zoom_bar.draw(); drawmode_bar.draw(); rect1.display(); // rect2.display(); if (colortype==1) { colormode1.basecolor=color(140,10,10); colormode2.basecolor=color(10,10,140); } else { if (colortype==2) { colormode2.basecolor=color(140,10,10); colormode1.basecolor=color(10,10,140); } } colormode1.display(); colormode2.display(); } void update() { threshold_bar.update(); zoom_bar.update(); red_bar.update(); green_bar.update(); blue_bar.update(); drawmode_bar.update(); if(locked == false) { rect1.update(); //rect2.update(); colormode1.update(); colormode2.update(); } else { locked = false; } if(mousePressed) { if(rect1.pressed()) { for(int i=0;i 1) { spos = spos + (newspos-spos)/loose; } } boolean over() { if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight) { return true; } else { return false; } } void draw() { fill(backcolor); rect(xpos, ypos, swidth, sheight); if(over || locked) { fill(153, 102, 120); } else { fill(10,10,10); } rect(spos, ypos, sheight, sheight); } float getPos() { return spos * ratio; } } class Button { int x, y; int xsize,ysize; color basecolor, highlightcolor; color currentcolor; boolean over = false; boolean pressed = false; String st; void update() { if(over()) { currentcolor = highlightcolor; } else { currentcolor = basecolor; } } boolean pressed() { if(over) { locked = true; return true; } else { locked = false; return false; } } boolean over() { return true; } void display() { } } class RectButton extends Button { RectButton(int ix, int iy, int xs,int ys, color icolor, color ihighlight,String s) { x = ix; y = iy; xsize = xs; ysize = ys; basecolor = icolor; highlightcolor = ihighlight; currentcolor = basecolor; st=s; } boolean over() { if (mouseX >= x && mouseX <= x+xsize && mouseY >= y && mouseY <= y+ysize) { over=true; return true; } else { over=false; return false; } } void display() { noFill(); stroke(0); rect(x-2, y-2, xsize+4, ysize+4); stroke(255); fill(currentcolor); rect(x, y, xsize, ysize); fill(255); text(st, x+5, y+ysize/2+4); } } void saveToJPG(String fileName) { int xmax,ymax; xmax=800; ymax=600; int[] pix = new int[xmax*ymax+1]; PImage wallpaper = new PImage(pix,xmax,ymax,RGB); drawtype=getDrawModescalar(); drawpattern(wallpaper,xmax,ymax,getRedscalar()/10,getGreenscalar()/10,getBluescalar()/10,getThreshold(),getZoomscalar()); int w=xmax; int h=ymax; BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics m = img.getGraphics(); pattern.loadPixels(); for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { m.setColor(new Color(wallpaper.pixels[j*w+i])); m.drawLine(i, j, i, j); } } m.dispose(); try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName)); com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(bos); com.sun.image.codec.jpeg.JPEGEncodeParam jep = encoder.getDefaultJPEGEncodeParam(img); jep.setQuality(1.0f, false); encoder.setJPEGEncodeParam(jep); encoder.encode(img); bos.close(); } catch ( Exception e ) { } } File getAFileForSave(){ File file = null; File currentdirectory = new File("."); JFileChooser filechooser = new JFileChooser(currentdirectory); int replycode = filechooser.showSaveDialog(frame); if (replycode == JFileChooser.APPROVE_OPTION){ file = filechooser.getSelectedFile(); } return file; } }