Skip to main content

Canvas in LWUIT

24 replies [Last post]
Anonymous

Hello, I'm MIDP-developer and I found LWUIT a week ago.
It´s nice but I wonder if is posible to put a canvas object into a LWUIT
form.

Thanks.

--
Alejandro Caraballo
SOLTEL SOLUCIONES INFORMATICAS, S.L.

C. Industria, Edif.:Metropol1, 3º mod.:14
41927. Mairena del Aljarafe (Sevilla)
Tfno: +34 955 08 73 00
Fax: +34 955 08 74 00
www.soltel.es
Avda. José Mª Alcaraz y Alenda, 47. 4ºA
Edificio Forum
06011 Badajoz

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
btbird
Offline
Joined: 2009-08-21

hi, Mr.Chen
As u mentioned this code works fine.
But I am getting error at this point
javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();
Note: some input file using or covered outdated API.
I use Netbeans.
and I can not find why?

markww
Offline
Joined: 2006-03-20

Is it safe to call paint() on the Canvas instance? Not sure if I'm reading the docs correctly on lcdui.Canvas, but it says:

"The application code must never call paint(); it is called only by the implementation."

http://java.sun.com/javame/reference/apis/jsr118/

[code]
public void paint(Graphics g)
{
javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();
midpGraphics.setClip(g.getTranslateX() + g.getClipX(),
g.getTranslateY() + g.getClipY(), g.getClipWidth(), g.getClipHeight());
midpGraphics.translate(getX() + g.getTranslateX(), getY() +
g.getTranslateY());

// ????
inner.paint(midpGraphics);

midpGraphics.translate(-getX() - g.getTranslateX(), -getY() -
g.getTranslateY());
}
[/code]

Message was edited by: markww

chen

Hi,
If the Canvas was the current displayable i wouldn't recommend calling
paint, in this case the your canvas is never been setted to the MIDP
Display, calling paint in this case is just a method call overhead.

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> Is it safe to call paint() on the Canvas instance? Not sure if I'm reading the docs correctly on lcdui.Canvas, but it says:
>
> "The application code must never call paint(); it is called only by the implementation."
>
> http://java.sun.com/javame/reference/apis/jsr118/
>
> [code]
> public void paint(Graphics g)
> {
> javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();
> midpGraphics.setClip(g.getTranslateX() + g.getClipX(),
> g.getTranslateY() + g.getClipY(), g.getClipWidth(), g.getClipHeight());
> midpGraphics.translate(getX() + g.getTranslateX(), getY() +
> g.getTranslateY());
>
> inner.paint(midpGraphics);
>
> midpGraphics.translate(-getX() - g.getTranslateX(), -getY() -
> g.getTranslateY());
> }
> [/code]
> [Message sent by forum member 'markww' (markww)]
>
> http://forums.java.net/jive/thread.jspa?messageID=288264
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

markww
Offline
Joined: 2006-03-20

It seems:

com.sun.lwuit.Graphics.getGraphics()

is no longer a public method in the July release. It was being used here in the posted example:

[code]
javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();
[/code]

Any other way to put a Canvas in a Container, or just plain get it rendered to screen while using LWUIT at the same time?

Thanks

chen

This method was never public!, it is package protected.
The Canvas Wrapper is in the same package "com.sun.lwuit.*" this is why
i could access it

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> It seems:
>
> com.sun.lwuit.Graphics.getGraphics()
>
> is no longer a public method in the July release. It was being used here in the posted example:
>
> [code]
> javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();
> [/code]
>
> Any other way to put a Canvas in a Container, or just plain get it rendered to screen while using LWUIT at the same time?
>
> Thanks
> [Message sent by forum member 'markww' (markww)]
>
> http://forums.java.net/jive/thread.jspa?messageID=288696
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

markww
Offline
Joined: 2006-03-20

Sorry admitedly I am a C++ developer, I didn't know you could just create the same path and the java compiler treats it as the same package, even though it's not part of the jar. Anyway looking forward to giving the Canvas wrapper a try,

Thanks

markww
Offline
Joined: 2006-03-20

Well, it worked, but the performance is not good. I'm trying to use this in a game and the responsiveness is low.

Is there some other recommended method to doing this? I'm suprised more people aren't trying to integrate canvas objects with LWUIT, since it's a core UI component?

Thanks

vilastalewale
Offline
Joined: 2007-02-22

