use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class Dialog method show.
/**
* Shows a modal dialog with the given component as its "body" placed in the
* center.
*
* @param title title for the dialog
* @param body component placed in the center of the dialog
* @param defaultCommand command to be assigned as the default command or null
* @param cmds commands that are added to the form any click on any command
* will dispose the form
* @param type the type of the alert one of TYPE_WARNING, TYPE_INFO,
* TYPE_ERROR, TYPE_CONFIRMATION or TYPE_ALARM
* @param icon the icon for the dialog, can be null
* @param timeout a timeout after which null would be returned if timeout is 0 inifinite time is used
* @param transition the transition installed when the dialog enters/leaves
* @return the command pressed by the user
*/
public static Command show(String title, Component body, Command defaultCommand, Command[] cmds, int type, Image icon, long timeout, Transition transition) {
Dialog dialog = new Dialog(title);
dialog.dialogType = type;
dialog.setTransitionInAnimator(transition);
dialog.setTransitionOutAnimator(transition);
dialog.lastCommandPressed = null;
dialog.setLayout(new BorderLayout());
if (cmds != null) {
if (commandsAsButtons) {
dialog.placeButtonCommands(cmds);
} else {
for (int iter = 0; iter < cmds.length; iter++) {
dialog.addCommand(cmds[iter]);
}
}
// maps the first command to back
if (cmds.length == 1 || cmds.length == 2) {
dialog.setBackCommand(cmds[0]);
}
}
if (defaultCommand != null) {
dialog.setDefaultCommand(defaultCommand);
}
dialog.addComponent(BorderLayout.CENTER, body);
if (icon != null) {
dialog.addComponent(BorderLayout.EAST, new Label(icon));
}
if (timeout != 0) {
dialog.setTimeout(timeout);
}
if (body.isScrollable() || disableStaticDialogScrolling) {
dialog.setScrollable(false);
}
dialog.show();
return dialog.lastCommandPressed;
}
use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class Dialog method showPopupDialog.
/**
* A popup dialog is shown with the context of a component and its selection, it is disposed seamlessly if the back button is pressed
* or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
* dialog has the PopupDialog style by default.
*
* @param rect the screen rectangle to which the popup should point
* @return the command that might have been triggered by the user within the dialog if commands are placed in the dialog
*/
public Command showPopupDialog(Rectangle rect) {
if (getDialogUIID().equals("Dialog")) {
setDialogUIID("PopupDialog");
if (getTitleComponent().getUIID().equals("DialogTitle")) {
getTitleComponent().setUIID("PopupDialogTitle");
}
getContentPane().setUIID("PopupContentPane");
}
disposeOnRotation = true;
disposeWhenPointerOutOfBounds = true;
Command backCommand = null;
if (getBackCommand() == null) {
backCommand = new Command("Back");
setBackCommand(backCommand);
}
Component contentPane = super.getContentPane();
Label title = super.getTitleComponent();
int menuHeight = calcMenuHeight();
UIManager manager = getUIManager();
// preferred size logic of the dialog won't work with large title borders
if (dialogTitle != null && manager.isThemeConstant("hideEmptyTitleBool", false)) {
boolean b = getTitle().length() > 0;
getTitleArea().setVisible(b);
getTitleComponent().setVisible(b);
if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
getTitleComponent().setPreferredSize(new Dimension(0, 0));
getTitleComponent().getStyle().setBorder(null);
getTitleArea().setPreferredSize(new Dimension(0, 0));
if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
int titlePT = getTitleComponent().getStyle().getPaddingTop();
byte[] pu = getContentPane().getStyle().getPaddingUnit();
if (pu == null) {
pu = new byte[4];
}
pu[0] = Style.UNIT_TYPE_PIXELS;
getContentPane().getStyle().setPaddingUnit(pu);
int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
}
}
}
// allows a text area to recalculate its preferred size if embedded within a dialog
revalidate();
Style contentPaneStyle = getDialogStyle();
boolean restoreArrow = false;
if (manager.isThemeConstant(getDialogUIID() + "ArrowBool", false)) {
Image t = manager.getThemeImageConstant(getDialogUIID() + "ArrowTopImage");
Image b = manager.getThemeImageConstant(getDialogUIID() + "ArrowBottomImage");
Image l = manager.getThemeImageConstant(getDialogUIID() + "ArrowLeftImage");
Image r = manager.getThemeImageConstant(getDialogUIID() + "ArrowRightImage");
Border border = contentPaneStyle.getBorder();
if (border != null) {
border.setImageBorderSpecialTile(t, b, l, r, rect);
restoreArrow = true;
}
}
int prefHeight = contentPane.getPreferredH();
int prefWidth = contentPane.getPreferredW();
if (contentPaneStyle.getBorder() != null) {
prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
}
prefWidth += getUIManager().getLookAndFeel().getVerticalScrollWidth();
int availableHeight = Display.getInstance().getDisplayHeight() - menuHeight - title.getPreferredH();
int availableWidth = Display.getInstance().getDisplayWidth();
int width = Math.min(availableWidth, prefWidth);
int x = 0;
int y = 0;
Command result;
boolean showPortrait;
if (popupDirectionBiasPortrait != null) {
showPortrait = popupDirectionBiasPortrait.booleanValue();
} else {
showPortrait = Display.getInstance().isPortrait();
}
// if we don't have enough space then disregard device orientation
if (showPortrait) {
if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
showPortrait = false;
}
} else {
if (availableHeight / 2 > availableWidth - rect.getWidth()) {
showPortrait = true;
}
}
if (showPortrait) {
if (width < availableWidth) {
int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
// if the ideal position is less than 0 just use 0
if (idealX > 0) {
// if the idealX is too far to the right just align to the right
if (idealX + width > availableWidth) {
x = availableWidth - width;
} else {
x = idealX;
}
}
}
if (rect.getY() < availableHeight / 2) {
// popup downwards
y = rect.getY() + rect.getSize().getHeight();
int height = Math.min(prefHeight, availableHeight - y);
result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
} else {
// popup upwards
int height = Math.min(prefHeight, availableHeight - (availableHeight - rect.getY()));
y = rect.getY() - height;
result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
}
} else {
int height = Math.min(prefHeight, availableHeight);
if (height < availableHeight) {
int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
// if the ideal position is less than 0 just use 0
if (idealY > 0) {
// if the idealY is too far up just align to the top
if (idealY + height > availableHeight) {
y = availableHeight - height;
} else {
y = idealY;
}
}
}
if (prefWidth > rect.getX()) {
// popup right
x = rect.getX() + rect.getSize().getWidth();
if (x + prefWidth > availableWidth) {
x = availableWidth - prefWidth;
}
width = Math.min(prefWidth, availableWidth - x);
result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
} else {
// popup left
width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
x = rect.getX() - width;
result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
}
}
if (restoreArrow) {
contentPaneStyle.getBorder().clearImageBorderSpecialTile();
}
if (result == backCommand) {
return null;
}
return result;
}
use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class EncodedImage method scaledEncoded.
/**
* Performs scaling using ImageIO to generate an encoded Image
* @param width the width of the image, -1 to scale based on height and preserve aspect ratio
* @param height the height of the image, -1 to scale based on width and preserve aspect ratio
* @return new encoded image
*/
public EncodedImage scaledEncoded(int width, int height) {
if (width == getWidth() && height == getHeight()) {
return this;
}
if (width < 0) {
float ratio = ((float) height) / ((float) getHeight());
width = Math.max(1, (int) (getWidth() * ratio));
} else {
if (height < 0) {
float ratio = ((float) width) / ((float) getWidth());
height = Math.max(1, (int) (getHeight() * ratio));
}
}
try {
ImageIO io = ImageIO.getImageIO();
if (io != null) {
String format = ImageIO.FORMAT_PNG;
if (isOpaque() || !io.isFormatSupported(ImageIO.FORMAT_PNG)) {
if (io.isFormatSupported(ImageIO.FORMAT_JPEG)) {
format = ImageIO.FORMAT_JPEG;
}
}
if (io.isFormatSupported(format)) {
// do an image IO scale which is more efficient
ByteArrayOutputStream bo = new ByteArrayOutputStream();
io.save(new ByteArrayInputStream(getImageData()), bo, format, width, height, 0.9f);
EncodedImage img = EncodedImage.create(bo.toByteArray());
Util.cleanup(bo);
img.opaque = opaque;
img.opaqueChecked = opaqueChecked;
if (width > -1 && height > -1) {
img.width = width;
img.height = height;
}
return img;
}
}
} catch (IOException err) {
// normally this shouldn't happen but this will keep falling back to the existing scaled code
Log.e(err);
}
return null;
}
use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class Image method rotate.
/**
* Returns an instance of this image rotated by the given number of degrees. By default 90 degree
* angle divisions are supported, anything else is implementation dependent. This method assumes
* a square image. Notice that it is inefficient in the current implementation to rotate to
* non-square angles,
* <p>E.g. rotating an image to 45, 90 and 135 degrees is inefficient. Use rotatate to 45, 90
* and then rotate the 45 to another 90 degrees to achieve the same effect with less memory.
*
* @param degrees A degree in right angle must be larger than 0 and up to 359 degrees
* @return new image instance with the closest possible rotation
*/
public Image rotate(int degrees) {
CodenameOneImplementation i = Display.impl;
if (i.isRotationDrawingSupported()) {
if (degrees >= 90) {
int newTransform = 0;
if (transform != 0) {
newTransform = (transform + degrees) % 360;
} else {
newTransform = degrees % 360;
}
degrees %= 90;
newTransform -= degrees;
if (degrees != 0) {
Image newImage = new Image(Display.impl.rotate(image, degrees));
newImage.transform = newTransform;
return newImage;
} else {
Image newImage = new Image(image);
newImage.transform = newTransform;
return newImage;
}
}
if (degrees != 0) {
return new Image(Display.impl.rotate(image, degrees));
}
return this;
} else {
return new Image(Display.impl.rotate(image, degrees));
}
}
use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class Image method scaledImpl.
/**
* Returns a scaled version of this image image using the given width and height,
* this is a fast algorithm that preserves translucent information.
* The method accepts -1 to preserve aspect ratio in the given axis.
*
* @param width width for the scaling
* @param height height of the scaled image
* @return new image instance scaled to the given height and width
*/
Image scaledImpl(int width, int height) {
if (width == -1) {
return scaledHeight(height);
}
if (height == -1) {
return scaledWidth(width);
}
Dimension d = new Dimension(width, height);
Image i = getCachedImage(d);
if (i != null) {
return i;
}
if (svgData != null) {
try {
i = createSVG(svgBaseURL, animated, svgData);
} catch (IOException ex) {
i = new Image(this.image);
}
} else {
i = new Image(this.image);
}
i.scaleCache = scaleCache;
i.scale(width, height);
i.transform = this.transform;
i.animated = animated;
i.svgBaseURL = svgBaseURL;
i.svgData = svgData;
cacheImage(new Dimension(width, height), i);
return i;
}
Aggregations