Connect4

import javax.swing.JFrame; public class Starter extends JFrame { public Starter() { add(new Board()); setTitle("Board"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500,500); setLocationRelativeTo(null); setVisible(true); setResizable(false); } public static void main(String[] args) { new Starter(); } } 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 player1turn = true; int turn = 1; boolean ingame = true; private Dimension d; int BOARD_WIDTH=500; int BOARD_HEIGHT=500; int x = 0; BufferedImage img; String message = "Click Board to Start"; private Thread animator; int[][] cells = new int[6][7]; 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); g.setColor(Color.blue); int x = 0; int y = 0; int r = 50; int space = 50; for (int row = 0; row<cells.length; row++){ for (int col = 0; col<cells[0].length; col++){ if(cells[row][col]==1) g.setColor(Color.red); else if(cells[row][col]==2) g.setColor(Color.yellow); else g.setColor(Color.blue); 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.yellow); 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 mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); int xCell = (x/50) + 1;//row int yCell = (y/50) + 1;//col message = xCell + " " + yCell; // cells[yCell-1][xCell-1] = 1; PlaceChip(xCell - 1); } public void PlaceChip(int col){ for (int i = cells.length - 1; i> -1; i--){ if (cells[i][col]==0){ cells[i][col]=turn; break; }} if (turn == 1){ turn = 2; }else{ turn = 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 = 500; 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

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.