Hi
I got an error in this code
g.getGraphics()
getGraphics is not public is not able to access from outside.

public void paint(Graphics g) {
javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();
midpGraphics.setClip(g.getTranslateX() + g.getClipX(),
g.getTranslateY() + g.getClipY(), g.getClipWidth(), g.getClipHeight());

midpGraphics.translate(getX() + g.getTranslateX(), getY() +
g.getTranslateY());

inner.paint(midpGraphics);

midpGraphics.translate(-getX() - g.getTranslateX(), -getY() -
g.getTranslateY());

}

Thanking Regards
Vilas Talewale

javatai
Offline
Joined: 2005-06-21

Another easy way is to get referenced Display Object in Midlet
my way is to get Display Object in midlet constructor with global variable.

J2Display = javax.microedition.lcdui.Display.getDisplay(this);
Display.init(this);

latter you can use the J2Display.setCurrent(your display object);
Better way to do that. !!

Regards
Anson

ehsanzarin
Offline
Joined: 2009-08-15

hi everyone
I am going to use a splashScreen in my application that uses Lwuit.
I have written a class for splash screen in canvas area as following:

import java.util.*;
import javax.microedition.lcdui.*;

public class SplashScreen extends Canvas {
private Display display;
private Displayable next;
private Timer timer = new Timer();

public SplashScreen(
Display display, Displayable next ){
this.display = display;
this.next = next;

display.setCurrent( this );
}

protected void keyPressed( int keyCode ){
dismiss();
}

protected void paint( Graphics g ){
// do your drawing here
}

protected void pointerPressed( int x, int y ){
dismiss();
}

protected void showNotify(){
timer.schedule( new CountDown(), 5000 );
}

private void dismiss(){
timer.cancel();
display.setCurrent( next );
}

private class CountDown extends TimerTask {
public void run(){
dismiss();
}
}
}

in the Midlet class and in the startApp I just call the splash class as following:

new SplashScreen(display, new TrivialForm() );

but the error is can not find symbol
symbol:constructor SplashScreen

I would appreciate it if anyone tell how I solve my problem.

Chen Fishbein

I don't see any LWUIT related code in your post.

Chen

lwuit-users@mobileandembedded.org wrote:
> hi everyone
> I am going to use a splashScreen in my application that uses Lwuit.
> I have written a class for splash screen in canvas area as following:
>
> import java.util.*;
> import javax.microedition.lcdui.*;
>
> public class SplashScreen extends Canvas {
> private Display display;
> private Displayable next;
> private Timer timer = new Timer();
>
> public SplashScreen(
> Display display, Displayable next ){
> this.display = display;
> this.next = next;
>
> display.setCurrent( this );
> }
>
> protected void keyPressed( int keyCode ){
> dismiss();
> }
>
> protected void paint( Graphics g ){
> // do your drawing here
> }
>
> protected void pointerPressed( int x, int y ){
> dismiss();
> }
>
> protected void showNotify(){
> timer.schedule( new CountDown(), 5000 );
> }
>
> private void dismiss(){
> timer.cancel();
> display.setCurrent( next );
> }
>
> private class CountDown extends TimerTask {
> public void run(){
> dismiss();
> }
> }
> }
>
>
> in the Midlet class and in the startApp I just call the splash class as following:
>
> new SplashScreen(display, new TrivialForm() );
>
> but the error is can not find symbol
> symbol:constructor SplashScreen
>
> I would appreciate it if anyone tell how I solve my problem.
> [Message sent by forum member 'ehsanzarin' (ehsanzarin)]
>
> http://forums.java.net/jive/thread.jspa?messageID=361312
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

javatai
Offline
Joined: 2005-06-21

Thanks , I got your code with this.

And I also test the original Display object
it works !!

Thank you
Regards
Anson

robinthapa
Offline
Joined: 2009-08-31

Hello All,

The below code is based on javax.microedition.lcdui.*;
Its simply a classs that extends Canavas and implements ActionListener......
What i am trying is to invoke a class which has a textField which uses LWUIT.That is shown in Bold below:

public void commandAction(Command c, Displayable d) {
if (c == back) {
midlet.showMenu();
} else if (c == select) {
if (sl.getCurrentSel() == 0) {
midlet.showJam();
} else if (sl.getCurrentSel() == 1) {
midlet.showHighWay();
} else if (sl.getCurrentSel() == 2) {
[b] [u] ///// i wish to invoke a Canavs[/u][/b]
}
}
}

