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!