1

Cannot put my game into the GUI without there being issues. When starting the GUI, it shows the game. It glitches out and only fixes after pressing on of the buttons. However the buttons are hidden unless you put your mouse over it. Here is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class BBSays extends JFrame implements ActionListener, MouseListener 
{
    private BufferedImage image;
    public static BBSays bbsays;
    public Renderer renderer;
    public static final int WIDTH = 800, HEIGHT = 800;
    public int flashed = 0, glowTime, dark, ticks, indexPattern;
    public boolean creatingPattern = true;  
    public ArrayList<Integer> pattern;
    public Random random;
    private boolean gameOver;
    private JPanel game;
    JFrame frame = new JFrame("BB8 Says");
    private JPanel menu;
    private JPanel credits;
    ImageIcon bbegif = new ImageIcon("tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");


    public BBSays()
    {

        Timer timer = new Timer(20, this);
        renderer = new Renderer();
        frame.setSize(WIDTH +7, HEIGHT +30);
        frame.setVisible(true);
        frame.addMouseListener(this);
        frame.add(renderer);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
        start();
        timer.start();
        menu = new JPanel();
        credits = new JPanel();
        game = new JPanel();
        menu.setBackground(Color.yellow);
        credits.setBackground(Color.yellow);
        game.setBackground(Color.yellow);
        JButton button = new JButton("Start");
        JButton button2 = new JButton("Exit");
        JButton button4 = new JButton("Start");
        JLabel greet = new JLabel("                       Welcome to BB8 Says");
        JLabel jif = new JLabel(bbegif);
        JLabel jif2 = new JLabel(bbegif);
        JLabel saus = new JLabel("BB8 Image: https://49.media.tumblr.com/7ba3be87bff2efc009e9cfa889d46b4e/tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                frame.setContentPane(game);
                frame.invalidate();
                frame.validate();
            };
        });
        button2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            };
        });
        menu.setLayout(new GridLayout(2,2));
        menu.add(jif2);
        menu.add(greet);
        menu.add(jif);
        menu.add(button);
        menu.add(button6);
        menu.add(button2);
        frame.setVisible(true);     
    }

    private class MenuAction implements ActionListener {
        private JPanel panel;
        private MenuAction(JPanel pnl) {
            this.panel = pnl;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            changePanel(panel);
        }
    }
    private void changePanel(JPanel panel) {
        getContentPane().removeAll();
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().doLayout();
        update(getGraphics());
    }
    public void start()
    {
        random = new Random();
        pattern = new ArrayList<Integer>();
        indexPattern = 0;
        dark = 2;
        flashed = 0;
        ticks = 0;
    }

    public static void main(String[] args)
    {
        bbsays = new BBSays();
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        ticks++;

        if (ticks % 20 == 0)
        {
            flashed = 0;

            if (dark >= 0)
            {
                dark--;
            }
        }

        if (creatingPattern)
        {
            if (dark <= 0)
            {
                if (indexPattern >= pattern.size())
                {
                    flashed = random.nextInt(40) % 4 + 1;
                    pattern.add(flashed);
                    indexPattern = 0;
                    creatingPattern = false;
                }
                else
                {
                    flashed = pattern.get(indexPattern);
                    indexPattern++;
                }

                dark = 2;
            }
        }
        else if (indexPattern == pattern.size())
        {
            creatingPattern = true;
            indexPattern = 0;
            dark = 2;
        }

        renderer.repaint();
    }

    public void paint(Graphics2D g) 
    {   
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.yellow);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        if (flashed == 1)
        {
            g.setColor(Color.blue);
        }
        else
        {
            g.setColor(Color.blue.darker());
        }
        g.fillRect(0, 0, WIDTH/2, HEIGHT/2);

        if (flashed == 2)
        {
            g.setColor(Color.green);
        }
        else
        {
            g.setColor(Color.green.darker());
        }
        g.fillRect(WIDTH/2, 0, WIDTH/2, HEIGHT/2);

        if (flashed == 3)
        {
            g.setColor(Color.orange);
        }
        else
        {
            g.setColor(Color.orange.darker());
        }
        g.fillRect(0, HEIGHT/2, WIDTH/2, HEIGHT/2);

        if (flashed == 4)
        {
            g.setColor(Color.gray);
        }
        else
        {
            g.setColor(Color.gray.darker());
        }
        g.fillRect(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2);

        g.setColor(Color.BLACK);
        g.fillRoundRect(220, 220, 350, 350, 300, 300);
        g.fillRect(WIDTH/2 - WIDTH/14, 0, WIDTH/7, HEIGHT);
        g.fillRect(0, WIDTH/2 - WIDTH/12, WIDTH, HEIGHT/7);

        g.setColor(Color.yellow);
        g.setStroke(new BasicStroke(200));
        g.drawOval(-100, -100, WIDTH+200, HEIGHT+200);

        g.setColor(Color.black);
        g.setStroke(new BasicStroke(5));
        g.drawOval(0, 0, WIDTH, HEIGHT);



        if (gameOver)
        {
            g.setColor(Color.WHITE);
            g.setFont(new Font("Comic Sans", 1, 80));
            g.drawString("You let", WIDTH / 2 - 140, HEIGHT / 2 - 70);
            g.drawString("down BB8 :(", WIDTH / 2 - 220, HEIGHT / 2 );
            g.drawString("Try again!", WIDTH / 2 - 195, HEIGHT / 2 + 80);
        }
        else
        {
            g.setColor(Color.WHITE);
            g.setFont(new Font("Ariel", 1, 144));
            g.drawString(indexPattern + "/" + pattern.size(), WIDTH / 2 - 100, HEIGHT / 2 + 42);
        }


    }

    @Override
    public void mousePressed(MouseEvent e) 
    {
        int x = e.getX(), y = e.getY();

        if (!creatingPattern && !gameOver)
        {
            if (x>0 && x<WIDTH/2 && y>0 && y<HEIGHT/2)
            {
                flashed = 1;
                ticks = 1;
            }
            else if (x>WIDTH/2 && x<WIDTH && y>0 && y<HEIGHT/2)
            {
                flashed = 2;
                ticks = 1;
            }
            else if (x>0 && x<WIDTH/2 && y>HEIGHT/2 && y<HEIGHT)
            {
                flashed = 3;
                ticks = 1;
            }
            else if (x>WIDTH/2 && x<WIDTH && y>HEIGHT/2 && y<HEIGHT)
            {
                flashed = 4;
                ticks = 1;
            }
            if (flashed != 0)
            {
                if (pattern.get(indexPattern)==flashed)
                {
                indexPattern++;
                }
                else
                {
                    gameOver = true;
                }
            }
            else
            {
                start();
                gameOver = true;
            }
        }
        else if (gameOver)
        {
            start();
            gameOver = false;
        }
    }

