setIcon in j2me to set icons for buttons - java-me

I am trying to put in an icon (a scaled image) as part of a button that also contains some text. I am programming in J2ME for the Nokia SDK (S60 device) and using Eclipse.
The code is as follows:
but = new Button("Some text");
Image img = null;
try {
img = Image.createImage("/flower.png");
} catch(IOException e) {
e1.printStackTrace();
}
but.setIcon(img);
The above lines are the code that works properly. I am facing problems in scaling the image to the size of the button. Whenever I try to do that, I get a divide by zero error. The function I am using to scale the image and the way it is being scaled is:
Image img2 = null;
img2 = img.scaled(but.getWidth()/2, but.getHeight());
but.setIcon(img2);
I am unable to figure out why I get a divide by zero error every time I try to run the above code. Is there some other function that I should use? Or is there something I am missing ?

which UI Framework are using, is it LWUIT? if yes, you can't get the width/height of any component before showing the form, you should use getPreferredWidth instead

Related

How to display an image saved as "/storage/emulated/0/Download/IMG_1582623402006.jpg" in emulator

I capture a image by camera in emulator and it was saved as:
"/storage/emulated/0/Download/IMG_1582623402006.jpg"
I am trying to display this image in < ImageView > as the following:
Bitmap finalBitmap = BitmapFactory.decodeFile("/storage/emulated/0/Download/IMG_1582623402006.jpg");
holder.image.setImageBitmap(scaledBitmap);
but it shows that "finalBitmap" is empty. What have I missed?
Thanks in advance!
You posted this over a year ago, so it may be too old at this point.
It appears that you are using two variables for your bitmaps: finalBitmap and scaledBitmap and not referencing the proper one in the setImageBitmap command.
Instead, have you tried this?
holder.image.setImageBitmap(finalBitmap);
Not totally sure, but adding some attributes in "options" seems make it work in emulator:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inSampleSize = 5;
Bitmap scaledBitmap = BitmapFactory.decodeFile(/storage/emulated/0/Download/IMG_1582623402006.jpg, options);
holder.image.setImageBitmap(scaledBitmap);

Chrome capture visible tab gives messy result

I have a Chrome extension, where I'm using the captureVisibleTab method of the chrome.tabs API to capture screenshots. I have tested the extension on 3 devices (Chromebooks) and I'm getting mixed results. Two of them work perfectly, but one always returns a completely malformed screenshot.
My code:
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
chrome.tabs.get(tabId, function (_tab) {
if (_tab.status == "complete" && _tab.active ) {
chrome.tabs.captureVisibleTab( function(dataUrl){
});
}
});
});
Any ideas what could be the issue on that one device?
EDIT
Example of a bad screenshot:
I suspect that the device pixel ratio is higher on your 3rd device. This was an issue I was having with Retina displays when building a screenshot app. Basically, certain high-resolution displays have a higher ratio pixels per square inch. You're going to want to find window.devicePixelRatio and divide the context scale by that amount.
Assuming that you are using Canvas to draw the screenshot and capture it into an image, this little snippet should help show what you're going to want to do:
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext("2d");
if(window.devicePixelRatio > 1){
context.scale(1/window.devicePixelRatio, 1/window.devicePixelRatio);
}
context.drawImage(image, 0, 0);
Let me know if that works for you.

How to control the loading order of multiple images in openseadragon

In my situation, I have 10 images loaded in a viewer with the same bounds. each image is arranged one behind other.
// source is an array of url images.
for(i=0;i < source.length-1;i++){
this.viewer.addTiledImage({
tileSource: source[i],
index: i,
opacity:0
});
}
My intention is to control opacity of each image using a slider (range input), the slider set the correspondent image opacity to 1, and let the others with no opacity. This works well.
The idea is preload images and change images smoothly.
The problem
All images are loaded at same time when zooming or panning, so I want to control the load order and give priority to the visible image then load the next image, and so on.
How can I do that? I can't find any method to pause the loading of tiles.
Thanks for your help.
You can try using the 'open' event and adding the next image once the previous one has loaded. Something like:
var i =0, viewer = this.viewer;
viewer.addTiledImage({
tileSource: source[i],
index: i,
opacity:0
});
viewer.addHandler('open', function() {
i++;
if (source[i]) {
viewer.addTiledImage({
tileSource: source[i],
index: i,
opacity:0
});
} else {
viewer.removeAllHandlers('open');
}
});
Note that this is untested sample code but you should get the idea. And you should actually use removeHandler() instead of removeAllHandlers(), but this is just an example.
I can fix my issue by controlling opacity instead of control network download, openseadragon has no method to control the download order at dzi image level, so the way is control the opacity because 0 opacity don't do any download. Thanks to the openseadragon contributors for help me to solve this issue.
https://github.com/openseadragon/openseadragon/issues/971

