0

so I have an interface that reads a 2D Char array and is supposed to make a graphical drawing in a newly created window.

Here's the code:

class View extends JFrame {

        private final List<Integer> path = new ArrayList<Integer>();
        private int pathIndex;

        private View() {
            setTitle("Simple Maze Solver");
            setSize(640, 480);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            //DepthFirst.searchPath(storelab, 1, 1, path);
            pathIndex = path.size() - 2;
     }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.translate(50, 50);

        // draws the maze
        for (int row = 0; row < storelab.length; row++) {
            for (int col = 0; col < storelab[0].length; col++) {
                Color color;
                switch (storelab[row][col]) {
                    case 1 : color = Color.BLACK; break;
                    case 9 : color = Color.RED; break;
                    default : color = Color.WHITE;
                }
                g.setColor(color);
                g.fillRect(30 * col, 30 * row, 30, 30);
                g.setColor(Color.BLACK);
                g.drawRect(30 * col, 30 * row, 30, 30);
            }
        }

        // draw the path list
        for (int p = 0; p < path.size(); p += 2) {
            int pathX = path.get(p);
            int pathY = path.get(p + 1);
            g.setColor(Color.GREEN);
            g.fillRect(pathX * 30, pathY * 30, 30, 30);
        }

        // draw the ball on path
        int pathX = path.get(pathIndex);
        int pathY = path.get(pathIndex + 1);
        g.setColor(Color.RED);
        g.fillOval(pathX * 30, pathY * 30, 30, 30);
    }

    @Override
    protected void processKeyEvent(KeyEvent ke) {
        if (ke.getID() != KeyEvent.KEY_PRESSED) {
            return;
        }
        if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
            pathIndex -= 2;
            if (pathIndex < 0) {
                pathIndex = 0;
            }
        }
        else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
            pathIndex += 2;
            if (pathIndex > path.size() - 2) {
                pathIndex = path.size() - 2;
            }
        }
        repaint();
    }

    public void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                View view = new View();
                view.setVisible(true);


            }


        });

    }
 }
}

It's supposed to make a graphical drawing. Now, in another class, my Interface class, as you can probably guess :D, I have an interface with some buttons, now my problem is, how do I link the graphical drawing of my array to the button in the Interface class? When I click that button, it's supposed to trigger the JFrame class.

Here's a relevant snippet of it:

public static void main(String[] args) {
    Ch14JButtonFrame frame = new Ch14JButtonFrame();
    frame.setVisible(true);
}

public Ch14JButtonFrame() {

    /*Vai ser definido os parametros (dimensoes, titulo, etc.) da iterface*/
    Container contentPane = getContentPane();
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setResizable(true);
    setTitle("Jogo do Labirinto 37732 37861");
    setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    contentPane.setLayout(new FlowLayout());


    **showButton = new JButton("Show Labyrinth");
    contentPane.add(showButton);
    showButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

        }**
    });

The button that's supposed to trigget it, is named showButton. Any help appreciated!

  • You should, at first, to create a link to the instance of class View and later, using variable repaint that class. – Vasyl Lyashkevych Jun 14 '17 at 17:33
  • 1) Don't extend `JFrame`, it's a rigid container that can't be placed inside other containers, instead extend `JPanel`, and draw over it, then add that panel to the frame. See [Extends jframe vs creating inside of class](https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program). Related to (1), don't override `paint(...)` but `paintComponent`. 3) Don't call `setSize(...)`, instead override `getPreferredSize(...)` method and call `pack()`. 4) Don't use a Key Listener, instead use Key Bindings. – Frakcool Jun 14 '17 at 18:02
  • To answer your question: move the code inside `main()` method of `View` class inside the `actionPerformed()` method – Frakcool Jun 14 '17 at 18:03

1 Answers1

0

If you want to open the frame in a new window you could do this:

showButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        new View().setVisible(true);
    }
});

Also you must change your View class to public and also the constructor.

public class View extends JFrame {
    ...
    public View() {
        ...
    }
}

But if you want to embed the content of the View Frame in another frame that's another process.

sirandy
  • 1,834
  • 5
  • 27
  • 32