vprise
Offline
Joined: 2003-11-07

That isn't supported in LWUIT, you need to switch your application to using LWUIT.

chen

Hi,
I'm not sure what you mean when you say "canvas object", but you can
easily extend the Component class and add your own widgets(see the
userguide section 13)

Regards,
Chen

Alejandro R. Caraballo Maestre wrote:
> Hello, I'm MIDP-developer and I found LWUIT a week ago.
> It´s nice but I wonder if is posible to put a canvas object into a LWUIT
> form.
>
> Thanks.
>
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

Alejandro R. Caraballo Maestre

I mean put javax.microedition.lcdui.Canvas object into a LWUIT form, or
container.
I've got a little midp-aplication for paint in a PDA screen and I would
like to use in my LWUIT programs. One of the metods of my class returns a
canvas object that I want to put in my LWUIT aplication.
Could I get it?

Thanks

El Jue, 29 de Mayo de 2008, 5:33 pm, chen escribió:
> Hi,
> I'm not sure what you mean when you say "canvas object", but you can
> easily extend the Component class and add your own widgets(see the
> userguide section 13)
>
> Regards,
> Chen
>
>
> Alejandro R. Caraballo Maestre wrote:
>> Hello, I'm MIDP-developer and I found LWUIT a week ago.
>> It´s nice but I wonder if is posible to put a canvas object into a LWUIT
>> form.
>>
>> Thanks.
>>
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

Neto Marin

Hi Alejandro,

I think it´s impossible because containers just accept Component:
https://lwuit.dev.java.net/nonav/javadocs/com/sun/lwuit/Container.html
And Canvas is from javax.microedition.lcdui, and so, it´s not compatible.

[]s
Neto

On Thu, May 29, 2008 at 1:07 PM, Alejandro R. Caraballo Maestre <
alejandro.caraballo@soltel.es> wrote:

> I mean put javax.microedition.lcdui.Canvas object into a LWUIT form, or
> container.
> I've got a little midp-aplication for paint in a PDA screen and I would
> like to use in my LWUIT programs. One of the metods of my class returns a
> canvas object that I want to put in my LWUIT aplication.
> Could I get it?
>
> Thanks
>
> El Jue, 29 de Mayo de 2008, 5:33 pm, chen escribió:
> > Hi,
> > I'm not sure what you mean when you say "canvas object", but you can
> > easily extend the Component class and add your own widgets(see the
> > userguide section 13)
> >
> > Regards,
> > Chen
> >
> >
> > Alejandro R. Caraballo Maestre wrote:
> >> Hello, I'm MIDP-developer and I found LWUIT a week ago.
> >> It´s nice but I wonder if is posible to put a canvas object into a LWUIT
> >> form.
> >>
> >> Thanks.
> >>
> >>
> >>
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> > For additional commands, e-mail: users-help@lwuit.dev.java.net
> >
> >
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>

--
Antonio Marin Neto
Sun Certified Programmer for Java 2 Platform 1.4

GSM Mobile: +55 19 8166 6720
Blog: http://weblogs.java.net/blog/netomarin/
GTalk: netomarin@gmail.com
Skype: netomarin
[att1.html]

Shai Almog

