[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Local Bounds in Java Applets...

Posted on 2004-08-18
8
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

649 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