Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help

    Hello, I have a problem with a shape generator. It randoms the shapes just fine, I just do not know how to make all the shapes fully stay within the frames border.

    RandomShapeMaker.java
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import java.util.Random;
    public class RandomShapeMaker extends JPanel{
        private static final Random randomShape = new Random(); 
        public void paintComponent( Graphics g ){
            super.paintComponent( g );
            int width = getWidth();
            int height = getHeight();
            int shape;//holding the shape
            Color colour;//holding the colour
            int x;//holding x coordinate
            int y;//holding y coordinate
            int shapeWidth;
            int shapeHeight;
            for( int i = 0; i < 100; i++ ){
                //determine the coordinates and size of shapes to draw
                shape = decideShape();          
                colour = decideColour();
                x = decideCoordinates( width );
                y = decideCoordinates( height );
                shapeWidth = decideSize( width );
                shapeHeight = decideSize( height );
                g.setColor(decideColour());             
                //switch statement for shape
                switch( shape ){
                    case 1:                     
                        g.fillOval( x, y, shapeWidth, shapeHeight );
                        break;              
                    case 2:
                        g.fillRect( x, y, shapeWidth, shapeHeight );
                        break;
                }//shape switch         
            }//end of for loop                  
        }//end of paintComponent method
        //choose randomly the shape, 1 for oval, 2 for rectangle        
        public static int decideShape(){
            int theShape = 1 + randomShape.nextInt(2);
        //  System.out.printf("Shape value returned is %d\n", theShape);    
            return theShape;    
        }
        //choose randomly the colour 
        public static Color decideColour(){
            Color theColour = new Color(randomShape.nextInt(256), randomShape.nextInt(256), randomShape.nextInt(256));
            //System.out.printf("Colour value returned is %d\n", theColour);
            return theColour;
        }
        //choose random coordinates, x and y depending on the parameter passed to it
        public static int decideCoordinates( int origin ){
            int coordinate = randomShape.nextInt( origin + 1 );
            //System.out.printf("Coordinate value returned is %d\n", coordinate);
            return coordinate;
        }
        //choose random side lenghts
        public static int decideSize(int dimension){
            int side = randomShape.nextInt( dimension / 2 );
            //System.out.printf("Coordinate value returned is %d\n", side);
            return side;    
        }
    }//end of class Shapes

    ShapeGenerator.java
    import javax.swing.JFrame;
    public class ShapeGenerator{    
        public static void main( String[] args){
                RandomShapeMaker panel = new RandomShapeMaker();
                JFrame application = new JFrame();
                application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                application.add( panel );
                application.setSize( 1500, 800 );
                application.setVisible( true );
            }
        }

    Regards,
    Jing

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,633
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Need help

    how to make the whole shapes stay in the frame.
    I'm not sure what you are asking. Do you know how to compare the bounds of the shape with the bounds of the frame?
    Can you change the code to make sure that bounds of the shape stay within the bounds of the frame?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help

    Quote Originally Posted by Norm View Post
    I'm not sure what you are asking. Do you know how to compare the bounds of the shape with the bounds of the frame?
    Can you change the code to make sure that bounds of the shape stay within the bounds of the frame?
    Thanks for fast answer. I get what you mean but I'm not sure how to compare the bounds of my shapes. Any suggestions?

    EDIT:

    Can I use this for my code or would you give me any other suggestions?
    public Rectangle getBounds() {
             return shape.getBounds();

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,633
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Need help

    Get the right bound's value and the bottom bound's y value for the frame.
    When creating a shape compare the shape's x + width value against the x for the frame and the shape's y + height value against the y for the frame.
    Skip using those values (x, width, y and height) if the above tests fail.
    If you don't understand my answer, don't ignore it, ask a question.