GenericImageCanvas.java from GridBlocks at Krugle
Show GenericImageCanvas.java syntax highlighted
/**
* Copyright (c) 2004
* Helsinki Institute of Physics
* see LICENSE file for details
*
* GenericImageCanvas.java
* Created on Oct 7, 2003
*/
package fi.hip.gb.client;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import fi.hip.gb.midlet.core.LiteDescription;
import fi.hip.gb.midlet.core.LiteResult;
import fi.hip.gb.midlet.util.Float;
/**
* Shows one image on screen.
*
* @author Juho Karppinen
*/
public abstract class GenericImageCanvas extends Canvas implements CommandListener {
/** image to be shown */
private Image image;
/** result data */
protected LiteResult data;
private boolean mode = false;
/** x-start coordinate, value in pixels */
private int cropX;
/** y-start coordinate, value in pixels */
private int cropY;
/** width in pixels */
private int cropW;
/** heigth in pixels */
private int cropH;
/** empty marginal on left and right */
private int marginalX;
/** empty marginal on top and bottom */
private int marginalY;
public GenericImageCanvas(LiteResult imageData) {
this.data = imageData;
this.image = Image.createImage(this.data.readBytes(), 0, this.data.getSize());
if(System.getProperty("microedition.profiles").startsWith("MIDP-2"))
setTitle(data.getName());
this.marginalX = (this.getWidth() - this.image.getWidth())/2;
this.marginalY = (this.getHeight() - this.image.getHeight())/2;
this.cropX = this.marginalX;
this.cropY = this.marginalY;
System.out.println("marginals for image "
+ image.getWidth() + "x" + image.getHeight()
+ " is " + marginalX + "," + marginalY);
setCommandListener(this);
}
public void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
if(! mode) {
if (action == FIRE) {
mode = true;
} else if(action == RIGHT) {
cropX+=5;
if(cropW > 0) cropW-=5;
} else if(action == LEFT) {
cropX-=5;
if(cropW > 0) cropW+=5;
} else if(action == DOWN) {
cropY+=5;
if(cropH > 0) cropH-=5;
} else if(action == UP) {
cropY-=5;
if(cropH > 0) cropH+=5;
}
} else {
if (action == FIRE) {
mode = false;
} else if(action == RIGHT) {
cropW+=5;
} else if(action == LEFT) {
cropW-=5;
} else if(action == DOWN) {
cropH+=5;
} else if(action == UP) {
cropH-=5;
}
}
// check boundaries
if(cropX < marginalX) cropX = marginalX;
if(cropX > getWidth()-marginalX) cropX = getWidth()-marginalX;
if(cropY < marginalY) cropY = marginalY;
if(cropY > getHeight()-marginalY) cropY = getHeight()-marginalY;
if(cropW < 0) cropW = 0;
if(cropX + cropW > getWidth()) cropW = getWidth() - cropX;
if(cropH < 0) cropH = 0;
if(cropY + cropH > getHeight()) cropH = getHeight() - cropY;
repaint();
//System.out.println("x " + cropX + " / " + cropW);
//System.out.println("y " + cropY + " / " + cropH);
}
public void paint(Graphics g) {
if(image != null) {
g.setGrayScale(255);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(
image,
getWidth() / 2,
getHeight() / 2,
Graphics.HCENTER | Graphics.VCENTER);
//if(cropX != 0 && cropY != 0) {
g.setGrayScale(255);
g.fillRect(cropX, cropY, 6, 6);
g.drawRect(cropX, cropY, cropW, cropH);
g.setGrayScale(0);
g.fillRect(cropX, cropY, 3, 3);
g.drawRect(cropX+1, cropY+1, cropW-2, cropH-2);
//}
}
}
/**
* Gets description for new retrieving cropped area.
*
* @param oldWds general description about the job and flags of the result
* @return given description with updated CROP values
*/
protected LiteDescription getDescription(LiteDescription oldWds) {
String[] params = oldWds.getParameters();
String oldCrop = params[2];
// process with area selection
if(cropW != 0 && cropH != 0) {
System.out.println(oldWds.getFlags().toString());
//String oldCrop = oldWds.getFlags().getProperty("CROP");
System.out.println("oldcrop " + oldCrop);
String newCrop = null;
if(oldCrop != null) {
newCrop = getCropString(oldCrop);
} else {
newCrop = new Float(cropX-marginalX).Div(getWidth()).toString() + ","
+ new Float(cropY-marginalY).Div(getHeight()).toString() + ","
+ new Float(cropW).Div(this.image.getWidth()).toString() + ","
+ new Float(cropH).Div(this.image.getHeight()).toString();
}
System.out.println("new CROP = " + newCrop);
// no existing value found, so lets add a new crop parameter
params[2] = newCrop;
}
LiteDescription wds = new LiteDescription("", "processImage");
if(oldWds.getParentID() != null)
wds.setParentID(oldWds.getParentID());
else
wds.setParentID(oldWds.getJobID());
for(int i=0; i < params.length; i++)
wds.addParameter(params[i]);
return wds;
}
/**
* Gets the area of cropping. Old crop value must be given
* because new crop is taken from original file, not from
* the already cropped image.
*
* @param oldCropValue old value in string format x:y:width:height
* @return new crop value in format x:y:width:height
*/
private String getCropString(String oldCropValue) {
// parse the input and fill the values
Float oldX = new Float(1);
Float oldY = new Float(1);
Float oldW = new Float(1);
Float oldH = new Float(1);
int startIndex = 0;
int endIndex = 0;
int position = 0;
while(startIndex + 1 < oldCropValue.length()) {
endIndex = oldCropValue.indexOf(",", startIndex+1);
if(endIndex == -1)
endIndex = oldCropValue.length();
String value = oldCropValue.substring(startIndex, endIndex);
if(position == 0) {
oldX = Float.parse(value);
} else if(position == 1) {
oldY = Float.parse(value);
} else if(position == 2) {
oldW = Float.parse(value);
} else if(position == 3) {
oldH = Float.parse(value);
}
position++;
startIndex = endIndex+1;
}
System.out.println("******** old CROP = " + oldX + "," + oldY + " " + oldW + "x" + oldH);
System.out.println("******** poins CROP = " + cropX + "," + cropY + " " + cropW + "x" + cropH);
System.out.println("******** image size = " + image.getWidth() + "x" + image.getHeight());
// calculate new cropping value
return
oldX.Add(oldW.Mul(cropX-marginalX).Div(this.image.getWidth())).toString() + ","
+ oldY.Add(oldH.Mul(cropY-marginalY).Div(this.image.getHeight())).toString() + ","
+ oldW.Mul(new Float(cropW).Div(this.image.getWidth())).toString() + ","
+ oldH.Mul(new Float(cropH).Div(this.image.getHeight())).toString();
}
}
See more files for this project here