use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class GlassTutorial method paint.
/**
* {@inheritDoc}
*/
public void paint(Graphics g, Rectangle rect) {
if (internal == null) {
internal = new Label(" ");
internal.setUIID("GlassTutorial");
}
internal.setSize(rect.getSize());
internal.paintComponent(g);
int componentCount = vec.size();
for (int iter = 0; iter < componentCount; iter++) {
Component current = (Component) vec.elementAt(iter);
String pos = (String) current.getClientProperty(POS);
Component dest = (Component) current.getClientProperty(DEST);
int xpos = dest.getAbsoluteX();
int ypos = dest.getAbsoluteY();
int w = dest.getWidth();
int h = dest.getHeight();
if (pos.equals(BorderLayout.CENTER)) {
current.setX(xpos);
current.setY(ypos);
current.setWidth(w);
current.setHeight(h);
current.paintComponent(g);
continue;
}
Dimension d = current.getPreferredSize();
current.setWidth(d.getWidth());
current.setHeight(d.getHeight());
if (pos.equals(BorderLayout.SOUTH)) {
current.setX(xpos + w / 2 - d.getWidth() / 2);
current.setY(ypos + h);
current.paintComponent(g);
continue;
}
if (pos.equals(BorderLayout.NORTH)) {
current.setX(xpos + w / 2 - d.getWidth() / 2);
current.setY(ypos - d.getHeight());
current.paintComponent(g);
continue;
}
if (pos.equals(BorderLayout.EAST)) {
current.setX(xpos + w);
current.setY(ypos + h / 2 - d.getHeight() / 2);
current.paintComponent(g);
continue;
}
if (pos.equals(BorderLayout.WEST)) {
current.setX(xpos - d.getWidth());
current.setY(ypos + h / 2 - d.getHeight() / 2);
current.paintComponent(g);
continue;
}
}
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class Resources method readTimeline.
Timeline readTimeline(DataInputStream input) throws IOException {
int duration = input.readInt();
int width = input.readInt();
int height = input.readInt();
AnimationObject[] animations = new AnimationObject[input.readShort()];
int alen = animations.length;
for (int iter = 0; iter < alen; iter++) {
String name = input.readUTF();
int startTime = input.readInt();
int animDuration = input.readInt();
int x = input.readInt();
int y = input.readInt();
Image i = getImage(name);
if (i == null) {
animations[iter] = AnimationObject.createAnimationImage(name, this, x, y);
} else {
animations[iter] = AnimationObject.createAnimationImage(i, x, y);
}
animations[iter].setStartTime(startTime);
animations[iter].setEndTime(startTime + animDuration);
int frameDelay = input.readInt();
if (frameDelay > -1) {
int frameWidth = input.readInt();
int frameHeight = input.readInt();
animations[iter].defineFrames(frameWidth, frameHeight, frameDelay);
}
if (input.readBoolean()) {
animations[iter].defineMotionX(input.readInt(), startTime, animDuration, x, input.readInt());
}
if (input.readBoolean()) {
animations[iter].defineMotionY(input.readInt(), startTime, animDuration, y, input.readInt());
}
if (input.readBoolean()) {
animations[iter].defineWidth(input.readInt(), startTime, animDuration, input.readInt(), input.readInt());
}
if (input.readBoolean()) {
animations[iter].defineHeight(input.readInt(), startTime, animDuration, input.readInt(), input.readInt());
}
if (input.readBoolean()) {
animations[iter].defineOpacity(input.readInt(), startTime, animDuration, input.readInt(), input.readInt());
}
if (input.readBoolean()) {
animations[iter].defineOrientation(input.readInt(), startTime, animDuration, input.readInt(), input.readInt());
}
}
Timeline tl = Timeline.createTimeline(duration, animations, new Dimension(width, height));
return tl;
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class TableLayout method placeComponent.
/**
* Places the component/constraint in the proper alignment within the cell whose bounds are given
*/
private void placeComponent(boolean rtl, Constraint con, int x, int y, int width, int height) {
con.parent.setX(x);
con.parent.setY(y);
con.parent.setWidth(width);
con.parent.setHeight(height);
Dimension pref = con.parent.getPreferredSize();
int pWidth = pref.getWidth();
int pHeight = pref.getHeight();
if (pWidth < width) {
int d = (width - pWidth);
int a = con.align;
if (rtl) {
switch(a) {
case Component.LEFT:
a = Component.RIGHT;
break;
case Component.RIGHT:
a = Component.LEFT;
break;
}
}
switch(a) {
case Component.LEFT:
con.parent.setX(x);
con.parent.setWidth(width - d);
break;
case Component.RIGHT:
con.parent.setX(x + d);
con.parent.setWidth(width - d);
break;
case Component.CENTER:
con.parent.setX(x + d / 2);
con.parent.setWidth(width - d);
break;
}
}
if (pHeight < height) {
int d = (height - pHeight);
switch(con.valign) {
case Component.TOP:
con.parent.setY(y);
con.parent.setHeight(height - d);
break;
case Component.BOTTOM:
con.parent.setY(y + d);
con.parent.setHeight(height - d);
break;
case Component.CENTER:
con.parent.setY(y + d / 2);
con.parent.setHeight(height - d);
break;
}
}
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class TableLayout method getPreferredSize.
/**
* {@inheritDoc}
*/
public Dimension getPreferredSize(Container parent) {
Style s = parent.getStyle();
int w = s.getPaddingLeftNoRTL() + s.getPaddingRightNoRTL();
int h = s.getPaddingTop() + s.getPaddingBottom();
int maxW = Display.getInstance().getDisplayWidth() * 2;
int maxH = Display.getInstance().getDisplayHeight() * 2;
for (int iter = 0; iter < columns; iter++) {
w += getColumnWidthPixels(iter, maxW, -1);
}
for (int iter = 0; iter < rows; iter++) {
h += getRowHeightPixels(iter, maxH, -1);
}
return new Dimension(w, h);
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class ImageIO method saveAndKeepAspect.
/**
* Scales an image on disk while maintaining an aspect ratio, the appropriate aspect size will be
* picked based on the status of scaleToFill
* @param imageFilePath the path to the image
* @param preferredOutputPath the url where the image will be saved
* @param format the format for the image either FORMAT_JPEG or FORMAT_PNG
* @param width the desired width, either width or height will be respected based on aspect dimensions
* @param height the desired height, either width or height will be respected based on aspect dimensions
* @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that
* this isn't implemented in all platforms.
* @param onlyDownscale will not scale if the resolution to scale will be higher in this case will return the imageFilePath
* @param scaleToFill when set to true will pick the larger value so the resulting image will be at least as big as width x height, when set to false
* will create an image that is no bigger than width x height
* @return the url for the scaled image or the url of the unscaled image
* @throws IOException if the operation fails
*/
public String saveAndKeepAspect(String imageFilePath, String preferredOutputPath, String format, int width, int height, float quality, boolean onlyDownscale, boolean scaleToFill) throws IOException {
Dimension d = getImageSize(imageFilePath);
if (onlyDownscale) {
if (scaleToFill) {
if (d.getHeight() <= height || d.getWidth() <= width) {
return imageFilePath;
}
} else {
if (d.getHeight() <= height && d.getWidth() <= width) {
return imageFilePath;
}
}
}
float ratio = ((float) d.getWidth()) / ((float) d.getHeight());
int heightBasedOnWidth = (int) (((float) width) / ratio);
int widthBasedOnHeight = (int) (((float) height) * ratio);
if (scaleToFill) {
if (heightBasedOnWidth >= width) {
height = heightBasedOnWidth;
} else {
width = widthBasedOnHeight;
}
} else {
if (heightBasedOnWidth > width) {
width = widthBasedOnHeight;
} else {
height = heightBasedOnWidth;
}
}
OutputStream im = FileSystemStorage.getInstance().openOutputStream(preferredOutputPath);
save(imageFilePath, im, format, width, height, quality);
return preferredOutputPath;
}
Aggregations