Drawing on panel Object

L

lrantisi

I am using the Panel object to represent a drawing area that the user
can use to draw different shapes.
I s there a clear example about how to draw points on the surface on
the Panel object.
Thanks
 
W

Wesley Hall

lrantisi said:
I am using the Panel object to represent a drawing area that the user
can use to draw different shapes.
I s there a clear example about how to draw points on the surface on
the Panel object.
Thanks


Firstly, don't use Panel. Panels are designed as containers for other
components, not generic drawing areas. The class you are looking for is
java.awt.Canvas which IS designed as a generic drawing area.

Secondly, what you need to do is overide the paint method of Canvas...
something like this...

public class DrawingCanvas
{

public void paint(Graphics graphics)
{
//manipulate graphics object here
}

}

The Graphics object contains all the methods you will need to draw onto
the canvas.

Check your API docs under java.awt.Graphics for a list of all the
methods available for your drawing needs.
 
L

lrantisi

Thank you for your answer.
But I am originally using an Applet. My Panel was put on the surface of
the Applet.
Now how can I put the Canvas on the Applet instead of the Panel. The
Canvas is not one of the components that can be created by (Darg-Drop).
I am using Exclipse software from IBM. I checked the components window,
there is no canvas to (drag-drop) over the Applet face, as it is the
case with the Panel.
Thanks
..
 
W

Wesley Hall

See below...

" Thank you for your answer.
But I am originally using an Applet. My Panel was put on the surface of
the Applet.
Now how can I put the Canvas on the Applet instead of the Panel. The
Canvas is not one of the components that can be created by (Darg-Drop).
I am using Exclipse software from IBM. I checked the components window,
there is no canvas to (drag-drop) over the Applet face, as it is the
case with the Panel.
Thanks "

Please post your replies underneath rather than on top. Posting at the
top is (unsurprisingly) known as 'top-posting' and is frowned upon
because it makes it more difficult to see context as you read from top
to bottom.

Why not add the canvas to your panel? :)

Set your panel's layout manager to 'BorderLayout', and add the canvas
using BorderLayout.CENTER. To do this... first go the constructor of
your panel and make sure the constructor says...

Panel panel = new Panel(new BorderLayout());

....you can probably do this by setting the layout manager using eclipse,
I wouldn't know :)

Now you can create your canvas instance and add it to the panel...

Canvas canvas = new DrawingCanvas();
panel.add(canvas, BorderLayout.CENTER);

....and there you go.

If you are confused on how to do this kind of GUI creation in code
rather than through eclipse, you have just discovered why it is a bad
idea to learn Swing/AWT development with GUI editing tools. With the
kind of code you are writing you will not be able to do everything
through eclipse so it is time to get your hands dirty and learn what is
really going on. You will be a much better developer for it.

I happen to use a GUI editing tool for any Swing GUIs I write (with is
rare nowdays), but because I know I can open the code and do it by hand
if the shit hits the fan, for me a GUI editor is a tool rather than a
crutch.

Break away, break free, and rise above all those VB monkeys :eek:P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top