By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

awt canvas on a swing form okay?

P: n/a
hi
1) i plan on having an awt canvas component (to draw graphs) on a JFrame
with other swing components..will this be okay? i've read that swing and
awt aren't compatible..
2)Also, if i have a function which simply loops infinately that is part of a
form, what happens when a form event (eg button click) occurs? will the
event handler be executed simulatanesouly with the loop? or does the event
excecute first and then the loop is continued? or what?

eg.

public static void main() // a main functino inside a JFrame. creates
//form with components and then enters an infinate loop
{
init_form(); //create a form and components
while (1==1) //execute infinately after creating form
{ }

}
//
//
event_Handler() //event handler for a button on the form...eg for a button
//click event
{
does something..
}

thanks,

Joseph
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi All,
Thanks for the advice on how to draw graphics using swing..i didn't know
that it could be done on a JPanel or JComponent..i read the 2d tutorial and
it said to use an AWT canvas..now i know what to do, i'll just use a JPanel
or JComponent to draw a simple graph on..

regarding the loop issue, my main algorithm was doing this:

- on program start-up create swing gui form with swing components..including
a close and start button.
-then immediately continually scan port for a high voltage..this will be a C
function (interfaced through Java API) that runs forever until it gets the
high signal..but i need to do other things if certain buttons are pressed
at the same time while scanning..eg the close button should shut the app..

so my question was asking how would the Java environment handle it all..will
the event handlers for the buttons execute at the same time with the
function which polls the pc port...so i wanted to know whether the loop
will keep running and the gui still be able to respond to events (eg.
button click events,)..i'm not totaly sure, but i think the event handlers
will run simultaneously with the loop..ie. even though my app isn't
multi-threaded, the java environment is...i'll be looking more into it
though..

thanks
joseph


Joseph wrote:
hi
1) i plan on having an awt canvas component (to draw graphs) on a JFrame
with other swing components..will this be okay? i've read that swing and
awt aren't compatible..
2)Also, if i have a function which simply loops infinately that is part of
a
form, what happens when a form event (eg button click) occurs? will the
event handler be executed simulatanesouly with the loop? or does the
event
excecute first and then the loop is continued? or what?

eg.

public static void main() // a main functino inside a JFrame. creates
//form with components and then enters an infinate loop
{
init_form(); //create a form and components
while (1==1) //execute infinately after creating form
{ }

}
//
//
event_Handler() //event handler for a button on the form...eg for a
button //click event
{
does something..
}

thanks,

Joseph


Jul 17 '05 #2

P: n/a
Joseph <ka****@bigpond.com> wrote in message news:<rG*******************@news-server.bigpond.net.au>...
hi
1) i plan on having an awt canvas component (to draw graphs) on a JFrame
with other swing components..will this be okay? i've read that swing and
awt aren't compatible..
In theory they should be. In practice they usually aren't. Try using
a JComponent subclass instead, with paintComponent() .

2)Also, if i have a function which simply loops infinately that is part of a
form, what happens when a form event (eg button click) occurs? will the
event handler be executed simulatanesouly with the loop? or does the event
excecute first and then the loop is continued? or what?


The answer is yes and no. Normally events are fired on a separate event
processing thread, but if your code is sat in a tight loop without
defering to other threads, your loop will likely strangle the event
thread, choking it of the oxygen it needs to run - at least on some
platforms.
-FISH- ><>
Jul 17 '05 #3

P: n/a
FISH wrote:
Joseph <ka****@bigpond.com> wrote in message news:<rG*******************@news-server.bigpond.net.au>...
hi
1) i plan on having an awt canvas component (to draw graphs) on a JFrame
with other swing components..will this be okay? i've read that swing and
awt aren't compatible..

In theory they should be.


No, not even in theory. AWT components are always and necessarily opaque whereas
a Swing component can be wholly or partially transparent.

Jul 17 '05 #4

P: n/a
Michael Borgwardt <br****@brazils-animeland.de> wrote in
news:c3*************@ID-161931.news.uni-berlin.de:
FISH wrote:
Joseph <ka****@bigpond.com> wrote in message
news:<rG*******************@news-server.bigpond.net.au>...
hi
1) i plan on having an awt canvas component (to draw graphs) on a
JFrame with other swing components..will this be okay? i've read that
swing and awt aren't compatible..

In theory they should be.


No, not even in theory. AWT components are always and necessarily opaque
whereas a Swing component can be wholly or partially transparent.


I must not be understanding you correctly. Subclasses of Component can be
as opaque or as transparent as their paint(Graphics) and update(Graphics)
methods draw them to be. Certain objects (Canvas, Panel, and their
subclasses) use native windows and thus are forced to be opaque, but not
all AWT components.