Hi Alejandro,
while Neto is 100% correct there is a trick that you can do which if
performance is not critical to you might actually work... (note that
I haven't tried it!)
Essentially what you can do is this:
Derive from the canvas class.
Create a MIDP mutable image with the size in which you want the
canvas to paint (you can override getWidth/getHeight for the desired
size).
Call super.paint(Graphics) with the graphics from the MIDP mutable
image.
Extract the RGB data and draw it using LWUIT RGBImage.

This will be slow but not as slow as it sounds since you can reuse
the RGB array and don't have to allocate it over again.

You can pass events into the canvas easily since most LWUIT events
are very similar to canvas based events.

> Hi Alejandro,
>
> I think it´s impossible because containers just accept Component:
> https://lwuit.dev.java.net/nonav/javadocs/com/sun/lwuit/Container.html
> And Canvas is from javax.microedition.lcdui, and so, it´s not
> compatible.
>
> []s
> Neto
>
> On Thu, May 29, 2008 at 1:07 PM, Alejandro R. Caraballo Maestre
> wrote:
> I mean put javax.microedition.lcdui.Canvas object into a LWUIT
> form, or
> container.
> I've got a little midp-aplication for paint in a PDA screen and I
> would
> like to use in my LWUIT programs. One of the metods of my class
> returns a
> canvas object that I want to put in my LWUIT aplication.
> Could I get it?
>
> Thanks
>
> El Jue, 29 de Mayo de 2008, 5:33 pm, chen escribió:
> > Hi,
> > I'm not sure what you mean when you say "canvas object", but you can
> > easily extend the Component class and add your own widgets(see the
> > userguide section 13)
> >
> > Regards,
> > Chen
> >
> >
> > Alejandro R. Caraballo Maestre wrote:
> >> Hello, I'm MIDP-developer and I found LWUIT a week ago.
> >> It´s nice but I wonder if is posible to put a canvas object into
> a LWUIT
> >> form.
> >>
> >> Thanks.
> >>
> >>
> >>
> >>
> >>
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> > For additional commands, e-mail: users-help@lwuit.dev.java.net
> >
> >
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>
>
> --
> Antonio Marin Neto
> Sun Certified Programmer for Java 2 Platform 1.4
>
> GSM Mobile: +55 19 8166 6720
> Blog: http://weblogs.java.net/blog/netomarin/
> GTalk: netomarin@gmail.com
> Skype: netomarin

[att1.html]

Alejandro R. Caraballo Maestre

Thanks, Shai and Neto. I will try what Shay told me.

El Jue, 29 de Mayo de 2008, 10:28 pm, Shai Almog escribió:
> Hi Alejandro,
> while Neto is 100% correct there is a trick that you can do which if
> performance is not critical to you might actually work... (note that
> I haven't tried it!)
> Essentially what you can do is this:
> Derive from the canvas class.
> Create a MIDP mutable image with the size in which you want the
> canvas to paint (you can override getWidth/getHeight for the desired
> size).
> Call super.paint(Graphics) with the graphics from the MIDP mutable
> image.
> Extract the RGB data and draw it using LWUIT RGBImage.
>
> This will be slow but not as slow as it sounds since you can reuse
> the RGB array and don't have to allocate it over again.
>
> You can pass events into the canvas easily since most LWUIT events
> are very similar to canvas based events.
>
>> Hi Alejandro,
>>
>> I think it´s impossible because containers just accept Component:
>> https://lwuit.dev.java.net/nonav/javadocs/com/sun/lwuit/Container.html
>> And Canvas is from javax.microedition.lcdui, and so, it´s not
>> compatible.
>>
>> []s
>> Neto
>>
>> On Thu, May 29, 2008 at 1:07 PM, Alejandro R. Caraballo Maestre
>> wrote:
>> I mean put javax.microedition.lcdui.Canvas object into a LWUIT
>> form, or
>> container.
>> I've got a little midp-aplication for paint in a PDA screen and I
>> would
>> like to use in my LWUIT programs. One of the metods of my class
>> returns a
>> canvas object that I want to put in my LWUIT aplication.
>> Could I get it?
>>
>> Thanks
>>
>> El Jue, 29 de Mayo de 2008, 5:33 pm, chen escribió:
>> > Hi,
>> > I'm not sure what you mean when you say "canvas object", but you can
>> > easily extend the Component class and add your own widgets(see the
>> > userguide section 13)
>> >
>> > Regards,
>> > Chen
>> >
>> >
>> > Alejandro R. Caraballo Maestre wrote:
>> >> Hello, I'm MIDP-developer and I found LWUIT a week ago.
>> >> It´s nice but I wonder if is posible to put a canvas object into
>> a LWUIT
>> >> form.
>> >>
>> >> Thanks.
>> >>
>> >>
>> >>
>> >>
>> >>
>> >
>> >
>> >
>> ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
>> > For additional commands, e-mail: users-help@lwuit.dev.java.net
>> >
>> >
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
>> For additional commands, e-mail: users-help@lwuit.dev.java.net
>>
>>
>>
>>
>> --
>> Antonio Marin Neto
>> Sun Certified Programmer for Java 2 Platform 1.4
>>
>> GSM Mobile: +55 19 8166 6720
>> Blog: http://weblogs.java.net/blog/netomarin/
>> GTalk: netomarin@gmail.com
>> Skype: netomarin
>
>

--
Alejandro Caraballo
SOLTEL SOLUCIONES INFORMATICAS, S.L.

C. Industria, Edif.:Metropol1, 3º mod.:14
41927. Mairena del Aljarafe (Sevilla)
Tfno: +34 955 08 73 00
Fax: +34 955 08 74 00
www.soltel.es
Avda. José Mª Alcaraz y Alenda, 47. 4ºA
Edificio Forum
06011 Badajoz

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

chen

Hi Alejandro,
There is another option that might give you better performance results.
Create a wrapper Component for Canvas:

*package com.sun.lwuit;

public class CanvasWrapper extends Component{

private Canvas c;

public class CanvasWrapper(Canvas c){
...
}

public void paint(com.sun.lwuit.Graphics g) {
///*//the getGraphics() is a package protected method therefore the
Wrapper class is declared
//in com.sun.lwuit package/*
javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();
//*/insert translate code/
* c.paint(midpGraphics);
//*/undo //translate code/

*}

}
*

I haven't tried this myself but you might encounter a few issues when
you do that:
1)make sure the wrapper component gets size by overriding
calcPreferedSize or by setting setPreferedSize.
2)your canvas probably paints from 0, 0 location that will break the
lwuit Form, what you will have to do is to adjust
the translate params of the midpGraphics Object (use the getX() and
getY(), you might need to use g.getTranslateX() and g.getTranslateY() as
well), before you call to c.paint(midpGraphics);
3)you might want to map all events from the wrapper to the Canvas Object
simply by overriding keyPressed, keyReleased, etc.. in the Wrapper
Component and call to the Canvas Object keyPressed, keyReleased, etc..
methods

