inavnacorp inavnacorp - 7 months ago 33
Java Question

create image Hype Processing, processing.org

I am trying to create image using http://www.hypeframework.org/ and processing.org. I want to create the image and store in a folder then display it on the web using java spark. The following are the steps that I started with.


  1. Create maven project using NetBeans

  2. Added processing.org, spark libraries in maven

  3. Downloaded hype processing jar added to my libraries



My question is how do I create image with then save it to the webapp folder? I am relatively new to hype and processing drawing shapes stuff. The following are my code that I put together:

Right now this cimg.setup() in the main spark controller throws java.lang.NumberFormatException: For input string: "#BC7A38,#996A37,#968063,#AB977C,#997949,#AE9162,#DFC597,#CAB790" error.

I searched google, but can't find any lead. Any help on how I should accomplish my task is greatly appreciated. Thanks!

//-- spark controller -
public class Main {
public static void main(String[] args) {
get("/", new TemplateViewRoute() {
@Override
public ModelAndView handle(Request req, Response res) throws Exception {
HashMap model = new HashMap();
HypeProcessing cimg = new HypeProcessing();
cimg.setup();
model.put("template", "templates/form.vtl");
return new ModelAndView(model, "templates/layout.vtl");
}
}, new VelocityTemplateEngine());
}
}


//--- HypeProcessing class ----
public class HypeProcessing extends PApplet
{
HDrawablePool pool;
HColorPool colors;

@Override
public void setup()
{
int h = 300;
int w = 300;
//size(w, h);
//H.init(this).background(Integer.parseInt("#202020"));
//smooth();

colors = new HColorPool(Integer.parseInt("#BC7A38,#996A37,#968063,#AB977C,#997949,#AE9162,#DFC597,#CAB790"));

pool = new HDrawablePool(121);
pool.autoAddToStage()
.add(new HShape("svg1.svg"))
.add(new HShape("svg2.svg"))
.add(new HShape("svg3.svg"))
.add(new HShape("svg4.svg"))
.add(new HShape("svg5.svg"))
.add(new HShape("svg6.svg"))

.layout(
new HGridLayout()
.startX(50)
.startY(50)
.spacing(50,50)
.cols(11)
)

.onCreate(
new HCallback() {

@Override
public void run(Object obj) {
HShape d = (HShape) obj;
d
.enableStyle(false)
.strokeJoin(ROUND)
.strokeCap(ROUND)
.strokeWeight(1)
.stroke(Integer.parseInt("#000000"))
// .anchorAt(H.CENTER)
.rotate( (int)random(4) * 90 )
.size( 50 + ( (int)random(4) * 50 ) ) // 50, 100, 150, 200
;
d.randomColors(colors.fillOnly());
}
}
)
.requestAll();
saveImage(w,h);
noLoop();
// ...
}

@Override
public void draw()
{
//rect(10, 10, 80, 80);
H.drawStage();
// ...
}

public void saveImage(int width, int height)
{
//Helper c = new Helper();
String p = "webapp/images/";
String fn = p+"image_"+Helper.generateRandomNumber();
PGraphics img = createGraphics(width, height);
img.beginDraw();
boolean use3D = false;
float alpha = 1;
H.stage().paintAll(img, use3D, alpha);
img.endDraw();
img.save(fn);
}

}

Answer

Processing uses a custom color type. You can use web notation like #ff00ff, and under the hood Processing stores that as an integer, so you can even do things like:

int c = #ff00ff;

However, Java functions like Integer.parseInt() have no idea how to handle web notation. Java has no way of mapping a String like "#ff00ff" to an int value. This is why you're getting your error.

Even if Java did know how to map web notation to an int value, you're passing in multiple values within a single String, which doesn't make a ton of sense.

But the point is, you can only use web notation if you're using the Processing editor. If you're writing this in something other than the Processing editor, you'll have to use the color() function.

HColorPool(color(0, 255, 0), color(255, 0, 255), ...);
Comments