Instead of an AWT Canvas, how about a Swing JCanvas ?
Note that http://java.sun.com/products/jfc/tsc...ing/index.html
says:
"In fact, because both the AWT and Swing component sets use the same AWT
infrastructure, it's possible to mix both kinds of components in the same
program -- a technique that allows for phased migration of applications."

Read the referenced web page further to find out the rules and the
limitations. However, it assumes that AWT components are heavyweight (and
thus opaque). In fact, AWT has had lightweight (potentially transparent)
components since JDK 1.1. Thus, replace its references to AWT and Swing by
references to heavyweights (Canvas, Panel, JWindow, JFrame, JDialog,
JApplet and their subclasses), and lightweights (Component, Container, all
Swing components [except for JWindow, JFrame, JDialog, and JApplet], and
their subclasses). See also
http://java.sun.com/j2se/1.3/docs/gu...htweights.html
for more information.

--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Jul 17 '05 #5

P: n/a
Hi all,
thanks alot for the suggestions...the behavior I predicted, that the EDT
would run simultaneously with the polling function is true...i don't know
for certain whether the EDT has enough priority for the whole thing to run
smoothly (ie respond properly to buttonclicks), but people here seem to
think that i'm better off forking a thread each time i need to poll, giving
it a priority based on how quickly i want the app to respond to the high
signal..

thank you
Joseph

Joseph wrote:
Hi All,
Thanks for the advice on how to draw graphics using swing..i didn't know
that it could be done on a JPanel or JComponent..i read the 2d tutorial
and it said to use an AWT canvas..now i know what to do, i'll just use a
JPanel or JComponent to draw a simple graph on..

regarding the loop issue, my main algorithm was doing this:

- on program start-up create swing gui form with swing
components..including a close and start button.
-then immediately continually scan port for a high voltage..this will be a
C function (interfaced through Java API) that runs forever until it gets
the high signal..but i need to do other things if certain buttons are
pressed at the same time while scanning..eg the close button should shut
the app..

so my question was asking how would the Java environment handle it
all..will the event handlers for the buttons execute at the same time with
the function which polls the pc port...so i wanted to know whether the
loop will keep running and the gui still be able to respond to events (eg.
button click events,)..i'm not totaly sure, but i think the event handlers
will run simultaneously with the loop..ie. even though my app isn't
multi-threaded, the java environment is...i'll be looking more into it
though..

thanks
joseph

Joseph wrote:
hi
1) i plan on having an awt canvas component (to draw graphs) on a JFrame
with other swing components..will this be okay? i've read that swing and
awt aren't compatible..
2)Also, if i have a function which simply loops infinately that is part
of a
form, what happens when a form event (eg button click) occurs? will the
event handler be executed simulatanesouly with the loop? or does the
event
excecute first and then the loop is continued? or what?

eg.

public static void main() // a main functino inside a JFrame. creates
//form with components and then enters an infinate loop
{
init_form(); //create a form and components
while (1==1) //execute infinately after creating form
{ }

}
//
//
event_Handler() //event handler for a button on the form...eg for a
button //click event
{
does something..
}

thanks,

Joseph


Jul 17 '05 #6

P: n/a
hi
just to let u all know i did a little test and the gui seemed to be
completely responsive..ie the EDT ran fine even though the main thread had
a while loop in it...although, when the while loop updated graphics on the
screen, it couldn't handle it (because the update graphics methods actually
occur in the EDT as well)..so long as my main thread only polls the ports
(continually) and doesn't write to the screen it should be fine..i plan on
making my OO design compatible with both, alhtough i don't think i'll even
need another thread..

c yas
joe

test details (pesudo):
public static main ()
{
while (true) //infinately occupy cpu
{
calculate some numbers
}

}
.....

button_event()
{
bring up a simple dialog //for test purposes, as the events will reuire
//dialog boxes
}

i guess my initial prediction (that the priority of the EDT is high enough)
was true after all :-)

Joseph wrote:
hi
1) i plan on having an awt canvas component (to draw graphs) on a JFrame
with other swing components..will this be okay? i've read that swing and
awt aren't compatible..
2)Also, if i have a function which simply loops infinately that is part of
a
form, what happens when a form event (eg button click) occurs? will the
event handler be executed simulatanesouly with the loop? or does the
event
excecute first and then the loop is continued? or what?

eg.

public static void main() // a main functino inside a JFrame. creates
//form with components and then enters an infinate loop
{
init_form(); //create a form and components
while (1==1) //execute infinately after creating form
{ }

}
//
//
event_Handler() //event handler for a button on the form...eg for a
button //click event
{
does something..
}

thanks,

Joseph


Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.