Regards and Good Luck,
Chen

Alejandro R. Caraballo Maestre wrote:
> Thanks, Shai and Neto. I will try what Shay told me.
>
> El Jue, 29 de Mayo de 2008, 10:28 pm, Shai Almog escribió:
>
>> Hi Alejandro,
>> while Neto is 100% correct there is a trick that you can do which if
>> performance is not critical to you might actually work... (note that
>> I haven't tried it!)
>> Essentially what you can do is this:
>> Derive from the canvas class.
>> Create a MIDP mutable image with the size in which you want the
>> canvas to paint (you can override getWidth/getHeight for the desired
>> size).
>> Call super.paint(Graphics) with the graphics from the MIDP mutable
>> image.
>> Extract the RGB data and draw it using LWUIT RGBImage.
>>
>> This will be slow but not as slow as it sounds since you can reuse
>> the RGB array and don't have to allocate it over again.
>>
>> You can pass events into the canvas easily since most LWUIT events
>> are very similar to canvas based events.
>>
>>
>>> Hi Alejandro,
>>>
>>> I think it´s impossible because containers just accept Component:
>>> https://lwuit.dev.java.net/nonav/javadocs/com/sun/lwuit/Container.html
>>> And Canvas is from javax.microedition.lcdui, and so, it´s not
>>> compatible.
>>>
>>> []s
>>> Neto
>>>
>>> On Thu, May 29, 2008 at 1:07 PM, Alejandro R. Caraballo Maestre
>>> wrote:
>>> I mean put javax.microedition.lcdui.Canvas object into a LWUIT
>>> form, or
>>> container.
>>> I've got a little midp-aplication for paint in a PDA screen and I
>>> would
>>> like to use in my LWUIT programs. One of the metods of my class
>>> returns a
>>> canvas object that I want to put in my LWUIT aplication.
>>> Could I get it?
>>>
>>> Thanks
>>>
>>> El Jue, 29 de Mayo de 2008, 5:33 pm, chen escribió:
>>>
>>>> Hi,
>>>> I'm not sure what you mean when you say "canvas object", but you can
>>>> easily extend the Component class and add your own widgets(see the
>>>> userguide section 13)
>>>>
>>>> Regards,
>>>> Chen
>>>>
>>>>
>>>> Alejandro R. Caraballo Maestre wrote:
>>>>
>>>>> Hello, I'm MIDP-developer and I found LWUIT a week ago.
>>>>> It´s nice but I wonder if is posible to put a canvas object into
>>>>>
>>> a LWUIT
>>>
>>>>> form.
>>>>>
>>>>> Thanks.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>>
>>>> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
>>>> For additional commands, e-mail: users-help@lwuit.dev.java.net
>>>>
>>>>
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
>>> For additional commands, e-mail: users-help@lwuit.dev.java.net
>>>
>>>
>>>
>>>
>>> --
>>> Antonio Marin Neto
>>> Sun Certified Programmer for Java 2 Platform 1.4
>>>
>>> GSM Mobile: +55 19 8166 6720
>>> Blog: http://weblogs.java.net/blog/netomarin/
>>> GTalk: netomarin@gmail.com
>>> Skype: netomarin
>>>
>>
>
>
>

[att1.html]

javatai
Offline
Joined: 2005-06-21

Hi, Mr. Chen:
May you show the source for that ?
Or LWUIT may provide the utility for us to use ?

Thanks
Regards

chen

Hi,
Here are the code samples of embedding a Canvas inside a LWUIT Form:

1)This is a simple Canvas I created that simply draws a line and
printouts the key events.
Notice that i am not using Canvas.getWidth() and Canvas.getHeight()
since these methods will give me the Screen dimensions instead of the
Component size.

