Reputation: 15
This is copied and adapted from the example I received from an earlier question
I have 2 issues at the moment
Firstly, I can only get one JButton to appear on screen, then that JButton is not responding when I press it. Also, how can I stop the first square from appearing?
I know the program is in one big blob ATM, I will separate into individual classes once I sort these issues out.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingPaintDemo3 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.pack();
f.setLayout(null);
JButton seed = new JButton ("Seed");
seed.setBounds(1050,50,100,50);
f.add(seed);
seed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Start");
}
});
JButton start = new JButton ("Start");
start.setBounds(1050,150,100,50);
f.add(start);
JButton stop = new JButton ("Stop");
stop.setBounds(1050,250,100,50);
//f.add(stop);
JButton reset = new JButton ("Reset");
reset.setBounds(1050,350,100,50);
//f.add(reset);
f.setVisible(true);
int lifegrid [] [] [] = new int [42] [62] [2];
for (int i=0; i<42; i++) {
for (int j=0; j<62; j++) {
lifegrid [i] [j] [0] = 0;
lifegrid [i] [j] [1] = 0;
}
}
}
}
class MyPanel extends JPanel {
int lifegrid [] [] [] = new int [62] [42] [2];
int squareX = 0;
int squareY = 0;
int gridX = 0;
int gridY = 0;
public MyPanel() {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
squareX = e.getX();
squareY = e.getY();
if ((squareX>50 & squareX<950) & (squareY>50 & squareY <650) ){
gridX =(squareX-50)/15+1;
gridY =(squareY-50)/15+1;
squareX = (squareX -50)/15 * 15 + 50;
squareY = (squareY -50)/15 * 15 + 50;
lifegrid [gridX] [gridY] [0] = 1;
System.out.println(gridX + " " + gridY);
repaint(squareX,squareY,15,15);}
else {}
}
});
}
public Dimension getPreferredSize() {
return new Dimension(1280,800);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(squareX,squareY,13,13);
g.setColor(Color.BLACK);
g.drawRect(squareX,squareY,13,13);
}
}
Upvotes: 0
Views: 200
Reputation: 102953
You have 2 buttons.
One button's face reads 'Seed'. When you press it, your action listener will print "Start".
The other button's face reads 'Start'. It has no action listener, therefore, if pressed, nothing happens.
SOURCE: Just.. read your code, it's right there.
I assume that's a bug. If not, that is quite confusing: Why would pressing the button that does NOT have 'Start' written on it, print 'Start' to sysout?
You then add both buttons to a panel with dubious layouting.
I assume you're pressing the only button you can see (With 'Start' on it), because it is right on top of the other button, and you observe nothing.
Which is exactly what you programmed.
Probable solutions:
Upvotes: 3