-5

Basically, I'm trying to do a test on my GUI to make sure it will paint. Here are my classes: Game

public class Game {
    private static GUI gui = new GUI();

    private static int[][] pixels = new int[10][10];
    public static void main(String[] args) {

    }
    public void startGame() {
        System.out.print("start");
        gui.setGameFrame();
    }
    public static GUI getGUI() {
        return gui;
    }
    public static int[][] getGraphics() {
        return pixels;
    }
}

GUI

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class GUI extends JPanel {
private static Game game = new Game();
private static JPanel panel = new JPanel();
private static JFrame frame = new JFrame();
final private static int FRAME_HEIGHT = 500;
final private static int FRAME_WIDTH = 500;
//Board size 25x25px
final private static int PIXEL_SIZE = 20;

public GUI () {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            setAttributes();
            makeMenu();
        }
    });
}
public static void setAttributes() {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("");
    frame.setBackground(Color.black);
    frame.setVisible(true);
}

private static void makeMenu() {
    JButton start = new JButton("Start");
    start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            game.startGame();
        }
    });
    panel.add(start);
    frame.add(panel);
    frame.pack();
}
public void setGameFrame() {
    panel.removeAll();
    frame.getContentPane().add(Game.getGUI());
    frame.setTitle("Snake v0.1");
    frame.setSize(getPreferredSize());
}
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.white);
    g.fillRect(5, 5, 10, 10);
}
@Override
public Dimension getPreferredSize() {
    return new Dimension(FRAME_WIDTH, FRAME_HEIGHT);
 }
public void paintGraphics() {
    int[][] pixels = Game.getGraphics();
}
}

I've attempted to debug it, but cannot trace why it isn't functioning. I believe it's something to do with: frame.getContentPane().add(Game.getGUI()); But I'm not certain.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Modify You
  • 153
  • 4
  • 14
  • 1
    -1, Quit multiposting. You already have a topic on this and you got good advice (http://stackoverflow.com/q/20693744/131872). Keep the conversation in one posting so everybody know what has been suggested. Did you look at the tutorial? There is also a section on `Custom Painting` that will show you how to better structure your program. Start with a working example and then customize the painting. – camickr Dec 20 '13 at 01:58
  • Your `main` method is empty – MadProgrammer Dec 20 '13 at 02:02
  • What's the purpose of the `Game` class? Why not just run everything from the Gui class? – Paul Samsotha Dec 20 '13 at 02:16
  • @peeskillet because further code will involve logic and event handling, and sending these events to the GUI class for updating. – Modify You Dec 20 '13 at 02:33

2 Answers2

2

Me: "What's the purpose of the Game class? Why not just run everything from the Gui class? "

You: "because further code will involve logic and event handling, and sending these events to the GUI class for updating."

Think of the Game as your data model. Use the Game class for only data and data manipulation and keep all the GUI procedures in your GUI class. Just create an instance of the Game class in your GUI class. Run the program from your GUI class, i.e. have the main method in your GUI class. and run the invokeLater from the main method.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

I copied and pasted your code directly into Netbeans, and it seems to be working.

On mine there seems to be some graphical object covering the top left of the white rectangle, which is why it didn't show up initially.

Try changing your fillRect() function to fill a larger area. This worked for me:

g.fillRect(5, 5, 100, 100);
rcampos
  • 11
  • 2
  • The problem is that I need to have each pixel paintable, so I need to remove the blocking object. – Modify You Dec 20 '13 at 02:40
  • 1
    What's happening is that your **White** rectangle is painted onto the **JPanel** that extends from the **GUI class**, but the **JButton** has been added to the JPanel declared inside of the GUI class, named `panel`. So you are working with two JPanels. The JButton is added to the `panel` object, and you correctly called the `panel.removeAll();` to remove the button, but you will also need to remove the panel from the `frame` object using `frame.remove(panel);`. Now that little gray box wont show, and the **White** box will draw onto the JPanel that extends from the GUI class. – rcampos Dec 20 '13 at 03:33
  • 1
    Ah, thank you! I figured out that I only needed to remove the panel, as that would remove the button too. – Modify You Dec 20 '13 at 20:25