public class MyCanvas extends Canvas{

private int width, height;

public void setWidth(int width) {
this.width = width;
}

public void setHeight(int height) {
this.height = height;
}

public void paint(Graphics g) {
g.drawLine(0, 0, width, height);

}

public void keyPressed(int key) {
System.out.println("keyPressed " + key);
}

protected void keyReleased(int key) {
System.out.println("keyReleased " + key);
}
}

2)This is the wrapper class.
Some important points on this code:
*When the Component gets its size I delegate the width and the
height to the inner Canvas.
*The keyPressed calls Component.setHandlesInput(boolean val) method
when the fire key is pressed.
Normally, a Form moves the foucs from 1 component to the other when the
user clicks on the arrow keys, by using the setHandlesInput method I
implies to the Form to stop navigate between the Components on the Screen.
That allows me to keep the focus on the CanvasWrapper and to fwd all the
events to the inner Canvas.

package com.sun.lwuit;

public class CanvasWrapper extends Component {

private MyCanvas inner;

public CanvasWrapper(MyCanvas inner) {
this.inner = inner;
getStyle().setBgTransparency(0);

}

public void paint(Graphics g) {
javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();
midpGraphics.setClip(g.getTranslateX() + g.getClipX(),
g.getTranslateY() + g.getClipY(), g.getClipWidth(), g.getClipHeight());

midpGraphics.translate(getX() + g.getTranslateX(), getY() +
g.getTranslateY());

inner.paint(midpGraphics);

midpGraphics.translate(-getX() - g.getTranslateX(), -getY() -
g.getTranslateY());

}

public void setWidth(int width) {
super.setWidth(width);
inner.setWidth(width);
}

public void setHeight(int height) {
super.setHeight(height);
inner.setHeight(height);
}

public void keyPressed(int keyCode) {
if(Display.getInstance().getGameAction(keyCode) ==
Display.GAME_FIRE){
setHandlesInput(!handlesInput());
}
inner.keyPressed(keyCode);
}

public void keyReleased(int keyCode) {
inner.keyReleased(keyCode);
}
}

3) here is sample code to how to use the code above:

Form test = new Form("test");
test.setLayout(new BorderLayout());
test.addComponent(BorderLayout.CENTER, new CanvasWrapper(new
MyCanvas()));
test.show();

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> Hi, Mr. Chen:
> May you show the source for that ?
> Or LWUIT may provide the utility for us to use ?
>
> Thanks
> Regards
> [Message sent by forum member 'javatai' (javatai)]
>
> http://forums.java.net/jive/thread.jspa?messageID=278668
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

javatai
Offline
Joined: 2005-06-21

Thanks , I got your code with this.

And I also test the original Display object
it works !!

Thank you
Regards
Anson

vilastalewale
Offline
Joined: 2007-02-22

Hi javatai
As u mentioned this code works fine.
But I am getting error at this point
javax.microedition.lcdui.Graphics midpGraphics = g.getGraphics();

getGraphics() is not public and u cannot acccess from outside as someone mentioned that it is protected method not public so i cannot access so how shouid i resolve this problem.

public void paint(Graphics g)
{
javax.microedition.lcdui.Graphics midpGraphics = getGraphics();

midpGraphics.setClip(g.getTranslateX() + g.getClipX(), g.getTranslateY() + g.getClipY(), g.getClipWidth(), g.getClipHeight());

midpGraphics.translate(getX() + g.getTranslateX(), getY() + g.getTranslateY());

inner.paint(midpGraphics);

midpGraphics.translate(-getX() - g.getTranslateX(), -getY() - g.getTranslateY());

}
Thanking Regards
Vilas Talewale