10 Replies Latest reply: Mar 19, 2010 1:18 PM by 843807 RSS

    pulling my hair out

    843807
      I've wasted the past... I don't know how many hours, and I'm starting to worry I've been going about this all wrong. Any kind of guidance would be much appreciated:

      I have a canvas onto which is being drawn stuff. anything, it doesn't really matter. that part works great.

      the canvas is the only object on the window, and it takes up the entire window.

      onto this canvas I need to place some UI elements. buttons, text fields, text areas with scroll bars, etc.

      my first attempt: I am not very handy with layouts, so I set out to learn about them. with some mucking around I was able to get the window/canvas to work within a setLayout(null) layout, positioning the canvas within, hard-setting various sizes... etc. but when I tried to put a text field on top, I could not get it to display. I tried mucking with setComponentZOrder in various ways, etc, etc, to no avail.

      my second attempt: I searched the internet for information, and discovered many forum posts saying that "high-level" elements and "low-level" elements were not allowed to occupy the same space. every single one seemed concerned with development for cellphones, however I am developing strictly for the desktop at this time, and while I have never developed for mobile devices, I am sure they come with many more restrictions than the desktop. all the same, I could find no other information other than "best to try and code your own UI elements". I did this for a while, but while buttons are easy to do, scrolling text areas are not, and I abandoned it. "this is stupid - Sun has given me all of the perfect UI elements. if we can tell Windows 'hey, draw this window on top of this window', then we should really be able to tell Sun 'hey, draw this element on top of this element.'..." I did more internet searches, but still could not find any information on laying, say, a TextField, or a JTextField on top of a canvas, except from cellphone developers frustrated by the same problem >_>

      my third attempt: I searched for custom UI libraries designed for rendering on Graphics, Graphics2D, Canvas... if you really-really can't ask Java to put a text field on top of a Canvas - and even if you can - someone has surely written a custom UI library, which will render buttons and text fields and scroll bars and everything else on a Canvas. however, I have been unable to locate such a thing.

      my fourth attempt: posting here :P I'm beginning to worry, from the lack of "why can't I do this?" and third-party UI libraries, that, in fact, you can, and I'm missing something obvious. some other weird layout manager thingum... I don't know.

      I've been mucking with this all night, and would like to now pass out and sleep. sleep would probably also help at this point. but any external help would be much much appreciated.
        • 1. This Thread is now moved
          PhHein
          Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
          • 2. Re: pulling my hair out
            843807
            After all that I'm still not sure what you're asking exactly.

            You don't put other components on a Canvas by the way. A canvas is supposed to be just a blank slate to draw on. If you're trying to add GUI widgets, you probably want a Panel.

            AWT is kind of old hat at this point and you might be better off using Swing.

            [edit]
            My recommendation to ask on the AWT forum is rendered moot.

            Edited by: paulcw on Mar 18, 2010 11:13 AM
            • 3. Re: pulling my hair out
              DarrylBurke
              Learn to read the API. A Canvas is not a Container. You need a class that inherits from Container. Paul has pointed you to Panel. From the documentation for Panel
              Panel is the simplest container class. A panel provides space in which an application can attach any other component, including other panels.

              I second the recommendation to abandon AWT and learn Swing instead. Any future Swing related questions should be posted in the [Swing forum|http://forums.sun.com/forum.jspa?forumID=57], where you wlil find a link to the Swing tutorial.

              db
              • 4. Re: pulling my hair out
                843807
                telkoth wrote:
                I've wasted the past... I don't know how many hours, and I'm starting to worry I've been going about this all wrong. Any kind of guidance would be much appreciated:
                You don't code much do you nor study engineering? Hours and hours of study and trying is very common.

                As stated JPanels will work for what you are asking to do, you may even get them into the proper configuration if you use the appropriate layouts.
                • 5. Re: pulling my hair out
                  843807
                  sorry if my initial post was confusing/... of a frustrated tone. I will blame lack of sleep. perhaps I should have waited to post.

                  anyway! the use of a JFrame instead of a Canvas appears to have done the trick, with a few other modifications to the code - brilliant. it's not my own code, but it's pretty readable, and I feel like java function names are in most places pretty clear.

                  the whole "container" method of doing things is not particularly... natural or familiar to me. it surprises me that it's so difficult, or in some cases impossible, to just tell Java "put this object at (x, y) and be done with it," but whatever: if Java says "use my containers," then I'll learn to use it's containers. I guess that was my initial frustration. in some ways, it's still hard to believe that some things can't be done that way.

                  I was also surprised to learn that Canvas is apparently an older/out-of-date object. in all the reading I've been doing of graphical applications, Canvas seems to be the object of choice, but I suppose these articles are old. I am using Eclipse, which seems to do a great job of telling me when something is deprecated (many articles, I've noticed, handle threads in poor ways, using .stop(), etc)... but are there any guides/manuals available that can recommend "don't use X, use Y instead?" I notice increasingly that Java has many, many different ways to do things, with no clear indication of which is better suited to the task, and/or which are just so old as to be avoided!

                  sorry again about my frantic post, and thanks for putting up with me :P
                  • 6. Re: pulling my hair out
                    843807
                    telkoth wrote:
                    sorry if my initial post was confusing/... of a frustrated tone. I will blame lack of sleep. perhaps I should have waited to post.

                    anyway! the use of a JFrame instead of a Canvas appears to have done the trick, with a few other modifications to the code - brilliant. it's not my own code, but it's pretty readable, and I feel like java function names are in most places pretty clear.

                    the whole "container" method of doing things is not particularly... natural or familiar to me. it surprises me that it's so difficult, or in some cases impossible, to just tell Java "put this object at (x, y) and be done with it," but whatever: if Java says "use my containers," then I'll learn to use it's containers. I guess that was my initial frustration. in some ways, it's still hard to believe that some things can't be done that way.
                    Alas, it can be: it's called AbsoluteLayout--or No Layout--or Null Layout, depends on where you read about it. In most cases it's called bad programming: you absolutely loose the ability to resize easily. (I cannot believe I just said that--me the Absolute Layout Evangelist of the late 90's).
                    I was also surprised to learn that Canvas is apparently an older/out-of-date object. in all the reading I've been doing of graphical applications, Canvas seems to be the object of choice, but I suppose these articles are old. I am using Eclipse, which seems to do a great job of telling me when something is deprecated (many articles, I've noticed, handle threads in poor ways, using .stop(), etc)... but are there any guides/manuals available that can recommend "don't use X, use Y instead?" I notice increasingly that Java has many, many different ways to do things, with no clear indication of which is better suited to the task, and/or which are just so old as to be avoided!
                    There is a lot of people writing books--many are recently published, but not really up to date. The Java Tutorials are great--I prefer the "Really Big Tutorial".
                    • 7. Re: pulling my hair out
                      DarrylBurke
                      Just go through the tutorials and you'll find everything you need to know to get going.
                      [http://java.sun.com/docs/books/tutorial/]

                      db
                      • 8. Re: pulling my hair out
                        843807
                        this is now slightly tangential, but an opinion on my approach would be nice... I worry my approach is insane/the wrong way to do things for Java.

                        again, my thought is to draw custom graphics/interface/etc, and stick java UI objects on top of it - why reinvent all those lovely JTextFields, etc...

                        so I have something like this now, which largely works, thanks to your feedback :)

                        1. a JPanel, which is being drawn to via getGraphics() and all that; a loop of "do logic, render graphics, draw" (<-- this method is familiar to me, coming from a non-Java background, but I worry I should be using Java's paintComponent() and related functions, rather than ignoring them)

                        2. add()'d to the JPanel, various Swing objects: JTextField, JTextArea, etc, etc.

                        there's a bit of flicker which I'm having trouble sorting out, and I wonder if it's due to how Java wants to render things... if the Swing objects are getting rendered separately, then my JPanel, etc, flicker-flicker. is this a point at which I can also not reinvent the wheel... should I be using paintComponent() etc instead of a custom "logic, render, draw" loop? <-- is such a loop guaranteed to interfere with the normal rendering of other Swing objects? (my render/draw process basically goes "draw all the JPanel stuff; paintComponents(graphics); graphics.dispose(); graphics = (Graphics2D)getGraphics();")
                        • 9. Re: pulling my hair out
                          DarrylBurke
                          telkoth wrote:
                          1. a JPanel, which is being drawn to via getGraphics()
                          Never use getGraphics() for obtaining a Graphics reference of a component. Never, never, never.
                          and all that; a loop of "do logic, render graphics, draw" (<-- this method is familiar to me, coming from a non-Java background, but I worry I should be using Java's paintComponent() and related functions, rather than ignoring them)
                          You've been given a link to the tutorials. There's a section on Performing Custom Painting.
                          there's a bit of flicker which I'm having trouble sorting out, and I wonder if it's due to how Java wants to render things
                          No, it's due to your wrong approach.
                          ... if the Swing objects are getting rendered separately, then my JPanel, etc, flicker-flicker. is this a point at which I can also not reinvent the wheel... should I be using paintComponent()
                          Yes.
                          etc instead of a custom "logic, render, draw" loop? <-- is such a loop guaranteed to interfere with the normal rendering of other Swing objects?
                          Yes.

                          You've also been given a link to the Swing forum and advised to post Swing related questions there.

                          db
                          • 10. Re: pulling my hair out
                            843807
                            ^ knowing I've got the wrong idea is certainly valuable - heh. if I need more help along these lines, I'll be sure to start a new thread in a more appropriate area of the forums. thanks again.