Why does getBounds return 0?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    Why does getBounds return 0?

    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:
    [code=java]
    import java.applet.App let;
    import java.awt.*;
    import java.awt.event. *;

    public class MoveIt extends Applet implements ActionListener
    {
    private Image cupImage;
    private int iTop = 15;
    private int iLeft = 15;
    private Canvas drawingCanvas = new Canvas();
    public void init()
    {
    cupImage = getImage(getDoc umentBase(), "cup.gif");
    this.setLayout( new BorderLayout()) ;
    this.setSize(30 0, 300);
    this.setBackgro und(Color.BLUE) ;
    drawingCanvas.s etBackground(Co lor.BLUE);
    this.add(drawin gCanvas, BorderLayout.NO RTH);
    Panel keyPanel = new Panel();
    keyPanel.setLay out(new BorderLayout()) ;
    Button upButton = new Button("Up");
    keyPanel.add(up Button,BorderLa yout.NORTH);
    upButton.addAct ionListener(thi s);
    Button centerButton = new Button("Center" );
    keyPanel.add(ce nterButton,Bord erLayout.CENTER );
    centerButton.ad dActionListener (this);
    Button downButton = new Button("Down");
    keyPanel.add(do wnButton,Border Layout.SOUTH);
    downButton.addA ctionListener(t his);
    Button leftButton = new Button("Left");
    keyPanel.add(le ftButton,Border Layout.WEST);
    leftButton.addA ctionListener(t his);
    Button rightButton = new Button("Right") ;
    keyPanel.add(ri ghtButton,Borde rLayout.EAST);
    rightButton.add ActionListener( this);
    this.add(keyPan el, BorderLayout.SO UTH);
    }
    public void paint(Graphics g)
    {
    g.drawImage(cup Image, iLeft, iTop, this);
    }
    public void actionPerformed (ActionEvent e)
    {
    String sItem = e.getActionComm and();
    char cFirst = sItem.charAt(0) ;
    switch (cFirst)
    {
    case 'C':
    Rectangle rCanvas = drawingCanvas.g etBounds();
    int iImageWidth = cupImage.getWid th(this);
    int iImageHeight = cupImage.getHei ght(this);
    iLeft = (rCanvas.width - iImageWidth)/2;
    iTop = (rCanvas.height - iImageHeight)/2;
    break;
    case 'U':
    iTop -= 15;
    break;
    case 'D':
    iTop += 15;
    break;
    case 'L':
    iLeft -= 15;
    break;
    case 'R':
    iLeft += 15;
    break;
    }
    repaint();
    }
    }
    [/code]
    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
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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(bl ah blah blah)), but I'm not sure if that implicitly sets drawingCanvas' size as well. To check that, throw a System.out.prin tln() 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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        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

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by SammyB
          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

          Comment

          Working...