Created
February 5, 2018 17:59
-
-
Save memish/93c7fd471e330b5115400c1f6f42272a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.MouseListener; | |
import java.awt.event.MouseEvent; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Font; | |
import java.awt.FontMetrics; | |
import java.awt.Graphics; | |
import java.awt.Toolkit; | |
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
import javax.swing.Timer; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.Random; | |
import javax.swing.ImageIcon; | |
import javax.swing.JPanel; | |
import javax.imageio.*; | |
import java.awt.image.*; | |
import java.io.*; | |
public class Board extends JPanel implements Runnable, MouseListener | |
{ | |
boolean ingame = true; | |
private Dimension d; | |
int BOARD_WIDTH=500; | |
int BOARD_HEIGHT=500; | |
//deleted x value | |
//NEW | |
int[][] cells = new int[6][7];//row col | |
int xCell; | |
int yCell; | |
int turn = 1; | |
BufferedImage img; | |
String message = "Click Board to Start"; | |
private Thread animator; | |
public Board() | |
{ | |
addKeyListener(new TAdapter()); | |
addMouseListener(this); | |
setFocusable(true); | |
d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); | |
setBackground(Color.black); | |
/* | |
try { | |
img = ImageIO.read(this.getClass().getResource("mount.jpg")); | |
} catch (IOException e) { | |
System.out.println("Image could not be read"); | |
// System.exit(1); | |
} | |
*/ | |
if (animator == null || !ingame) { | |
animator = new Thread(this); | |
animator.start(); | |
} | |
setDoubleBuffered(true); | |
} | |
public void paint(Graphics g) | |
{ | |
super.paint(g); | |
g.setColor(Color.white); | |
g.fillRect(0, 0, d.width, d.height); | |
//g.fillOval(x,y,r,r); | |
//NEW | |
int x = 0; | |
int y = 0; | |
int r = 50; | |
int space = 60; | |
for(int row = 0; row<cells.length; row++){ | |
for(int col = 0; col<cells[0].length; col++){ | |
if(cells[row][col]==0) | |
g.setColor(Color.blue); | |
else if (cells[row][col]==1) | |
g.setColor(Color.red); | |
else | |
g.setColor(Color.yellow); | |
g.fillOval(x,y,r,r); | |
x += space; | |
} | |
y += space; | |
x = 0; | |
} | |
Font small = new Font("Helvetica", Font.BOLD, 14); | |
FontMetrics metr = this.getFontMetrics(small); | |
g.setColor(Color.black); | |
g.setFont(small); | |
g.drawString(message, 10, d.height-60); | |
if (ingame) { | |
// g.drawImage(img,0,0,200,200 ,null); | |
} | |
Toolkit.getDefaultToolkit().sync(); | |
g.dispose(); | |
} | |
private class TAdapter extends KeyAdapter { | |
public void keyReleased(KeyEvent e) { | |
int key = e.getKeyCode(); | |
} | |
public void keyPressed(KeyEvent e) { | |
//System.out.println( e.getKeyCode()); | |
// message = "Key Pressed: " + e.getKeyCode(); | |
int key = e.getKeyCode(); | |
if(key==39){ | |
} | |
} | |
} | |
public void placeChip(int col){ | |
for(int i = cells.length - 1; i>-1; i--){ | |
if( cells[i][col]==0){ | |
cells[i][col] = turn; | |
if(turn==1) | |
turn=2; | |
else | |
turn=1; | |
break; | |
} | |
} | |
} | |
public void mousePressed(MouseEvent e) { | |
int x = e.getX(); | |
int y = e.getY(); | |
//New | |
xCell = (x/60) + 1; | |
yCell = (y/60) + 1; | |
// message = xCell + " " + yCell; | |
placeChip(xCell-1); | |
} | |
public void mouseReleased(MouseEvent e) { | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
public void mouseClicked(MouseEvent e) { | |
} | |
public void run() { | |
long beforeTime, timeDiff, sleep; | |
beforeTime = System.currentTimeMillis(); | |
int animationDelay = 50; | |
long time = | |
System.currentTimeMillis(); | |
while (true) {//infinite loop | |
// spriteManager.update(); | |
repaint(); | |
try { | |
time += animationDelay; | |
Thread.sleep(Math.max(0,time - | |
System.currentTimeMillis())); | |
}catch (InterruptedException e) { | |
System.out.println(e); | |
}//end catch | |
}//end while loop | |
}//end of run | |
}//end of class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment