Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Changing color of canvas ...
Changing color of canvas ... [message #443896] Mon, 04 October 2004 13:07 Go to next message
Eclipse UserFriend
Originally posted by: hs.cybertec.at

I have a very ugly problem with canvas.
All I want to do is to change the colorof a canvas when a button is
pressed (for two seconds or so).
However, nothing happens ...
I have googled the internet but I haven't found something useful.

What I am doing is:

final Button relay_on_off_2 = new Button(out_group, SWT.NONE);
relay_on_off_2.setBounds(155, 15, 50, 25);
relay_on_off_2.setText("on / off");
relay_on_off_2.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
openRelay(2);
}
});



/* open a relay for 2 seconds */
private void openRelay(int x)
{
int hex = 0x53 + x;
CommandFrame fr = new CommandFrame();
String data = "1";
fr.compileData(data.getBytes(), hex, (byte) 0x11);
Elog.Elog("sending data to relay (open) ...");

CommandFrame fr2 = new CommandFrame();
String data2 = "0";
fr2.compileData(data2.getBytes(), hex, (byte) 0x11);

try
{
out.write(fr.extractAll());

Color x4 = v_rel_status_1.getBackground();
x4.dispose();
v_rel_status_1.setBackground(new Color(this.display, 100, 100, 100));

Thread.sleep(2000);

x4 = v_rel_status_1.getBackground();
x4.dispose();
v_rel_status_1.setBackground(new Color(this.display, 255, 0, 0));
out.write(fr2.extractAll());

Elog.Elog("sending data to relay (close) ...");
}
catch (Exception e)
{
reportError("Cannot send data to device: " + e.getMessage());
Elog.Elog("sending data to relay (close) ...");
}
}


I have tried canvas.redraw() and so forth - even shell.redraw() did not
help.
Does anybody have an idea?

best regards,

Hans
Re: Changing color of canvas ... [message #443906 is a reply to message #443896] Mon, 04 October 2004 14:18 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Try calling Control.update() after you change the color to force outstanding
paint events for the control to be delivered right away rather than from the
event loop.

"Hans-Juergen Schoenig" <hs@cybertec.at> wrote in message
news:cjrhgd$5qk$1@eclipse.org...
> I have a very ugly problem with canvas.
> All I want to do is to change the colorof a canvas when a button is
> pressed (for two seconds or so).
> However, nothing happens ...
> I have googled the internet but I haven't found something useful.
>
> What I am doing is:
>
> final Button relay_on_off_2 = new Button(out_group, SWT.NONE);
> relay_on_off_2.setBounds(155, 15, 50, 25);
> relay_on_off_2.setText("on / off");
> relay_on_off_2.addSelectionListener(new SelectionAdapter()
> {
> public void widgetSelected(SelectionEvent e)
> {
> openRelay(2);
> }
> });
>
>
>
> /* open a relay for 2 seconds */
> private void openRelay(int x)
> {
> int hex = 0x53 + x;
> CommandFrame fr = new CommandFrame();
> String data = "1";
> fr.compileData(data.getBytes(), hex, (byte) 0x11);
> Elog.Elog("sending data to relay (open) ...");
>
> CommandFrame fr2 = new CommandFrame();
> String data2 = "0";
> fr2.compileData(data2.getBytes(), hex, (byte) 0x11);
>
> try
> {
> out.write(fr.extractAll());
>
> Color x4 = v_rel_status_1.getBackground();
> x4.dispose();
> v_rel_status_1.setBackground(new Color(this.display, 100, 100, 100));
>
> Thread.sleep(2000);
>
> x4 = v_rel_status_1.getBackground();
> x4.dispose();
> v_rel_status_1.setBackground(new Color(this.display, 255, 0, 0));
> out.write(fr2.extractAll());
>
> Elog.Elog("sending data to relay (close) ...");
> }
> catch (Exception e)
> {
> reportError("Cannot send data to device: " + e.getMessage());
> Elog.Elog("sending data to relay (close) ...");
> }
> }
>
>
> I have tried canvas.redraw() and so forth - even shell.redraw() did not
> help.
> Does anybody have an idea?
>
> best regards,
>
> Hans
>
>
>
>
Re: Changing color of canvas ... [message #443918 is a reply to message #443906] Mon, 04 October 2004 15:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: hs.cybertec.at

I have tried that as well but somehow it did not help me at all ...
Now I have ...

Color x4 = v_rel_status_1.getBackground();
x4.dispose();
v_rel_status_1.setBackground(new Color(this.display, 100, 100, 100));
shell.update();

Thread.sleep(2000);

.... somehow this does not change anything :(.

Hans



Steve Northover wrote:
> Try calling Control.update() after you change the color to force outstanding
> paint events for the control to be delivered right away rather than from the
> event loop.
>
> "Hans-Juergen Schoenig" <hs@cybertec.at> wrote in message
> news:cjrhgd$5qk$1@eclipse.org...
>
>>I have a very ugly problem with canvas.
>>All I want to do is to change the colorof a canvas when a button is
>>pressed (for two seconds or so).
>>However, nothing happens ...
>>I have googled the internet but I haven't found something useful.
>>
>>What I am doing is:
>>
>>final Button relay_on_off_2 = new Button(out_group, SWT.NONE);
>>relay_on_off_2.setBounds(155, 15, 50, 25);
>>relay_on_off_2.setText("on / off");
>>relay_on_off_2.addSelectionListener(new SelectionAdapter()
>>{
>>public void widgetSelected(SelectionEvent e)
>>{
>>openRelay(2);
>>}
>>});
>>
>>
>>
>>/* open a relay for 2 seconds */
>>private void openRelay(int x)
>>{
>>int hex = 0x53 + x;
>>CommandFrame fr = new CommandFrame();
>>String data = "1";
>>fr.compileData(data.getBytes(), hex, (byte) 0x11);
>>Elog.Elog("sending data to relay (open) ...");
>>
>>CommandFrame fr2 = new CommandFrame();
>>String data2 = "0";
>>fr2.compileData(data2.getBytes(), hex, (byte) 0x11);
>>
>>try
>>{
>>out.write(fr.extractAll());
>>
>>Color x4 = v_rel_status_1.getBackground();
>>x4.dispose();
>>v_rel_status_1.setBackground(new Color(this.display, 100, 100, 100));
>>
>>Thread.sleep(2000);
>>
>>x4 = v_rel_status_1.getBackground();
>>x4.dispose();
>>v_rel_status_1.setBackground(new Color(this.display, 255, 0, 0));
>>out.write(fr2.extractAll());
>>
>>Elog.Elog("sending data to relay (close) ...");
>>}
>>catch (Exception e)
>>{
>>reportError("Cannot send data to device: " + e.getMessage());
>>Elog.Elog("sending data to relay (close) ...");
>>}
>>}
>>
>>
>>I have tried canvas.redraw() and so forth - even shell.redraw() did not
>>help.
>>Does anybody have an idea?
>>
>>best regards,
>>
>>Hans
>>
>>
>>
>>
>
>
>
Re: Changing color of canvas ... [message #443926 is a reply to message #443896] Mon, 04 October 2004 16:02 Go to previous messageGo to next message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
Just a Opignon:
I didn't check in detail your code but generally, when you are in Button
Selection function is as result of the main loop
while(!shell.isDisposed()) display.readAndDispatch().
Normally, any message you send to system isn't really executed till you
don't leave the Button Function, letting the loop to proceed these events.

If you want something like that, or you have to set a MouseListener to
your button, or you have to empty the Windows message queue (do a
while(button.getShell().getDisplay().readAndDispatch()); ).

Regards,
Tiberiu

Hans-Juergen Schoenig wrote:

> I have a very ugly problem with canvas.
> All I want to do is to change the colorof a canvas when a button is
> pressed (for two seconds or so).
> However, nothing happens ...
> I have googled the internet but I haven't found something useful.

> What I am doing is:

> final Button relay_on_off_2 = new Button(out_group, SWT.NONE);
> relay_on_off_2.setBounds(155, 15, 50, 25);
> relay_on_off_2.setText("on / off");
> relay_on_off_2.addSelectionListener(new SelectionAdapter()
> {
> public void widgetSelected(SelectionEvent e)
> {
> openRelay(2);
> }
> });



> /* open a relay for 2 seconds */
> private void openRelay(int x)
> {
> int hex = 0x53 + x;
> CommandFrame fr = new CommandFrame();
> String data = "1";
> fr.compileData(data.getBytes(), hex, (byte) 0x11);
> Elog.Elog("sending data to relay (open) ...");

> CommandFrame fr2 = new CommandFrame();
> String data2 = "0";
> fr2.compileData(data2.getBytes(), hex, (byte) 0x11);

> try
> {
> out.write(fr.extractAll());

> Color x4 = v_rel_status_1.getBackground();
> x4.dispose();
> v_rel_status_1.setBackground(new Color(this.display, 100, 100, 100));

> Thread.sleep(2000);

> x4 = v_rel_status_1.getBackground();
> x4.dispose();
> v_rel_status_1.setBackground(new Color(this.display, 255, 0, 0));
> out.write(fr2.extractAll());

> Elog.Elog("sending data to relay (close) ...");
> }
> catch (Exception e)
> {
> reportError("Cannot send data to device: " + e.getMessage());
> Elog.Elog("sending data to relay (close) ...");
> }
> }


> I have tried canvas.redraw() and so forth - even shell.redraw() did not
> help.
> Does anybody have an idea?

> best regards,

> Hans
Re: Changing color of canvas ... [message #443929 is a reply to message #443918] Mon, 04 October 2004 16:29 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
WORKSFORME

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class EclipseCorner {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
Button button = new Button (shell, SWT.PUSH);
button.setText ("Change Color");
button.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
Color color = display.getSystemColor (SWT.COLOR_RED);
shell.setBackground (color);
shell.update ();
System.out.print ("Sleeping ...");
try {Thread.sleep (5000);} catch (Throwable th) {};
System.out.print ("done");
shell.setBackground (null);
}
});
button.pack();
shell.setSize(200, 200);
shell.open();

while (!shell.isDisposed()) {
if (display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}

"Hans-Juergen Schoenig" <hs@cybertec.at> wrote in message
news:416168B7.7030004@cybertec.at...
> I have tried that as well but somehow it did not help me at all ...
> Now I have ...
>
> Color x4 = v_rel_status_1.getBackground();
> x4.dispose();
> v_rel_status_1.setBackground(new Color(this.display, 100, 100, 100));
> shell.update();
>
> Thread.sleep(2000);
>
> ... somehow this does not change anything :(.
>
> Hans
>
>
>
> Steve Northover wrote:
> > Try calling Control.update() after you change the color to force
outstanding
> > paint events for the control to be delivered right away rather than from
the
> > event loop.
> >
> > "Hans-Juergen Schoenig" <hs@cybertec.at> wrote in message
> > news:cjrhgd$5qk$1@eclipse.org...
> >
> >>I have a very ugly problem with canvas.
> >>All I want to do is to change the colorof a canvas when a button is
> >>pressed (for two seconds or so).
> >>However, nothing happens ...
> >>I have googled the internet but I haven't found something useful.
> >>
> >>What I am doing is:
> >>
> >>final Button relay_on_off_2 = new Button(out_group, SWT.NONE);
> >>relay_on_off_2.setBounds(155, 15, 50, 25);
> >>relay_on_off_2.setText("on / off");
> >>relay_on_off_2.addSelectionListener(new SelectionAdapter()
> >>{
> >>public void widgetSelected(SelectionEvent e)
> >>{
> >>openRelay(2);
> >>}
> >>});
> >>
> >>
> >>
> >>/* open a relay for 2 seconds */
> >>private void openRelay(int x)
> >>{
> >>int hex = 0x53 + x;
> >>CommandFrame fr = new CommandFrame();
> >>String data = "1";
> >>fr.compileData(data.getBytes(), hex, (byte) 0x11);
> >>Elog.Elog("sending data to relay (open) ...");
> >>
> >>CommandFrame fr2 = new CommandFrame();
> >>String data2 = "0";
> >>fr2.compileData(data2.getBytes(), hex, (byte) 0x11);
> >>
> >>try
> >>{
> >>out.write(fr.extractAll());
> >>
> >>Color x4 = v_rel_status_1.getBackground();
> >>x4.dispose();
> >>v_rel_status_1.setBackground(new Color(this.display, 100, 100, 100));
> >>
> >>Thread.sleep(2000);
> >>
> >>x4 = v_rel_status_1.getBackground();
> >>x4.dispose();
> >>v_rel_status_1.setBackground(new Color(this.display, 255, 0, 0));
> >>out.write(fr2.extractAll());
> >>
> >>Elog.Elog("sending data to relay (close) ...");
> >>}
> >>catch (Exception e)
> >>{
> >>reportError("Cannot send data to device: " + e.getMessage());
> >>Elog.Elog("sending data to relay (close) ...");
> >>}
> >>}
> >>
> >>
> >>I have tried canvas.redraw() and so forth - even shell.redraw() did not
> >>help.
> >>Does anybody have an idea?
> >>
> >>best regards,
> >>
> >>Hans
> >>
> >>
> >>
> >>
> >
> >
> >
>
Previous Topic:Editable Masks
Next Topic:Problem with SWT Tree, TreeItem
Goto Forum:
  


Current Time: Sat Apr 27 04:06:35 GMT 2024

Powered by FUDForum. Page generated in 0.02922 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top