Solved

Local Bounds in Java Applets...

Posted on 2004-08-18
8
204 Views
Last Modified: 2010-03-31

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
0
Comment
Question by:bganoush
  • 5
  • 3
8 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11829967
> Will that always be the case?

Are you calling canvas.getBounds()?

Can you post your code?
0
 

Author Comment

by:bganoush
ID: 11831054

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
0
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 11831147
ahhh

does getSize() fix it?

assume top left is always 0, 0, and getSize should return you the height and width...
0
 

Author Comment

by:bganoush
ID: 11831232








                                                                       o!





0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:TimYates
ID: 11831272
?!?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11831277
Did it work?

Cool ;-)

Tim
0
 

Author Comment

by:bganoush
ID: 11831309

Yeah that works...

Thanks!
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11831316
getBounds returns the position of the component on its parent :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now