473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

awt canvas on a swing form okay?

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
6 6396
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dominic | last post by:
Hi all, I was referring to http://forum.java.sun.com/thread.jsp?forum=57&thread=204595 to handle the resizing problem. So I put the canvas into a JScrollPane, and then add the pane into the...
6
by: Oz Mortimer | last post by:
Hi, Is there any way that I can scroll a canvas - i.e. if there are too many items on the canvas you can still access by pressing down. I know Form does this (apparently) but I need to do it...
0
by: Airborne | last post by:
I am a newbie. In fact I am in school working on my Java project. Could someone please explain to me how to load a form from another form. I have a main form and from that form I want to show...
0
by: Chua Wen Ching | last post by:
Hi there. I had couple of Java codes with me that i not sure how would i code this in C#. I always use jcreator without ide. So a bit confuse over c#. Okay!
37
by: Geoff Jones | last post by:
Hi Suppose I have two forms: Form1 and Form2. I create and show an instance of Form2 in the code of Form1 e.g. Dim myForm2 = New Form2 myForm2.Show() How do I tell myForm2 that Form1 is...
11
by: Aaron Gray | last post by:
Hi, I have put together a bit of JavaScript to make a square resizable canvas :- http://www.aarongray.org/Test/JavaScript/resizable.html Problems I have :- a) I cannot seem to center it...
6
by: Nebulism | last post by:
I have been attempting to utilize a draw command script that loads a canvas, and through certain mouse events, draws rectangles. The original code is from...
1
by: DemonFox | last post by:
Hello to all I need to merge my code with the PaintBox component. the question is: i have a form with edit box where you put the number (data) of the node you want to insert ant it comes to the...
4
by: =?GB2312?B?0rvK18qr?= | last post by:
Hi all, Today I was writing a simple test app for a video decoder library. I use python to parse video files and input data to the library. I got a problem here, I need a windows form, and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.