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); } }