This is supposed to draw a line through the JButtons when square9 is clicked. It doesn't work but there are no errors and I don't see what the problem is. Ezzaral gave me the code to draw the line earlier. Obviously, the winning squares aren't always diagonal, so I will eventually have to adapt the code to draw the line across the correct squares. My code looks different than the code he gave me; however, I can't figure out any problems with it. The main difference is that he made an inner class for his action listener while I'm implementing ActionListener in my main class, but I don't see why this would be a problem.

public class TicTacToeGUI extends JFrame implements ActionListener{

	JPanel panel;
	JButton square1 = new JButton("");
	JButton square2 = new JButton("");
	JButton square3 = new JButton("");
	JButton square4 = new JButton("");
	JButton square5 = new JButton("");
	JButton square6 = new JButton("");
	JButton square7 = new JButton("");
	JButton square8 = new JButton("");
	JButton square9 = new JButton("");
	
	/**
	 * @param args
	 */
	
	public class myGlassPane extends JComponent{
		protected void paintComponent(Graphics g){
		int x0 = square1.getX()+square1.getWidth()/2;
		int y0 = square1.getY()+square1.getHeight()/2;
		int x1 = square9.getX()+square9.getWidth()/2;
		int y1 = square9.getY()+ square9.getHeight()/2;
		g.setColor(Color.RED);
		g.drawLine(x0, y0, x1, y1);
		}
		
	}
	
	
	public void actionPerformed(ActionEvent e){
		JButton temp = (JButton)e.getSource();
	        if (temp == square9){
			getGlassPane().setVisible(getGlassPane().isVisible() );
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}
	
	
	public TicTacToeGUI(){
		super();
		setPreferredSize(new Dimension(300,300));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel = new JPanel(new GridLayout(3,3));
		panel.add(square1);
		square1.addActionListener(this);
		panel.add(square2);
		square2.addActionListener(this);
		panel.add(square3);
		square3.addActionListener(this);
		panel.add(square4);
		square4.addActionListener(this);
		panel.add(square5);
		square5.addActionListener(this);
		panel.add(square6);
		square6.addActionListener(this);
		panel.add(square7);
		square7.addActionListener(this);
		panel.add(square8);
		square8.addActionListener(this);
		panel.add(square9);
		square9.addActionListener(this);
		getContentPane().add(panel, java.awt.BorderLayout.CENTER);
		
		pack();
		setGlassPane(new myGlassPane());
	}
	
	public static void createAndShowGUI(){
		new TicTacToeGUI().setVisible(true);
	}
}

Also, Ezzaral: I'm checking out that link you gave me (in the other thread) right now. . perhaps that will solve my problem. If so, I'll update here.

Recommended Answers

All 2 Replies

You missed the ! (not) operator in my code to toggle the glass pane on/off. You aren't ever setting it visible :)
Do this

getGlassPane().setVisible( !getGlassPane().isVisible() );

Whoops! Lol. Thanks. I'll leave this open for now, since I don't want to create more unnecessary threads about the same topic if I have more problems. But I'll mark it as solved eventually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.