Solved

Local Bounds in Java Applets...

Posted on 2004-08-18
8
207 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:bganoush
ID: 11831232








                                                                       o!





0
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
expectj telnet failing 5 45
difference between sorce folder and folder in eclipise 3 29
passing enum to a method 4 25
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

808 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