import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Random;
/**
* Created with IntelliJ IDEA.
*/
public class MM extends JComponent implements ActionListener, MouseListener {
private int screenX=0;
private int screenY=0;
private boolean start=true;
private int line=1;
private int digit=1;
private int[] arry= new int[5];
private int[] arryGuess= new int[60];
private int guessAryPointer=0;
private ActionEvent e;
private int xX=10,t, chkCirclesXcoordinate=12, circleSpace=55, barCircleSpace=45, yBarcircleSpace=30;
private int temprndm;
private Graphics l;
private Color colBall, colBallNum;
private String ballNum;
private int checkDigitCounter=0;
private Boolean backgroundPainted=false;
//JFrame window = new JFrame("Master Mind");
public MM(){
//resetting the guess array to 0.
guessAryPointer=0;
//resetting the guessing array counter to 0
guessAryPointer=0;
for (int f=0;f<=59;f++){
arryGuess[f]=0;
}
for (int f=0;f<=59;f++){
arryGuess[f]=0;
}
}
public static void main(String [] args) throws IOException {
JFrame window = new JFrame("Master Mind");
MM game= new MM();
window.add(game);
window.pack();
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setVisible(true);
//Timer t = new Timer(30, game);
//t.start();
window.addMouseListener(game);
System.out.println("in Main ");
Graphics ga=window.getGraphics();
}
@Override
public Dimension getPreferredSize() {
System.out.println(" in dimension ");
return new Dimension(450,460);
}
protected void paintComponent(Graphics g) {
System.out.println("at the start of paint component");
///// good working loop
// drawing colored bars: white/pink.
g.setColor(Color.blue);
g.drawLine(0,30,800,30);
for (int i=0;i<=11;i++){
if (i%2!=1){
g.setColor(Color.pink);
}
else{
g.setColor(Color.white);
}
g.fillRect(0,30+i*yBarcircleSpace,800,30);
}
//////end of good working loop
//drawing bottom circles--- THE BUTTONS---
//drawing 1
g.setColor(Color.yellow);
g.fillOval(chkCirclesXcoordinate,410,40,40);
g.setColor(Color.black);
g.drawString("1",chkCirclesXcoordinate+17,434);
//drawing 2
g.setColor(Color.red);
g.fillOval(chkCirclesXcoordinate+circleSpace,410,40,40);
g.setColor(Color.white);
g.drawString("2",chkCirclesXcoordinate+17+circleSpace,434);
//drawing 3
g.setColor(Color.blue);
g.fillOval(chkCirclesXcoordinate+circleSpace*2,410,40,40);
g.setColor(Color.yellow);
g.drawString("3",chkCirclesXcoordinate+17+circleSpace*2,434);
//drawing 4
g.setColor(Color.black);
g.fillOval(chkCirclesXcoordinate+circleSpace*3,410,40,40);
g.setColor(Color.white);
g.drawString("4",chkCirclesXcoordinate+17+circleSpace*3,434);
//drawing 5
g.setColor(Color.magenta);
g.fillOval(chkCirclesXcoordinate+circleSpace*4,410,40,40);
g.setColor(Color.white);
g.drawString("5",chkCirclesXcoordinate+17+circleSpace*4,434);
System.out.println("in paint component after painting ((5))");
//drawing 6
g.setColor(Color.darkGray);
g.fillOval(chkCirclesXcoordinate+circleSpace*5,410,40,40);
g.setColor(Color.white);
g.drawString("6",chkCirclesXcoordinate+17+circleSpace*5,434);
//drawing 7
g.setColor(Color.green);
g.fillOval(chkCirclesXcoordinate+circleSpace*6,410,40,40);
g.setColor(Color.black);
g.drawString("7",chkCirclesXcoordinate+17+circleSpace*6,434);
//drawing 8
g.setColor(Color.orange);
g.fillOval(chkCirclesXcoordinate+circleSpace*7,410,40,40);
g.setColor(Color.black);
g.drawString("8",chkCirclesXcoordinate+17+circleSpace*7,434);
//BottomCircles(gg); DONE
////////////////////////// start of the problem area
//painting the picked numbers in the array, all of them from start, each time the program returns to paint component
for ( t=0;t<guessAryPointer;t++) { // There is for the gues aray counter to paint them all from start
System.out.println("in drawing picked numbers, the T counter is: "+t);
digit=(guessAryPointer-1)%5; //getting the digit
line=(guessAryPointer-1)/5+1; //getting the line, every line has five bals/numbers
if(arryGuess[guessAryPointer-1]==1){ //this is to set the color and the number for each ball , which depend on the array value
colBall= Color.yellow;
colBallNum= Color.black ;
ballNum="1";
}
if(arryGuess[guessAryPointer-1]==2){
colBall= Color.red;
colBallNum= Color.white ;
ballNum="2";
}
if (arryGuess[guessAryPointer-1]==3) {
colBall= Color.blue;
colBallNum= Color.yellow ;
ballNum="3";
}
if (arryGuess[guessAryPointer-1]==4){
colBall= Color.black;
colBallNum= Color.white ;
ballNum="4";
}
if (arryGuess[guessAryPointer-1]==5){
colBall= Color.magenta;
colBallNum= Color.white ;
ballNum="5";
}
if (arryGuess[guessAryPointer-1]==6){
colBall= Color.darkGray;
colBallNum= Color.white ;
ballNum="6";
}
if (arryGuess[guessAryPointer-1]==7){
colBall= Color.green;
colBallNum= Color.black ;
ballNum="7";
}
if (arryGuess[guessAryPointer-1]==8){
colBall= Color.orange;
colBallNum= Color.black ;
ballNum="8";
}
//after setting the color and the value of the ball, we paint it
g.setColor(colBall);
g.fillOval(barCircleSpace*digit+6,yBarcircleSpace*line+1,28,28);
g.setColor(colBallNum);
g.drawString(ballNum,barCircleSpace*digit+17,yBarcircleSpace*line+19);
}
repaint();
//////////////////// end of problem area
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
MouseEvent mouseIvent = (MouseEvent) e;
screenX = mouseIvent.getX();
screenY = mouseIvent.getY();
System.out.println(" in mouse clicked ");
System.out.println("screen(X,Y) = " + screenX + "\t" + screenY);
//knowing which circle "number" was picked through knowing the point coordination at the time of the click
if (screenY>440 && screenY<479){
if(screenX>17 && screenX<56){
arryGuess[guessAryPointer]=1;
guessAryPointer++;
}
if(screenX>72 && screenX<111){
arryGuess[guessAryPointer]=2;
guessAryPointer++;
}
if(screenX>127 && screenX<166){
arryGuess[guessAryPointer]=3;
guessAryPointer++;
}
if(screenX>181 && screenX<220){
arryGuess[guessAryPointer]=4;
guessAryPointer++;
}
if(screenX>237 && screenX<276){
arryGuess[guessAryPointer]=5;
guessAryPointer++;
}
if(screenX>292 && screenX<331){
arryGuess[guessAryPointer]=6;
guessAryPointer++;
}
if(screenX>346 && screenX<385){
arryGuess[guessAryPointer]=7;
guessAryPointer++;
}
if(screenX>401 && screenX<440){
arryGuess[guessAryPointer]=8;
guessAryPointer++;
}
System.out.println("arryGuess: "+arryGuess[guessAryPointer-1]);
}
System.out.println("guessAryPointer= "+ ( guessAryPointer-1));
System.out.println("arryGuess: "+arryGuess[guessAryPointer-1]);
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}