Solved

Local Bounds in Java Applets...

Posted on 2004-08-18
8
206 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 185
xampp tool 12 48
hibernate insert example 13 28
SHA2 certs for IIS AND Java? 2 81
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

786 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