Here is the code for the renderer class:

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class Renderer extends JPanel
{

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (BBSays.bbsays != null)
        {
        BBSays.bbsays.paint((Graphics2D) g);
        }
    }

}

I think that the issue is here as I use the super. method and want to put the graphics on the game JPanel in the main code. I have tried many ways of doing this but I am not able to put the game on a JPanel. If you can help it is greatly appreciated.

WillA
  • 33
  • 6
  • 1
    1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). .. – Andrew Thompson Dec 31 '16 at 23:47
  • 1
    .. 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 5) `public class BBSays extends JFrame implements.. public void paint(Graphics2D g)` This code seems to be both custom painting a frame and adding components that would likely cover the entire area of the content pane. This is a recipe for disaster! – Andrew Thompson Dec 31 '16 at 23:47
  • @AndrewThompson how would you reccommend me fixing this then? – WillA Jan 01 '17 at 00:48
  • @AndrewThompson Is there a way to put the Graphics2D on a JPanel, if not what will I need to do. – WillA Jan 01 '17 at 00:59
  • @AndrewThompson Is there a way to separate the GUI and the game and run one off of the other, e.g, when pressing start, the JFrame of the GUI closes and the JFrame of the game opens? – WillA Jan 01 '17 at 01:11
  • *"Is there a way to put the Graphics2D on a JPanel"* Yes. Instead of overriding `paint(Graphics)` override `paintComponent(Graphics)`, immediately call the `super` method before doing anything else. *"Is there a way to separate the GUI and the game and run one off of the other, e.g, when pressing start, the JFrame of the GUI closes and the JFrame of the game opens?"* No need for more than one frame. Just have the `GamePanel` as one card in the card layout mentioned in the first comment. – Andrew Thompson Jan 01 '17 at 03:49
  • @AndrewThompson did I not already override paintComponent(Graphics) in the renderer class? – WillA Jan 01 '17 at 16:35
  • @AndrewThompson I do not really understand what you mean, could you show examples, or something? – WillA Jan 01 '17 at 17:10
  • *"did I not already override paintComponent(Graphics) in the renderer class?"* Right you are. I missed the 2nd class, and was only looking at the overridden `paint` method in the first. Take that out. *"I do not really understand what you mean, could you show examples, or something?"* The reason I made that mistake is largely because I was only looking briefly at the code, and the reson I only took a brief look is because I don't do anything more than that until there is an **MCVE** or **SSCCE** as mentioned in point (2) of my first comment. So reduce the problem down to one and I'll take .. – Andrew Thompson Jan 01 '17 at 17:26
  • .. a closer look at it, and possibly base an answer on that, or write code that shows how to go about it. But it all depends on you following a suggestion I offered 17 hours ago. – Andrew Thompson Jan 01 '17 at 17:27
  • @AndrewThompson the issue is that I do not know where the main problem lies, I will try and remove the parts that I know work and hope that helps – WillA Jan 01 '17 at 17:31
  • @AndrewThompson I have removed some parts, tell me if I need to remove more, I can remove the function of the game, so that you only see the graphics and not be able to play it – WillA Jan 01 '17 at 17:41
  • @AndrewThompson where is the overwritten paint method, is it renderer.repaint(); – WillA Jan 01 '17 at 17:56
  • @AndrewThompson Because I need it for the game to work, I have tried without. – WillA Jan 01 '17 at 19:01

0 Answers0