Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need a JAVA program that runs a player vs player game. Nothing too fancy. It need you use g.drawLine to make the board and
I need a JAVA program that runs a player vs player game. Nothing too fancy. It need you use g.drawLine to make the board and I need a mouseListener. I have some code already. I've been going at this for about 2 weeks now and I still haven't found a good method. ALL INPUT would be really helpful. Thanks!
import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class TicTacToe { public static void main(String[] args) { JFrame frame = new JFrame(" TicTacToe"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); Board board = new Board(); frame.add(board); frame.setVisible(true); } } class Board extends JComponent { public Board(){ addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent me) { int x = me.getX(); int y = me.getY(); System.out.print(x); System.out.print(y); } }); } public void paint(Graphics g) { g.drawLine(90, 0, 90, 300); g.drawLine(185, 0, 185, 300); g.drawLine(0, 85, 300, 85); g.drawLine(0, 175, 300, 175); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started