Results 1 to 10 of 10
  1. #1
    ludis is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default resizing a rectangle?

    public void drawFrame()
    {
    myCanvas.getSize();
    myCanvas.setForegroundColor(Color.black);
    Rectangle rect = (new Rectangle(20,20,WIDTH - 20, HEIGHT - 20));
    myCanvas.draw(rect);
    }

    Im trying to get the rectangle to change with the size of the canvas, when the canvas's size is changed, and leaving a 20px edge. However its only producing two line not a full rectangle? Any help be much appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Is this Swing? You may wish to create and post an SSCCE, and give us more details on your problem for quicker, better help. Please see the link for more info.

  3. #3
    ludis is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    No im using blueJ and have imported the java.awt if that helps :)

  4. #4
    ludis is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    import java.awt.Color;
    import java.awt.Rectangle;
    import java.awt. Font;

    /**
    * Class BallDemo - provides a demonstration of the
    * BouncingBall and Canvas classes.
    *

    */

    public class BallDemo
    {
    private Canvas myCanvas;
    private static final int WIDTH = 600;
    private static final int HEIGHT = 500;

    /**
    * Create a BallDemo object.
    * Creates a fresh canvas and makes it visible.
    */
    public BallDemo()
    {
    myCanvas = new Canvas("Ball Demo", WIDTH, HEIGHT);
    myCanvas.setVisible(true);
    }

    /**
    * Simulate two bouncing balls
    */
    public void bounce()
    {
    int ground = 400; // position of the ground line
    int xStart = 50; // x-start of the ground line
    int xLimit = 550; // x-limit of the ground line

    myCanvas.setVisible(true);

    // draw the ground
    myCanvas.setForegroundColor(Color.blue);
    myCanvas.drawLine(xStart, ground, xLimit, ground);

    // crate and show the balls
    BouncingBall ball = new BouncingBall(xStart, 50, 16, Color.blue, ground, myCanvas);
    ball.draw();
    BouncingBall ball2 = new BouncingBall(xStart + 20, 80, 20, Color.red, ground, myCanvas);
    ball2.draw();

    // Make them bounce until both have gone beyond the xLimit.
    boolean finished = false;
    while(!finished) {
    myCanvas.wait(50); // small delay
    ball.move();
    ball2.move();
    // stop once ball has travelled a certain distance on x axis
    if(ball.getXPosition() >= xLimit && ball2.getXPosition() >= xLimit) {
    finished = true;
    }
    }
    ball.erase();
    ball2.erase();
    }

    /**
    * Draw frame method...!
    */
    public void drawFrame()
    {
    myCanvas.getSize();
    myCanvas.setForegroundColor(Color.black);
    Rectangle rect = (new Rectangle(20,20,this.WIDTH - 20, this.HEIGHT - 20));
    myCanvas.draw(rect);
    }


    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ludis View Post
    No im using blueJ
    bluej is just the development environment you are using and has nothing to do with your current Java problem.

    and have imported the java.awt if that helps :)
    OK, so you're using the AWT library which is important.

    Still without more information and an SSCCE, about all I can do is wish you well.

    Edit:
    I see that you've posted code :)
    ... but without code tags, so it's hard to read. :(

    Let me look at it...

    Edit 2:
    And the code isn't an SSCCE (you didn't read the link I posted in my first reply, did you?), and so I can't compile, run or test the code. :(

    Anyway, best of luck.
    Last edited by Fubarable; 12-11-2010 at 09:24 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Posted in private message:

    Quote Originally Posted by ludis
    Hello im not to sure on how to break it down for you, as im kind of new to this.

    I could email you the files, so you could compile?

    Cheers
    Tell you what. Instead either create a JAR file with source code and convert the extension from jar to zip and post it here, or else simply place all the source files into a zip file and post it here in the forum (it allows text and zip file uploads). Also, most of us prefer all communication to be made through the forum, not through private messaging.

  7. #7
    ludis is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Ok here you go i think i know whats happening now just, cant solve it. I believe the Dimension object need int values and my accessor method is return values of type double.
    Attached Files Attached Files

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  9. #9
    ludis is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    I just run the package.blueJ and it all comes up! In BlueJ of course....!

  10. #10
    ludis is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Please help, ive been stuck on this problem for the last 3 days :(

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 12:29 AM
  2. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 04:39 PM
  3. button not resizing in awt
    By alinaqvi90 in forum AWT / Swing
    Replies: 1
    Last Post: 08-17-2010, 04:43 PM
  4. panel resizing
    By simo_mon in forum AWT / Swing
    Replies: 1
    Last Post: 08-15-2009, 03:09 PM
  5. Image Resizing and DPi
    By Rob_ in forum Java Applets
    Replies: 4
    Last Post: 11-27-2008, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •