473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Why does getBounds return 0?

SammyB
807 Expert 512MB
Our homework called for us to move a gif around in a canvas in an applet. Since it is an intro course, they used constants but I'm thinking, what if the user resizes the applet? So, I wrote the following code:
Expand|Select|Wrap|Line Numbers
  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class MoveIt extends Applet implements ActionListener
  6. {
  7.    private Image cupImage;
  8.    private int iTop = 15;
  9.    private int iLeft = 15;
  10.    private Canvas drawingCanvas = new Canvas();
  11.    public void init()
  12.    {
  13.       cupImage = getImage(getDocumentBase(), "cup.gif");
  14.       this.setLayout(new BorderLayout());
  15.       this.setSize(300, 300);
  16.       this.setBackground(Color.BLUE);
  17.       drawingCanvas.setBackground(Color.BLUE);
  18.       this.add(drawingCanvas, BorderLayout.NORTH);
  19.       Panel keyPanel = new Panel();
  20.       keyPanel.setLayout(new BorderLayout());
  21.       Button upButton = new Button("Up");
  22.       keyPanel.add(upButton,BorderLayout.NORTH);
  23.       upButton.addActionListener(this);
  24.       Button centerButton = new Button("Center");
  25.       keyPanel.add(centerButton,BorderLayout.CENTER);
  26.       centerButton.addActionListener(this);
  27.       Button downButton = new Button("Down");
  28.       keyPanel.add(downButton,BorderLayout.SOUTH);
  29.       downButton.addActionListener(this);
  30.       Button leftButton = new Button("Left");
  31.       keyPanel.add(leftButton,BorderLayout.WEST);
  32.       leftButton.addActionListener(this);
  33.       Button rightButton = new Button("Right");
  34.       keyPanel.add(rightButton,BorderLayout.EAST);
  35.       rightButton.addActionListener(this);
  36.       this.add(keyPanel, BorderLayout.SOUTH);
  37.    }
  38.    public void paint(Graphics g)
  39.    {
  40.       g.drawImage(cupImage, iLeft, iTop, this);
  41.    }
  42.    public void actionPerformed(ActionEvent e)
  43.    {
  44.       String sItem = e.getActionCommand();
  45.       char cFirst = sItem.charAt(0);
  46.       switch (cFirst)
  47.       {
  48.       case 'C':
  49.          Rectangle rCanvas = drawingCanvas.getBounds();
  50.          int iImageWidth = cupImage.getWidth(this);
  51.          int iImageHeight = cupImage.getHeight(this);
  52.          iLeft = (rCanvas.width - iImageWidth)/2;
  53.          iTop = (rCanvas.height - iImageHeight)/2;
  54.          break;
  55.       case 'U':
  56.          iTop -= 15;
  57.          break;
  58.       case 'D':
  59.          iTop += 15;
  60.          break;
  61.       case 'L':
  62.          iLeft -= 15;
  63.          break;
  64.       case 'R':
  65.          iLeft += 15;
  66.          break;
  67.       }
  68.       repaint();
  69.    }
  70. }
  71.  
My problem is in the Case 'C': rCanvas.height is zero! Why is that? How should I get the height & width of the Canvas. Also, I would like for the gif to be centered initially, but I don't know how to get its size without displaying it. Note, just use any small gif for cup.gif; my gif is 69x85. TIA. --Sam
Oct 29 '07 #1
4 3572
Ganon11
3,652 Expert 2GB
I haven't done very much work at all with Java graphics...but it looks like drawingCanvas never actually gets a size. Your class gets a size (when you call this.setSize(blah blah blah)), but I'm not sure if that implicitly sets drawingCanvas' size as well. To check that, throw a System.out.println() line there, if you can, with drawingCanvas' height and/or width, just to check that it is being initialized properly.

As far as centering the image, your code for getting the center point looks good. So now you have the center point, and you have the dimensions of the image (by using it's getWidth/getHeight/getBounds method) - it should be a simple task to get the top left corner point of the image.

I'm not sure why you can't get the image's dimensions before displaying it - once you get the image in init(), you should be able to access the dimensions and place it as expected.
Oct 29 '07 #2
JosAH
11,448 Expert 8TB
As far as I can see you put a keyPanel in the SOUTH part of the Applet and a
Canvas in the NORTH part, but there's nothing in the Canvas itself, so the height
can be zero while the width will be equal to the width of the keyPanel. If you
increase the height of the entire Applet the keyPanel will 'eat up' that height.

Check the setMinimumSize and the setPreferedSize methods for the Canvas.

kind regards,

Jos
Oct 29 '07 #3
SammyB
807 Expert 512MB
Or, just delete the Canvas! :oD>>>
The buttons don't seem to resize their height, so everthing works fine without the Canvas. The only unanswerable question is why the book said to put it in! We have never used the Canvas, so it's a mystery.

The good news is that next chapter we switch to Swing.

Thanks for your help! --Sam
Oct 29 '07 #4
JosAH
11,448 Expert 8TB
Or, just delete the Canvas! :oD>>>
The buttons don't seem to resize their height, so everthing works fine without the Canvas. The only unanswerable question is why the book said to put it in! We have never used the Canvas, so it's a mystery.

The good news is that next chapter we switch to Swing.

Thanks for your help! --Sam
Also note that your 'paint()' method is your Applet's paint() method so you're
not drawing on te canvas, you're drawing somewhere on the Applet itself.

kind regards,

Jos
Oct 30 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
2
by: news frontiernet.net | last post by:
I have key entered and tried to run example 4-6 from Dany Goodmans DYNAMIC HTML book, version one that is on pages 94-96. This is part of my effort to learn JavaScript. I checked each byte and...
4
by: Anon Email | last post by:
Hi people, Another question. In the code below, why is there a screen output? This part of the code: bool status = TheCalculator.Execute(input); is merely assigning the result of the...
3
by: garyusenet | last post by:
I've noticed that the media player window takes up the same percentage of the screen regardless of the screen resolution, it's dimensions aren't fixed in pixels but it takes up a set proportion of...
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
13
by: Protoman | last post by:
I'm getting an error: 10 C:\Dev-Cpp\Enigma.cpp no match for 'operator<' in 'i < (+cleartext)->std::basic_string<_CharT, _Traits, _Alloc>::end ()' Code: Enigma.hpp...
1
by: HarishAdea | last post by:
Hi, I am trying to run the JAVA pgm, but it is giving error as "selection does not contain a main type". The filename is "ScoreLeadSummary.java" when i try to run it or debug,it gives the pop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.