Re: Questions about drawing on Canvas



Zerex71 wrote:
Greetings,

I am attempting for the first time to do some drawing on a canvas.
What I would like to do is draw a standard set of three orthogonal
axes and then superimpose a line (vector) on the canvas as well, to
aid in visualizing some rotations. The questions I have are as
follows:

1. If I want the vectors to have any thickness, it sounds like I am
going to have to call drawRect() instead of drawLine() - true or
false?

See Stroke.

2. In my reading, I have seen code examples where they seem to extend
JPanel (I don't know why such a graphical object would subclass from a
panel, so I'm a bit puzzled by that) and override its paintComponent()
method. Why do you subclass from JPanel to draw rectangles, etc.?

Convention. You can use JComponent and some people recommend you do. JComponent and JPanel which extends it are both Containers as well. Sometimes you want to be able to add components to your GUI. Canvas is not a Container. Be sure to read the docs for Canvas.

3. Must you do everything to each thing you want to draw that you
would do to other container components (e.g. my canvas) such as
frame.add(canvas) and canvas.setVisible(true), etc.?

You can add the Canvas to a Container (eg. Frame or Panel) before you set the primary Frame visible. You should pack() or setSize() on the container to get it to layout before you set it visible.

4. When you do a setColor(color), it seems like it's just telling the
Graphics object "you're about to do something and I want you to do it
with this color). Is that correct?

Yes.

Why can you get away with not
specifying the fg and bg colors each time (for example, if you want to
draw a red rectangle with a black border, I have seen in that example
where they make two calls to setColor())?

Because that's not the way it works. In your paint() method you set the color you wish to draw with and then call the drawing method. If you want to change colors, set a new color and draw again.

5. Is there a specific order to packing and adding things before
showing them? I'm a little confused on what the absolute last thing
is that you need to do when you want to show your combined UI. (In
other words, I have a frame which contains the canvas, the axes, and
the vector, and so far I can only get the canvas to be drawn.)

setVisible(true) is the last call on your GUI container. Are you drawing in the paint() method? For AWT components (Frame, Panel, Canvas etc.) all drawing must occur in the paint() method. For Swing components it is the paintComponent() method.

I am hoping there are some simple answers here but unfortunately none
of the Java tutorials I have seen on Sun or in my manuals I printed
seem to provide any concrete answers. If you know of any good
websites that really explain the graphics paradigm well, I'm open to
hearing about them.

Post some simple code that demonstrates the problem you are having. It must be compilable (unless you are asking about compile errors).

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.



Relevant Pages

  • Re: Questions about drawing on Canvas
    ... I am attempting for the first time to do some drawing on a canvas. ... Why do you subclass from JPanel to draw rectangles, ... // TODO Find out what effect removing these LOC will have. ...
    (comp.lang.java.programmer)
  • Re: Questions about drawing on Canvas
    ... axes and then superimpose a line on the canvas as well, ... Why do you subclass from JPanel to draw rectangles, ... // TODO Find out what effect removing these LOC will have. ...
    (comp.lang.java.programmer)
  • Re: Connectors in Drawing toolbar grayed out
    ... To draw an arrow, you select ... independent of the canvas, you have to move it outside the canvas, then delete ... just like drawing an ordinary arrow. ... Connectors are supposed to be available only ...
    (microsoft.public.word.application.errors)
  • Re: Newbie looking for info on basic graphics with Delphi.
    ... panel objuct on the main form. ... some of them draw on their parent's canvas. ... I think all of Windows' ...
    (comp.lang.pascal.delphi.misc)
  • Re: Newbie looking for info on basic graphics with Delphi.
    ... Do I need to use a canvas to draw? ... Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ... Unlimited Access, Anonymous Accounts, Uncensored Broadband Access ...
    (comp.lang.pascal.delphi.misc)