SmartGWT Dialog setting Height for dynamix text

Here's the deal, im trying to create a popup window that uses a dynamic text,
when the text is too large i would like to cap the height of the window and
use a scrollbar instead to navigate through it but it does not seems to be
working.
code:
final Dialog dialog = new Dialog();
dialog.setMessage(direttivaDescription);
dialog.setOverflow(Overflow.AUTO);
dialog.setWidth(600);
dialog.setHeight(50);
dialog.setIcon(someIcon);
dialog.setButtons(new Button("OK"));
dialog.addButtonClickHandler(new ButtonClickHandler() {
public void onButtonClick(ButtonClickEvent event) {
dialog.hide();
}
});
dialog.draw();
If the text is too large the window height will be resized accordingly. The funny
part is that setWidth method seems to be working just fine.
You need a container such as HLayout, VLayout, DynamicForm etc. where you can add the message in it then finally add the container in the Dialog.
Sample code:
VLayout vLayout=new VLayout();
vLayout.addMember(new Label(message));
vLayout.setOverflow(Overflow.AUTO);
vLayout.setWidth100();
...
dialog.addItem(vLayout);
dialog.draw();
snapshot:

fix a splash screen image to display in j2me

In my j2me App I have tried canvas which works great on Nokia phone but doesn't run on samsung. For that I have to switch to some FORM which in both cases works but only issue is of size, if I create smaller image to fit for both phone screens, one (samsung) shows that ok but other (nokia) leaves a lot more space and vice versa.
I need to have code that could stretch my image and just fix if to the screen size which I basically get by form.getHeight() and form.getWidth() property. I wonder if there is property of Image.createImage(width, height) then why doesn't it stretch it to the value I provide?
my code for that is below
try {
System.out.println("Height: " + displayForm.getHeight());
System.out.println("Width: " + displayForm.getWidth());
Image img1 = Image.createImage("/bur/splashScreen1.PNG");
img1.createImage(displayForm.getHeight(), displayForm.getWidth());
displayForm.append(new ImageItem(null, img1, Item.LAYOUT_CENTER, null));
} catch (Exception ex) {
}
Image
A single image will not fit all screens. But more will do.
The smaller logo image should be less than 96x54, as this is the smallest screen resolution. This image can be used up to the resolution of 128x128 without problems. With bigger resolutions it will look tiny, though.
The bigger logo image should be a bit bigger than 128x128 and can be used up to 240x320.
The code bellow gives as example of how to implement this.
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
class Splash extends javax.microedition.lcdui.Canvas {
Image logo;
Splash () {
if (getWidth() <= 128) {
// sl stands for Small Logo and does not need to have a file extension
// this will use less space on the jar file
logo = Image.createImage("/sl");
} else {
// bl stands for Big Logo
logo = Image.createImage("/bl");
}
}
protected void paint (Graphics g) {
// With these anchors your logo image will be drawn on the center of the screen.
g.drawImage(logo, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER);
}
}
As seen in http://smallandadaptive.blogspot.com.br/2008/10/showing-splash.html
wonder if there is property of Image.createImage(width, height) then why doesn't it stretch it to the value I provide?
Parameters in this method have nothing to do with stretching, see API javadocs:
public static Image createImage(int width, int height)
Creates a new, mutable image for off-screen drawing.
Every pixel within the newly created image is white.
The width and height of the image must both be greater than zero.
Parameters:
width - the width of the new image, in pixels
height - the height of the new image, in pixels
Image class (API javadocs) has two more createImage methods that use parameters called "width" and "height" - one with six, another with four arguments but none of these has anything to do with stretching.
In createImage with six arguments, width and height specify size of the region to be copied (without stretching) from source image.
In method with four arguments, width and height specify how to interpret source ARGB array, without these it would be impossible to find out if, say, array of 12 values represents 3x4 image or 4x3. Again, this has nothing to do with stretching.

Resources