Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: set x and y scale to be triangle?

  1. #1
    dahc is offline Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default set x and y scale to be triangle?

    I was learning about the StdDraw method in class the other day and we looked at bouncing balls code to where balls bouncing inside a box.
    Java Code:
    public class BouncingBalls { 
    
        public static void main(String[] args) {
    
            // number of bouncing balls
            int n = Integer.parseInt(args[0]);
    
            // set boundary to box with coordinates between -1 and +1
            StdDraw.setXscale(-1.0, +1.0);
            StdDraw.setYscale(-1.0, +1.0);
    
            // create an array of n random balls
            Ball[] balls = new Ball[n];
            for (int i = 0; i < n; i++)
                balls[i] = new Ball();
    
            // do the animation loop
            StdDraw.enableDoubleBuffering();
            while (true) {
    
                // move the n balls
                for (int i = 0; i < n; i++) {
                    balls[i].move();
                }
    
                // draw the n balls
                StdDraw.clear(StdDraw.GRAY);
                StdDraw.setPenColor(StdDraw.BLACK);
                for (int i = 0; i < n; i++) {
                    balls[i].draw();
                }
                StdDraw.show();
                StdDraw.pause(20);
            }
        }
    }
    However, I was wondering how we could change this to have the balls bounce inside a triangle, rather than a box. Any thoughts? And to take it even further, how could we modify it to have the boundary for the bouncing balls not only to be the box but have it bounce off some triangle within the box?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: set x and y scale to be triangle?

    What package is the StdDraw class in? I don't recognize it as part of the Java SE classes.
    You will have to read the class's API doc to see what methods it has that might be useful for your project.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dahc is offline Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default Re: set x and y scale to be triangle?

    I'm not 100% sure, I just saw the code for it on princeton's book website http://introcs.cs.princeton.edu/java...alls.java.html

    http://introcs.cs.princeton.edu/java.../index.php#1.5
    Last edited by dahc; 03-03-2017 at 02:22 AM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: set x and y scale to be triangle?

    Did you read the API doc for the StdDraw class's methods? Are there any that look like they might be useful?

    Is the a forum site for the StdDraw class where you could ask questions about how to use it?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: set x and y scale to be triangle?

    I don't understand why different organizations (ACM, Princeton, et al) feel compelled to write their own graphics libraries. Unless it is something really special (e.g. gaming libraries) I would think that native Java would suffice and serve the student better.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: set x and y scale to be triangle?

    I guess kids like to make bouncing ball games and classes like StdDraw allow them to quickly write a 30 line program to do that.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. TIFF dpi scale
    By enrico in forum Java 2D
    Replies: 1
    Last Post: 01-31-2012, 09:01 AM
  2. Color scale
    By harshab in forum AWT / Swing
    Replies: 1
    Last Post: 06-10-2011, 05:12 PM
  3. how to scale correctly ?
    By h9h in forum Java 2D
    Replies: 10
    Last Post: 10-29-2009, 07:06 AM
  4. How to use Scale in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:49 PM

Tags for this Thread

Posting Permissions

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