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 9 of 9

Thread: Can Anyone Point me in the right direction?

  1. #1
    Junior Member ImyMTD's Avatar
    Join Date
    May 2013
    Location
    Canterbury
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Can Anyone Point me in the right direction?

    Doing an assignment for my university course based on an applet guessing game, using random shapes and shapes that are golden. Am i right to use a case/switch statement for this? I.E. Different cases for random shape and golden ratio shape?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can Anyone Point me in the right direction?

    Quote Originally Posted by ImyMTD View Post
    Am i right to use a case/switch statement for this?
    What are you doing? From what you said so far, maybe. Post your code or explain more about what you are trying to accomplish by using a switch

  3. #3
    Junior Member ImyMTD's Avatar
    Join Date
    May 2013
    Location
    Canterbury
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Can one Jbutton control several switch statements?

    My project requires me to create a game which produces random and golden ratio rectangles when the user presses one button, i have attempted to use a swich statement here so when the user clicks the programme will either draw a rectangle or a golden ratio rectange but when the button is clicked it only draws the 1st case is there anyway to get it to alternate between cases? Or should i be using an if else statement

    public class firstAttempt extends JApplet
    {
        private JButton start[];
        private String controls[] = { "Start"};
        private String guess[] = {  "Golden", "Normal" };
        private JPanel buttonPanel;
        private DrawPanel drawingArea;
        private int width = 500, height = 500;
        private double width_int;
        private double height_int;
        private double ratio;
        private double goldenX;
     
        public void init()
        {
            drawingArea = new DrawPanel ( width, height );
            start = new JButton [controls.length ];
            buttonPanel = new JPanel ();
            buttonPanel.setLayout (new GridLayout ( 1, controls.length ));
            ButtonHandler handler = new ButtonHandler();
     
            for (int i = 0; i < controls.length; i++ )
            {
                   start[ i ] = new JButton ( controls [ i ] );
                    buttonPanel.add ( start [ i ]);
                    start[ i ].addActionListener (handler);
            }
     
            Container c = getContentPane();
            c.add ( buttonPanel, BorderLayout.SOUTH);
            c.add ( drawingArea, BorderLayout.CENTER);
     
     
        }
     
     private class ButtonHandler implements ActionListener
     {
         public void actionPerformed ( ActionEvent e)
         {
             for ( int i =0; i< start.length; i++ )
                if (e.getSource () == start [ i ] )
                {
                    drawingArea.setCurrentChoice ( i );
                    break;
                }
            }
        }
        class DrawPanel extends JPanel
        {
            private int currentChoice = -1;
            private int width = 100, height=100;
            public DrawPanel ( int w, int h)
            {
                width = ( w>= 0 ? w :100 );
                height = (h>= 0 ? h : 100);
            }
     
            public void paintComponent( Graphics g )
         {
             super.paintComponent( g );
     
             switch( currentChoice ) 
             {
                 case 0:
                     g.fillRect( randomX(), randomY(), randomX(), randomY() );
                     break;
                 case 1:
                     g.drawRect( goldenX(), randomY(), randomX(), randomY() );
                     break;
     
             }
         }
             public void setCurrentChoice( int c )
         {
             currentChoice = c;
             repaint();
         }
         private int randomX()
         { 
             return (int) ( Math.random() * width ); 
         }
     
         private int randomY()
         { 
             return (int) ( Math.random() * height ); 
     
         }
     
         private int goldenX()
         {
           double ratio = (Math.sqrt(5.0)+1)/2;
           goldenX = height_int*ratio;
           width_int = (int) width;
           height_int = (int) height;
           return (int) ( Math.random()* width );
        }
     
    }
    }

  4. #4
    Junior Member ImyMTD's Avatar
    Join Date
    May 2013
    Location
    Canterbury
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can Anyone Point me in the right direction?

    Trying to get an applet to switch between golden ratio and random shapes using one Jbutton

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can Anyone Point me in the right direction?

    Can you explain what problems you are having and ask some questions about them? Your last post was a statement not a question.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member ImyMTD's Avatar
    Join Date
    May 2013
    Location
    Canterbury
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can Anyone Point me in the right direction?

    My project requires me to create a game which produces random and golden ratio rectangles when the user presses one button, i have attempted to use a swich statement here so when the user clicks the programme will either draw a rectangle or a golden ratio rectange but when the button is clicked it only draws the 1st case is there anyway to get it to alternate between cases? Or should i be using an if else statement

    import java.awt.*;
    import java.util.Random;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
     
    public class firstAttempt extends JApplet
    {
        private JButton start[];
        private String controls[] = { "Start"};
        private String guess[] = {  "Golden", "Normal" };
        private JPanel buttonPanel;
        private DrawPanel drawingArea;
        private int width = 500, height = 500;
        private double width_int;
        private double height_int;
        private double ratio;
        private double goldenX;
     
        public void init()
        {
            drawingArea = new DrawPanel ( width, height );
            start = new JButton [controls.length ];
            buttonPanel = new JPanel ();
            buttonPanel.setLayout (new GridLayout ( 1, controls.length ));
            ButtonHandler handler = new ButtonHandler();
     
            for (int i = 0; i < controls.length; i++ )
            {
                   start[ i ] = new JButton ( controls [ i ] );
                    buttonPanel.add ( start [ i ]);
                    start[ i ].addActionListener (handler);
            }
     
            Container c = getContentPane();
            c.add ( buttonPanel, BorderLayout.SOUTH);
            c.add ( drawingArea, BorderLayout.CENTER);
     
     
        }
     
     private class ButtonHandler implements ActionListener
     {
         public void actionPerformed ( ActionEvent e)
         {
             for ( int i =0; i< start.length; i++ )
                if (e.getSource () == start [ i ] )
                {
                    drawingArea.setCurrentChoice ( i );
                    break;
                }
            }
        }
        class DrawPanel extends JPanel
        {
            private int currentChoice = -1;
            private int width = 100, height=100;
            public DrawPanel ( int w, int h)
            {
                width = ( w>= 0 ? w :100 );
                height = (h>= 0 ? h : 100);
            }
     
            public void paintComponent( Graphics g )
         {
             super.paintComponent( g );
     
             switch( currentChoice ) 
             {
                 case 0:
                     g.fillRect( randomX(), randomY(), randomX(), randomY() );
                     break;
                 case 1:
                     g.drawRect( goldenX(), randomY(), randomX(), randomY() );
                     break;
     
             }
         }
             public void setCurrentChoice( int c )
         {
             currentChoice = c;
             repaint();
         }
         private int randomX()
         { 
             return (int) ( Math.random() * width ); 
         }
     
         private int randomY()
         { 
             return (int) ( Math.random() * height ); 
     
         }
     
         private int goldenX()
         {
           double ratio = (Math.sqrt(5.0)+1)/2;
           goldenX = height_int*ratio;
           width_int = (int) width;
           height_int = (int) height;
           return (int) ( Math.random()* width );
        }
     
    }
    }
    Last edited by ImyMTD; May 3rd, 2013 at 08:59 AM. Reason: Added import codes

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can Anyone Point me in the right direction?

    The posted code is missing the import statements and can't be compiled for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can Anyone Point me in the right direction?

    How can the variable: currentChoice ever get a value other than 0?

    Try doing some debugging by adding some println statements to the code to show where currentChoice is given a value and be sure to print out all the variables that control how currentChoice gets a value.
    How can currentChoice ever get a value of 1? What values will the variables need to have for that to happen?
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    ImyMTD (May 5th, 2013)

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can Anyone Point me in the right direction?

    Quote Originally Posted by ImyMTD View Post
    is there anyway to get it to alternate between cases?
    Yes, several. You might use a boolean like isGolden to determine if the rectangle is golden or not, and decide how to draw it in the same manner.
    Quote Originally Posted by ImyMTD View Post
    Or should i be using an if else statement
    You could do what you are doing either way

  11. The Following User Says Thank You to jps For This Useful Post:

    ImyMTD (May 5th, 2013)

Similar Threads

  1. Replies: 10
    Last Post: April 21st, 2013, 09:28 AM
  2. Can anyone point me in the right direction dealing with bytes
    By derekxec in forum Java Theory & Questions
    Replies: 5
    Last Post: August 18th, 2012, 01:44 PM
  3. HI WORLD! Can anyone point a complete novice in the right direction??
    By Irish-novice in forum Member Introductions
    Replies: 4
    Last Post: June 26th, 2012, 05:16 PM
  4. [SOLVED] Someone point me to the right direction..
    By ineedhelp in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 30th, 2011, 10:03 PM
  5. Direction,
    By Time in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 21st, 2010, 05:21 PM

Tags for this Thread