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?
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.
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?
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 ); } } }
Trying to get an applet to switch between golden ratio and random shapes using one Jbutton
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.
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
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.
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.
ImyMTD (May 5th, 2013)
ImyMTD (May 5th, 2013)