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: Java beginner - help needed!

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java beginner - help needed!

    I am taking a Java class and I am pretty lost.

    The assignment is:
    create a JFrame that will hold a series of 5 panels. The first 4 panels will randomly show different shapes (oval, circle, and square). The fifth panel will display the word “JACKPOT” if all 4 of the other panels contain a square and will display the word “RATS” if they have any other combination

    I keep track of how many of each shape appear, yet I get some weird incorrect results when I use System.out.println() to try to debug it. I also do not know if I need to display JACKPOT or RATS on the final panel as I can't get the results back to my main jackpot.java file.

    Help?

    jackpot.java:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
    public class jackpot{
     
    	public static void main(String [] args){
     
    		JFrame theGUI = new JFrame();
    		theGUI.setTitle("Test your luck!");
    		theGUI.setSize(750,180);
     
    		theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		Container pane = theGUI.getContentPane();
    		pane.setLayout(new GridLayout(1,5));
     
    		for(int i =1; i <= 4; i++){
     
    			Random gen = new Random();
     
    			int ran = gen.nextInt(3);
     
    			ColorPanel panel = new ColorPanel(Color.black);
    			pane.add(panel);
    		}
     
    		JPanel panel = new JPanel();
    		panel.setBackground(Color.black);
    		pane.add(panel);
     
    		theGUI.setVisible(true);
    	}
    }

    ColorPanel.java:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
    public class ColorPanel extends JPanel{
     
    	int rect = 0, oval = 0, circle = 0;	
     
    	public ColorPanel(Color backColor){
    		setBackground(backColor);
    	}
     
    	public void paintComponent(Graphics g){
     
    		super.paintComponent(g);
     
    		int width = getWidth() - 1;
    		int height = getHeight() - 1;
     
    		g.setColor(Color.white);
     
    		Random gen = new Random();
     
    		int ran = gen.nextInt(3);		
     
    		if(ran==0){
    			g.fillRect(0,0,width-5,height);
    			rect++;
    		}else if(ran==1){
    			g.fillOval(0,5,width-5,height/2);
    			oval++;
    		}else{
    			g.fillOval(0,0,width-5,height);
    			circle++;
    		}
     
    		//g.setColor(Color.white);
    		//g.drawString("RATS",0,0);
     
    	}	
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java beginner - help needed!

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Please respect Java's naming conventions and title your classes with names that begin with capital letters.

    All data is (or could be) created in your main() method, so you don't really need to get something "back" from somewhere else. Choose the shapes drawn in the JPanels based on random numbers generated in the main() method which are passed to the ColorPanel() constructor. Keep track of how many of each random number is generated. If the random number generator results in 3 of the same numbers, you have a winner, otherwise a loser.

    Other points:

    Move the bulk of your code from the main() method to a constructor and supporting methods. I recommend you add another class, "OneArmedBandit" (you pick the name). The Test class should kickoff the OneArmedBandit class that then uses the ColorPanel class to display the results. Separate the logic (OneArmedBandit) from the GUI (ColorPanel).

    Do not generate a new Random object each time through the loop. One Random object will do and will help the Random illusion. Seed the Random() constructor. (Refer to the Random API).

    Add an attribute to your ColorPanel class that defines the shape and/or text contained in the panel.

    Remove most logic (Random, ifs, etc.) from inside the paintComponent() method, reducing it to the logic needed to paint the panel.

  3. #3

    Default Re: Java beginner - help needed!

    what help do you want

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java beginner - help needed!

    Old thread is closed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Beginner. Help needed.
    By Jack1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2013, 12:18 AM
  2. Java Programming for beginner HELP needed ...
    By Vinayaka Sp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2013, 09:26 AM
  3. Very beginner level code help needed.
    By dcalabro in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 06:26 PM
  4. Help needed for beginner. Im so confused
    By walrus212 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 14th, 2011, 01:47 PM
  5. beginner-urgent java help needed
    By joeyyusiyi22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 10:43 AM