1

For a game I'm making, I want the player to begin at a menu screen(Panel05), and then click a button to start the actual game(Panel00). And, when playing the game, if they win or lose, when they click another button they either go back to the menu or go onto another level. Right now, the panels are all separate programs, with their own drivers, and I'm unsure on how to get one panel to work inside another, if that is at all possible. I would appreciate any and all advice, answers, or criticism given. Below is the panel for the menu

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;


public class Panel05 extends JPanel
{
 private BufferedImage myImage;
 private Graphics myBuffer;
 public JButton button1;

public Panel05()
{

  myImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
  myBuffer = myImage.getGraphics();




  setLayout(null);


  button1 = new JButton();
  button1.setSize(100, 100);
  button1.setLocation(500,500);
  button1.setForeground(Color.WHITE);
  button1.setFont(new Font("Serif", Font.BOLD, 30));
  button1.setText("Start");
  button1.addActionListener(new B1Listener());
  button1.setBorder(null);
  button1.setOpaque(false);
  button1.setContentAreaFilled(false);
  button1.setBorderPainted(false);
  add(button1);
  setFocusable(true);  
}


public void paintComponent(Graphics g)
{
  ImageIcon Nintendo = new ImageIcon("trumpL.png");

  g.drawImage(Nintendo.getImage(), 0, 0, 1000, 1000, null);

}

private class B1Listener implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {


  }
 }
}

And here is the panel for the actual first level of the game.

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.image.BufferedImage;
 import javax.swing.Timer;


 public class Panel00 extends JPanel
{
 private BufferedImage myImage;
 private Graphics myBuffer;
 public Timer timer;
 public JButton button1;
 public JButton button2;
 public JLabel label1 = new JLabel("Good Choice!");
 public JLabel label2 = new JLabel("You're Fired!!");
 public int x = 5;      //CountDown from 5  
 public int delay = 1000;   //milliseconds
 boolean drawWin = false;
 boolean drawLose = false;



 public Panel00()
{

  myImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
  myBuffer = myImage.getGraphics();




  setLayout(null);


  button1 = new JButton();
  button1.setSize(300, 200);
  button1.setLocation(100,150);
  button1.setFont(new Font("Serif", Font.BOLD, 18));
  button1.setText("<html><center>"+"Until we are able to determine and understand this problem"+"<br>"+" and the dangerous threat it poses, our country cannot be the victims of horrendous attacks"+"<br>"+"by people that believe only in Jihad, and have no sense of reason or respect for human life"+"</center></html>");
  button1.addActionListener(new B1Listener());
  button1.setBorder(null);
  button1.setOpaque(false);
  button1.setContentAreaFilled(false);
  button1.setBorderPainted(false);
  add(button1);

  button2 = new JButton();
  button2.setSize(300, 200);
  button2.setLocation(600,150);
  button2.setFont(new Font("Serif", Font.BOLD, 18));
  button2.setText("<html><center>"+"If ISIS wants to fight, fine with us. "+"<br>"+"We have wanted that fight for a long time. There is no room in the world for ISIS any more."+"<br>"+"The Muslims or us,  one of us will have to go."+"</center></html>");
  button2.addActionListener(new B2Listener());
  button2.setBorder(null);
  button2.setOpaque(false);
  button2.setContentAreaFilled(false);
  button2.setBorderPainted(false);
  add(button2);

  ActionListener counter = 
     new ActionListener() 
     {
        public void actionPerformed(ActionEvent evt) 
        { 
           repaint();
           x--;
           if (x == 0)
           {
              timer.stop();  
           }
        }
     };
  timer = new Timer(delay, counter);
  timer.start();


  setFocusable(true);  
}


public void paintComponent(Graphics g)
{
  ImageIcon Nintendo = new ImageIcon("trump speech.jpg");

  g.drawImage(Nintendo.getImage(), 0, 0, 1000, 1000, null);

  ImageIcon N = new ImageIcon("happy.JPG");

  g.setColor(Color.WHITE);
  g.fillOval(90,100,320,320);

  g.setColor(Color.WHITE);
  g.fillOval(590,100,320,320);

  g.setColor(Color.WHITE);
  g.setFont(new Font("Serif",Font.BOLD, 50));
  g.drawString(""+x,500,50);

  if (drawWin)
  {
     g.drawImage(N.getImage(), 0, 0, 1000, 1000, null); 
  }
  ImageIcon L = new ImageIcon("loser.JPG");
  if (drawLose)
  {
     g.drawImage(L.getImage(), 0, 0, 1000, 1000, null); 
  }

}

private class B1Listener implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {
     repaint();
     drawWin = true;

     label1.setLocation(250,650);
     label1.setSize(1000, 400);
     label1.setForeground(new Color(212, 175, 55));
     label1.setFont(new Font("Serif", Font.BOLD, 100));
     add(label1);

     button1.setEnabled(false);
     button2.setEnabled(false);

     button1.setText("");
     button2.setText("");

     timer.stop();

  }
 }

   private class B2Listener implements ActionListener
  {
  public void actionPerformed(ActionEvent e)
  {
     repaint();
     drawLose = true;

     label2.setLocation(500,700);
     label2.setSize(400, 400);
     label2.setForeground(Color.RED);
     label2.setFont(new Font("Serif", Font.BOLD, 40));
     add(label2);

     button1.setEnabled(false);
     button2.setEnabled(false);

     button1.setText("");
     button2.setText("");


     timer.stop();
  }
 }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
JH97
  • 9
  • 4

1 Answers1

3

Suggestions:

  • First and foremost, swap JPanels using a CardLayout. You'll need another JPanel, one that uses the CardLayout, you'll add your two JPanels above to this first JPanel with an appropriate String constant, and then you can swap JPanels at will. The CardLayout Tutorial will show you how. I have posted some code that can be found in some of these links. Here's a nice one.
  • Other serious issues in your code -- first and foremost, never read in images or any other files within a paintComponent method. This method is what mostly determines the perceived responsiveness of your GUI, and you never want to slow it down. But also, why do this? Why not read in the image once, store it in a variable, and be done with it?
  • Also call the super.paintComponent(...) method within your override, else you may not rid your GUI of dirty pixels.
  • Also Avoid using null layouts like the plague. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Could you explain where Constant goes in the new Card Layout JPanel and what it does exactly? – JH97 Jul 01 '16 at 21:38
  • @JH97: first read [the tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), then attempt to solve it, then if still stuck, show your code and your specific question. – Hovercraft Full Of Eels Jul 02 '16 at 03:00
  • I've spent several hours trying to understand cardlayout through the examples, and I just don't get it. I've done like 6 lines of codes and been stuck since. I understand what the goal is, and sort of how it is suppose to be done, but I don't know how to apply it to what I'm trying to do, even with all your help. All of the examples seem really different from mine, and use a lot of terms I'm unfamiliar with. I'm sure I'll learn about it all in time, but for the moment, it's just making me feel like an idiot and confusing me. – JH97 Jul 03 '16 at 13:26