Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class ConnectLDriver extends JApplet implements MouseListener {
- ConnectLGame thisGame = new ConnectLGame();
- Canvas canvas;
- uiHandler uih;
- int sqWid,sqHgt;
- int turn;
- JTextField label;
- JComboBox aiBox;
- boolean initialClick = false;
- boolean falseClick = false;
- Integer[] aiLevel = {1, 2, 3, 4, 5, 6, 7, 8, 9};
- public class Canvas extends JPanel {
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- int w,h;
- int rows = thisGame.rows;
- int cols = thisGame.cols; // number of rows and columns for the board
- w = getWidth();
- h = getHeight();
- g.setColor( Color.black);
- g.drawLine(10,10,200,200);
- sqWid = w/cols;
- sqHgt = h/rows;
- g.setColor(Color.gray);
- g.fillRect(0, 0, 500, 500);
- for (int j = 0; j < rows; j++) {
- for (int i=0; i < cols; i++) {
- g.setColor(Color.black);
- g.drawLine(i*sqWid, 0, i*sqWid, h);
- g.drawLine(0, j*sqHgt, w, j*sqHgt);
- if(thisGame.getValueInLoc(j, i) != thisGame.MARK_NONE) {
- if(thisGame.getValueInLoc(j, i) %2 == thisGame.MARK_O){
- g.setColor(Color.red);
- g.fillOval(i*sqWid, j*sqHgt, sqWid, sqHgt);
- }
- else if (thisGame.getValueInLoc(j, i) %2 == thisGame.MARK_X)
- g.setColor(Color.black);
- g.fillOval(i*sqWid, j*sqHgt, sqWid, sqHgt);
- }
- }
- }
- if(thisGame.getGameState() == thisGame.GAME_STATE_X_TURN)
- label.setText("Black's Turn");
- else if(thisGame.getGameState() == thisGame.GAME_STATE_O_TURN)
- label.setText("Red's Turn");
- else if (thisGame.getGameState() == thisGame.GAME_STATE_O_WON)
- label.setText("Red Wins");
- else if (thisGame.getGameState() == thisGame.GAME_STATE_X_WON)
- label.setText("Black Wins");
- if(falseClick)
- label.setText("Choose another column");
- }
- }
- private class uiHandler implements ActionListener { // The event listener.
- public void actionPerformed(ActionEvent e) {
- initialClick = true;
- checkBox();
- thisGame.reset();
- canvas.repaint();
- }
- } // end uiHandler class
- public void init() { // initialize the applet
- this.setSize(500, 500);
- createComponents();
- }
- public void createComponents() {
- JPanel content = new JPanel();
- content.setLayout( new BorderLayout());
- JPanel uip = new JPanel();
- uip.setLayout( new FlowLayout());
- label = new JTextField("Press New Game button");
- aiBox = new JComboBox(aiLevel);
- JButton b4 = new JButton("New Game");
- uip.add(aiBox);
- uip.add(b4);
- uip.add(label);
- uih = new uiHandler();
- b4.addActionListener(uih);
- canvas = new Canvas();
- canvas.setBackground(new Color(210, 180, 140));
- canvas.addMouseListener(this);
- content.add(canvas, BorderLayout.CENTER);
- content.add(uip, BorderLayout.SOUTH);
- setContentPane(content);
- }
- public void checkBox() {
- if(initialClick == false || thisGame.getGameState() == thisGame.GAME_STATE_O_WON || thisGame.getGameState() == thisGame.GAME_STATE_X_WON)
- {
- aiBox.setEnabled(true);
- }
- else
- aiBox.setEnabled(false);
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- // TODO Auto-generated method stub
- //System.out.println( "Coords "+ e.getX() + " " + e.getY());
- int x = e.getX();
- int y = e.getY();
- int c, r;
- c = x / sqWid;
- r = y / sqHgt;
- // System.out.println("TEST " + c + " " + r);
- if(initialClick == false)
- label.setText("Press New Game button");
- else {
- if(thisGame.placeChecker(c)) {
- falseClick = false;
- if ( thisGame.getGameState() == ConnectLGame.GAME_STATE_O_TURN ||
- thisGame.getGameState() == ConnectLGame.GAME_STATE_X_TURN) {
- mmPlayer.play(thisGame, aiBox.getSelectedIndex());
- }
- }
- else {
- falseClick = true;
- }
- checkBox();
- canvas.repaint();
- }
- }
- @Override
- public void mouseEntered(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseReleased(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement