c4

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; int x = 0; BufferedImage img; String message = "Click Board to Start"; private Thread animator; int[][] circs = new int[6][7]; int c = 0; int place = 0; int count = 0; int yel = 0; int red = 0; boolean go = false; 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.blue); g.fillRect(0, 0, d.width, d.height); Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = this.getFontMetrics(small); g.setColor(Color.white); g.setFont(small); g.drawString(message, 10, d.height-60); int r = 40; int x = 0; int y = 0; int space = 50; for(int row = 0; row < circs[0].length; row++){ for(int col = 0; col < circs.length; col++){ if (circs[col][row] == 1){ g.setColor(Color.red); } if(circs[col][row] == 2){ g.setColor(Color.yellow); } g.fillOval(x,y,r,r); g.setColor(Color.white); x += space; } y += space; x = 0; } if (ingame) { //g.drawImage(img,5,5,200,200 ,null); } Toolkit.getDefaultToolkit().sync(); g.dispose(); choosewin(); } public void choosewin(){ for(int row = 0; row < circs[0].length; row++){ for(int col = 0; col < circs.length; col++){ try{ if (circs[row][col] == 1 && circs[row][col + 1] == 1 && circs[row][col + 2] == 1 && circs[row][col + 3] == 1){ //System.out.println("Player 1 wins"); message = "Player 1 has won, click screen to play again"; go = true; } if (circs[row][col] == 2 && circs[row][col + 1] == 2 && circs[row][col + 2] == 2 && circs[row][col + 3] == 2){ //System.out.println("Player 2 wins"); message = "Player 2 has won, click screen to play again"; go = true; } if (circs[row][col] == 1 && circs[row + 1][col] == 1 && circs[row + 2][col] == 1 && circs[row + 3][col] == 1){ //System.out.println("Player 1 wins"); message = "Player 1 has won, click screen to play again"; ; go = true; } if (circs[row][col] == 2 && circs[row + 1][col] == 2 && circs[row + 2][col] == 2 && circs[row + 3][col] == 2){ //System.out.println("Player 2 wins"); message = "Player 2 has won, click screen to play again"; go = true; } }catch(Exception e){ } } } if (go == true){ ingame = false; } } 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; int yCell = (y/50) + 1; message = xCell + " " + yCell; boolean r = false; count++; //circs[xCell - 1][yCell - 1] = 1; for (int i = 6; i >= 0; i--){ if(circs[xCell - 1][i] == 0){ if (count % 2 == 0){ circs[xCell - 1][i] = 1; } else{ circs[xCell - 1][i] = 2; } if(r) circs[xCell - 1][i] = 0; r = true; } } place = xCell; c = yCell; if(go == true){ go = false; ingame = true; circs = new int [6][7]; message = ""; } } 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

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.