Link to home
Start Free TrialLog in
Avatar of bganoush
bganoush

asked on

Local Bounds in Java Applets...


I have a class that extends a Canvas. I draw into this canvas by overriding the paint(Graphics g) method. At the start of this method, I get the bounds by calling getBounds() and then I use the width and height to figure out where to draw. So far, I have assumed that the local drawing bounds are from 0,0 to width,height... Will that always be the case?

Also, I tried to call drawRect of the coordinates of the bounds rectangle but it is offset according to the canvas' location within its container Panel... Is there another method I can use to get the Canvas' "local" bounds?

-- Bubba
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

> Will that always be the case?

Are you calling canvas.getBounds()?

Can you post your code?
Avatar of bganoush
bganoush

ASKER


Here is some code... First, the code that creates the main panel (that contains a buttons panel and a display panel):

    Panel detailPanel = new Panel();
    detailPanel.setLayout(new BorderLayout());
    detailPanel.setBackground(Color.lightGray);
    detailPanel.setSize (600, 150);

    Panel detailButtons = new Panel();
    detailButtons.add(new Label("Detail"));
    detailButtons.add(displayButton);

    detailCanvas= new DetailCanvas(someData);
    detailCanvas.setSize(600, 115);

    detailPanel.add(detailButtons , "North");
    detailPanel.add(detailCanvas, "Center");

And here is the beginning of the paint method in class: DetailCanvas

        public void paint(Graphics g)
        {
                Rectangle theRect = getBounds();
                int bTop = 0; //theRect.y;
                int bLeft = 0; //theRect.x;
                int bBottom = theRect.y + theRect.height;
                int bRight = theRect.x + theRect.width;
                int bWidth = theRect.width;
                int bHeight = theRect.height;
                int barWidth = theRect.width * 2 / 3;

                g.drawRect(bLeft + 5, bTop + 2, bWidth - 10, bHeight - 10);
                g.setColor(Color.white);
                g.fillRect(bLeft + 5, bTop + 2, bWidth - 10, bHeight - 10);
                g.setColor(Color.black);

...

-- Bubba
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial








                                                                       o!





Did it work?

Cool ;-)

Tim

Yeah that works...

Thanks!
getBounds returns the position of the component on its parent :-)