Display Image

1,541 views
Skip to first unread message

Stuartdotclass

unread,
Jul 29, 2012, 5:24:02 PM7/29/12
to codenameone...@googlegroups.com
How to display an image in Codenameone without the GUI builder? i failed understand the logic of the gui builder and i decided to keep coding the way i am best at,
manually. usually i do this in java:
 private BufferedImage image;

   
public ImagePanel() {
       
try {                
          image
= ImageIO.read(new File("image name and path"));
       
} catch (IOException ex) {
           
// handle exception...
       
}
   
}

   
@Override
   
public void paintComponent(Graphics g) {
       
super.paintComponent(g);
        g
.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters            
   
}
what is the equivalent in codenameone? how do i position it manually the to the (x,y) i want?

Shai Almog

unread,
Jul 29, 2012, 11:32:55 PM7/29/12
to codenameone...@googlegroups.com
Its best to store images in the resource file but you can store them in the root of your src directory too.
For a resource file open the file once:
Resource r = Resources.open("theme.res");

Image i = r.getImage("imageName");


for a file in the root:
Image i = Image.createImage("/fileName.png");

Its best not to override paint or define a painter in Codename One, you can show an image either by setting an icon to a label or by setting it as a bgImage to a style object e.g. component.getUnselectedStyle().setBgImage(...);

Paultre Stuart Beaucéjour

unread,
Jul 30, 2012, 1:38:21 AM7/30/12
to codenameone...@googlegroups.com
i did so and i am getting exceptions and my app wouldnt run.  what did i do wrong again?
Screenshot from 2012-07-30 01:35:45.png
Screenshot from 2012-07-30 01:36:49.png

Chen Fishbein

unread,
Jul 30, 2012, 2:00:46 AM7/30/12
to codenameone...@googlegroups.com
place base.png under src/ folder and check if the images are in the jar file.

Paultre Stuart Beaucéjour

unread,
Jul 30, 2012, 2:10:30 AM7/30/12
to codenameone...@googlegroups.com
thanks. it is working. only a bit of visual traces like dust after firing an event, but i think it is from the simulator. i am getting a lot less stressed, i feel like calming now. thanks a lot for your support. i think i will be able to do more practice now. thanks

On Mon, Jul 30, 2012 at 2:00 AM, Chen Fishbein <cf2...@gmail.com> wrote:
place base.png under src/ folder and check if the images are in the jar file.

--
You received this message because you are subscribed to the Google
Groups "CodenameOne Discussions" group.
 
For more information about Codename One please visit http://www.codenameone.com/
 
To post to this group, send email to
codenameone...@googlegroups.com
To unsubscribe from this group, send email to
codenameone-discu...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/codenameone-discussions?hl=en?hl=en

ebu...@gmail.com

unread,
Aug 27, 2012, 8:39:49 AM8/27/12
to codenameone...@googlegroups.com


On Monday, July 30, 2012 5:32:55 AM UTC+2, Shai Almog wrote:
Its best not to override paint or define a painter in Codename One, you can show an image either by setting an icon to a label or by setting it as a bgImage to a style object e.g. component.getUnselectedStyle().setBgImage(...);

When using a Label to display an image, do I have the option to a) fit the image into a size construing, b) display at full pixel ratio, c) scroll the image? 

(In fact, at least in the simulator b) is what happens, but I can't figure out how to scroll around.)

Thanks! 

ebu...@gmail.com

unread,
Aug 27, 2012, 8:44:40 AM8/27/12
to codenameone...@googlegroups.com, ebu...@gmail.com

Aaaand answering myself:
looks like the label should be surrounded by a Container that is set to have Scrollable X and Y.
Simple enough, just need to throw enough containers into the mix at every point.

Reply all
Reply to author
Forward
0 new messages