Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.awt.AlphaComposite;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- class cliquesGame4 {
- public static void main(String[] args) {
- System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
- int border = 40;
- int squareSize = 665;
- int numberOfStudents = 17;
- int minConnections = 6;
- int maxConnections = 9;
- long seed = new Random().nextLong();
- new mainBody(minConnections, maxConnections, numberOfStudents, seed, squareSize, border);
- }
- }
- class mainBody extends JFrame implements ActionListener {
- int numberOfStudents, minConnections,maxConnections,squareSize,
- border,restartMinConnections,restartMaxConnections,restartNumberOfStudents,restartSquareSize;
- long seed;
- boolean lock= true;
- double tileSize;
- Color setColor = Color.red;
- Color unsetColor = new Color(144,144,144);
- Random rnd;
- Rectangle rectangleA = new Rectangle();
- Rectangle rectangleB = new Rectangle();
- ArrayList<Integer> toBeSwitchedX = new ArrayList<Integer>();
- ArrayList<Integer> toBeSwitchedY = new ArrayList<Integer>();
- Student[] arrayOfStudents;
- JPanel jPanel;
- JTextField textField, minTextField, maxTextField, studentsTextField, squareSizeTextField;
- JustButton[] jbX;
- JustButton[] jbY;
- MyButton[][] myButton;
- JCheckBox checkBox;
- public void restart() {
- ensureConsistency();
- minConnections=Integer.parseInt(minTextField.getText());
- maxConnections=Integer.parseInt(maxTextField.getText());
- numberOfStudents=Integer.parseInt(studentsTextField.getText());
- squareSize=Integer.parseInt(squareSizeTextField.getText());
- if (squareSize<550){
- squareSize=550;
- squareSizeTextField.setText("550");
- }
- this.dispose();
- new mainBody(minConnections, maxConnections,numberOfStudents,seed,squareSize,border);
- }
- public mainBody(int minConnections, int maxConnections, int numberOfStudents, long seed, int squareSize,int border) {
- this.minConnections = minConnections;
- this.maxConnections = maxConnections;
- this.numberOfStudents = numberOfStudents;
- this.seed = seed;
- this.squareSize = squareSize;
- this.border = border;
- restartMinConnections= minConnections;
- restartMaxConnections= maxConnections;
- restartNumberOfStudents= numberOfStudents;
- restartSquareSize= squareSize;
- tileSize = (double)squareSize/(double)(numberOfStudents+1);
- rnd = new Random(seed);
- createStudents(numberOfStudents);
- randomlyInterconnectStudents(minConnections, maxConnections);
- giveEachStudentColors();
- setupWindow();
- plotGraphicalRepresentation();
- }
- public void createStudents(int numberOfStudents) {
- arrayOfStudents = new Student[numberOfStudents];
- for (int i=0; i<numberOfStudents; i++) {
- arrayOfStudents[i] = new Student();
- }
- }
- public void randomlyInterconnectStudents(int minConnections, int maxConnections){
- for (int i=0; i< numberOfStudents; i++) {
- int numberOfConnections = rnd.nextInt(maxConnections - (minConnections-1)) + minConnections;
- arrayOfStudents[i].arrayOfConnectedStudents = new int[numberOfConnections];
- }
- int flag;
- int studentToBeAdded;
- int studentsContained;
- for (int i=0; i<numberOfStudents; i++) {
- studentsContained = arrayOfStudents[i].arrayOfConnectedStudents.length;
- arrayOfStudents[i].arrayOfConnectedStudents = new int[studentsContained];
- for(int x=0; x<studentsContained; x++) {
- // lazy way of making sure no same students are contained in the array
- do {
- studentToBeAdded= rnd.nextInt(numberOfStudents)+1;
- flag = 0;
- for(int o=0; o<studentsContained; o++) {
- if (arrayOfStudents[i].arrayOfConnectedStudents[o] == studentToBeAdded) {flag = 1;}
- }
- }
- while (flag ==1);
- arrayOfStudents[i].arrayOfConnectedStudents[x]=studentToBeAdded;
- arrayOfStudents[studentToBeAdded-1].arrayListFromConnections.add(i+1);
- }
- }
- }
- public void giveEachStudentColors() {
- for (int i=0; i<numberOfStudents; i++) {
- int studentsContained = arrayOfStudents[i].arrayOfConnectedStudents.length;
- int studentsFromConnections = arrayOfStudents[i].arrayListFromConnections.size();
- arrayOfStudents[i].colorOfStudent = BigInteger.valueOf(1).shiftLeft(i);
- for (int x=0; x<studentsContained; x++) {
- arrayOfStudents[i].colorOfStudent = arrayOfStudents[i].colorOfStudent.or(BigInteger.valueOf(1).shiftLeft(arrayOfStudents[i].arrayOfConnectedStudents[x]-1));
- }
- for (int x=0; x<studentsFromConnections; x++) {
- arrayOfStudents[i].colorOfStudent = arrayOfStudents[i].colorOfStudent.or(BigInteger.valueOf(1).shiftLeft(arrayOfStudents[i].arrayListFromConnections.get(x)-1));
- }
- // System.out.println(String.format("%"+Integer.toString(numberOfStudents)+"s", arrayOfStudents[i].colorOfStudent.toString(2)).replace(' ', '0'));
- String str = String.format("%"+Integer.toString(numberOfStudents)+"s", arrayOfStudents[i].colorOfStudent.toString(2)).replace(' ', '0');
- arrayOfStudents[i].colorString = str;
- arrayOfStudents[i].arrayColorOfStudent = new int[numberOfStudents];
- for (int x=0; x<numberOfStudents; x++) {
- arrayOfStudents[i].arrayColorOfStudent[x] = Character.getNumericValue(str.charAt(numberOfStudents-x-1));
- }
- }
- }
- public void setupWindow() {
- this.setName("cliqueGame");
- jPanel = new JPanel();
- jPanel.setLayout(null);
- jPanel.setBackground(Color.black);
- checkBox = new JCheckBox("interlock Columns/Rows",true);
- checkBox.addActionListener(this);
- checkBox.setActionCommand("lock");
- checkBox.setBounds(0,(int)(squareSize+2),170,border);
- jPanel.add(checkBox);
- minTextField = new JTextField(Integer.toString(restartMinConnections));
- maxTextField = new JTextField(Integer.toString(restartMaxConnections));
- studentsTextField = new JTextField(Integer.toString(restartNumberOfStudents));
- squareSizeTextField = new JTextField(Integer.toString(restartSquareSize));
- minTextField.setActionCommand("miT");
- maxTextField.setActionCommand("maT");
- studentsTextField.setActionCommand("studentsT");
- squareSizeTextField.setActionCommand("squareT");
- minTextField.addActionListener(this);
- maxTextField.addActionListener(this);
- studentsTextField.addActionListener(this);
- squareSizeTextField.addActionListener(this);
- minTextField.setBounds((int)450+2,(int)(squareSize+2),(squareSize-450)/4,border);
- maxTextField.setBounds((int)450+((squareSize-450)/4)*1+2,(int)(squareSize+2),(squareSize-450)/4,border);
- studentsTextField.setBounds((int)450+((squareSize-450)/4)*2+2,(int)(squareSize+2),(squareSize-450)/4,border);
- squareSizeTextField.setBounds((int)450+((squareSize-450)/4)*3+2,(int)(squareSize+2),(squareSize-450)/4,border);
- MyButton printMatrix = new MyButton(Color.green,Color.green,tileSize);
- printMatrix.setBounds((int)(tileSize*numberOfStudents)+1,0,(int)tileSize-3,(int)tileSize-3);
- printMatrix.addActionListener(this);
- printMatrix.setActionCommand("print");
- jPanel.add(printMatrix);
- jPanel.add(minTextField);
- jPanel.add(maxTextField);
- jPanel.add(studentsTextField);
- jPanel.add(squareSizeTextField);
- textField = new JTextField("enter seed here+enter: "+Long.toString(seed));
- textField.setActionCommand("enter");
- textField. setBounds(172,(int)(squareSize+2),450-172,border);;
- textField.addActionListener(this);
- jPanel.add(textField);
- this.add(jPanel);
- MyGlassPane glassPane = new MyGlassPane();
- this.setGlassPane(glassPane);
- glassPane.setVisible(true);
- this.setResizable(false);
- this.setSize((int)((tileSize)*(numberOfStudents+1)+5),(int)(tileSize*(numberOfStudents+1)+border+20));
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);// try {Thread.sleep(1000);}catch (Exception e){}
- }
- public void plotGraphicalRepresentation(){
- jbX = new JustButton[numberOfStudents];
- jbY = new JustButton[numberOfStudents];
- myButton = new MyButton[numberOfStudents][numberOfStudents];
- for(int i=0; i<numberOfStudents;i++) {
- jbX[numberOfStudents-i-1] = new JustButton(Integer.toString(numberOfStudents-i-1));
- jbY[i]= new JustButton(Integer.toString(i));
- jbX[numberOfStudents-i-1].setBorder(null);
- jbY[i].setBorder(null);
- jbX[numberOfStudents-i-1].setFont(new Font("Arial", Font.PLAIN, (int)(tileSize-tileSize/5)));
- jbY[i].setFont(new Font("Arial", Font.PLAIN, (int)(tileSize-tileSize/5)));
- jbX[numberOfStudents-i-1].setBounds((int)(i*tileSize),0,(int)tileSize-1,(int)tileSize-1);
- jbY[i].setBounds((int)(tileSize*numberOfStudents),(int)(tileSize+i*tileSize),(int)tileSize-1,(int)tileSize-1);
- jPanel.add(jbX[numberOfStudents-i-1]);
- jPanel.add(jbY[i]);
- jbX[numberOfStudents-i-1].addActionListener(this);
- jbX[numberOfStudents-i-1].setActionCommand(Integer.toString(numberOfStudents-i-1)+"x");
- jbY[i].addActionListener(this);
- jbY[i].setActionCommand(Integer.toString(i)+"y");
- }
- for (int i=0; i<numberOfStudents; i++) {
- for (int x=0; x<numberOfStudents; x++) {
- if (arrayOfStudents[i].colorString.charAt(x) == '1') {
- myButton[numberOfStudents-x-1][i] = new MyButton(setColor,setColor,tileSize);
- jPanel.add(myButton[numberOfStudents-x-1][i]);
- myButton[numberOfStudents-x-1][i].setBounds((int)(x*tileSize+1),(int)(tileSize+i*tileSize+1),(int)tileSize-3,(int)tileSize-3);
- myButton[numberOfStudents-x-1][i].addActionListener(this);
- myButton[numberOfStudents-x-1][i].setActionCommand("myButton");
- myButton[numberOfStudents-x-1][i].bit = 1;
- myButton[numberOfStudents-x-1][i].posX=numberOfStudents-x-1;
- myButton[numberOfStudents-x-1][i].posY=i;
- } else {
- myButton[numberOfStudents-x-1][i] = new MyButton(unsetColor, unsetColor,tileSize);
- jPanel.add( myButton[numberOfStudents-x-1][i]);
- myButton[numberOfStudents-x-1][i].setBounds((int)(x*tileSize+1),(int)(tileSize+i*tileSize+1),(int)tileSize-3,(int)tileSize-3);
- myButton[numberOfStudents-x-1][i].addActionListener(this);
- myButton[numberOfStudents-x-1][i].setActionCommand("myButton");
- myButton[numberOfStudents-x-1][i].posX=numberOfStudents-x-1;
- myButton[numberOfStudents-x-1][i].posY=i;
- }
- }
- }
- jPanel.repaint();
- repaint();
- }
- public void ensureConsistency() {
- restartNumberOfStudents = Integer.parseInt(studentsTextField.getText());
- restartMaxConnections = Integer.parseInt(maxTextField.getText());
- restartMinConnections = Integer.parseInt(minTextField.getText());
- if (restartMaxConnections > restartNumberOfStudents) {
- restartMaxConnections = restartNumberOfStudents;
- maxTextField.setText(Integer.toString(restartMaxConnections));
- }
- if (restartMinConnections > restartMaxConnections) {
- restartMinConnections = restartMaxConnections;
- minTextField.setText(Integer.toString(restartMaxConnections));
- }
- }
- MyButton highButton;
- int counter = 0;
- int highlightX = 0;
- int highlightY = 0;
- public void actionPerformed(ActionEvent e) {
- String str=e.getActionCommand();
- if (str.equals("myButton")){
- MyButton tempButton = (MyButton)e.getSource();
- int x = tempButton.posX;
- int y = tempButton.posY;
- if (tempButton.highlight == 0 && counter == 0) {
- highButton = tempButton;
- tempButton.switchHighlight();
- highlightX = x;
- highlightY = y;
- counter++;
- } else if (tempButton.highlight == 1 && counter == 1) {
- tempButton.switchHighlight();
- counter--;
- } else if (tempButton.highlight == 0 && counter ==1) {
- counter--;
- highButton.switchHighlight();
- if (highlightX < x ^ highlightX > x) {
- if (lock==true) {
- switchColumns(highlightX,x);
- switchRows(highlightX,x);
- } else {
- switchColumns(highlightX,x);
- }
- }
- if (highlightY < y ^ highlightY > y) {
- if (lock==true) {
- switchColumns(highlightY,y);
- switchRows(highlightY,y);
- } else {
- switchRows(highlightY,y);
- }
- }
- }
- return;
- }
- if (str.equals("print")){
- for (int y=0; y< numberOfStudents;y++){
- System.out.println();
- for (int x=0; x< numberOfStudents;x++){
- System.out.print(myButton[x][y].bit);
- }
- }
- return;
- }
- if (str.equals("miT")){
- String s = minTextField.getText().replaceAll("[^\\d]", "");
- minTextField.setText(s);
- restartMinConnections = Integer.parseInt(s);
- ensureConsistency();
- return;
- }
- if (str.equals("maT")){
- String s = maxTextField.getText().replaceAll("[^\\d]", "");
- maxTextField.setText(s);
- restartMaxConnections = Integer.parseInt(s);
- ensureConsistency();
- return;
- }
- if (str.equals("studentsT")){
- String s = studentsTextField.getText().replaceAll("[^\\d]", "");
- studentsTextField.setText(s);
- restartNumberOfStudents = Integer.parseInt(s);
- ensureConsistency();
- return;
- }
- if (str.equals("squareT")){
- String s = squareSizeTextField.getText().replaceAll("[^\\d]", "");
- squareSizeTextField.setText(s);
- restartSquareSize = Integer.parseInt(s);
- ensureConsistency();
- return;
- }
- if(str.equals("lock")) {
- if (lock==true){
- lock = false;
- return;
- } else {
- lock = true; }
- return;
- }
- if (str.equals("enter")){
- String s = textField.getText().replaceAll("[^\\d-]", "");
- int sl = s.length();
- if (s.length() == 0){seed = rnd.nextLong();restart();return;}
- if (s.charAt(0)=='-' && sl < 2) {seed = rnd.nextLong();restart();return;}
- s = s.substring(0,1)+s.substring(1,sl).replace("-", "");
- sl = s.length();
- BigInteger ba = new BigInteger(s);
- BigInteger bb = new BigInteger(Long.toString((long)Math.pow(2,63)));
- BigInteger bc = new BigInteger(Long.toString(-(long)Math.pow(2,63)-1));
- if ((ba.compareTo(bb))==1) {
- textField.setText(Long.toString((long)Math.pow(2,63)));
- } else if (ba.compareTo(bc) ==-1) {
- textField.setText(Long.toString(-(long)(Math.pow(2,63))));
- return;
- } else {
- if (sl >= 20 && (s.charAt(0) != '-')) {
- sl = 19;
- } else if (sl >= 20 && (s.charAt(0) == '-')){
- sl = 20;
- }
- seed = Long.parseLong(s.substring(0,sl));
- textField.setText(Long.toString(seed));
- restart();
- }
- }
- int m,n;
- if(str.contains("x")){
- int a = Integer.parseInt(str.substring(0,str.length()-1));
- if (toBeSwitchedX.contains(a)){
- toBeSwitchedX.remove((Integer)a);
- jbX[a].unsetBackground();
- return;
- } else {
- toBeSwitchedX.add(a);
- jbX[a].setBackground();
- if(toBeSwitchedX.size()==2) {
- jbX[toBeSwitchedX.get(0)].unsetBackground();
- jbX[toBeSwitchedX.get(1)].unsetBackground();
- n = toBeSwitchedX.get(0);
- m =toBeSwitchedX.get(1);
- if (lock == true) {
- switchColumns(m,n);
- switchRows(m,n);
- } else {
- switchColumns(m,n);
- }
- }
- }
- return;
- }
- if(str.contains("y")){
- int a = Integer.parseInt(str.substring(0,str.length()-1));
- if (toBeSwitchedY.contains(a)){
- toBeSwitchedY.remove((Integer)a);
- jbY[a].unsetBackground();
- return;
- } else {
- toBeSwitchedY.add(a);
- jbY[a].setBackground();
- if(toBeSwitchedY.size()==2) {
- jbY[toBeSwitchedY.get(0)].unsetBackground();
- jbY[toBeSwitchedY.get(1)].unsetBackground();
- m = toBeSwitchedY.get(0);
- n =toBeSwitchedY.get(1);
- if (lock == true) {
- switchColumns(m,n);
- switchRows(m,n);
- } else {
- switchRows(m,n);
- }
- }
- }
- return;
- }
- }
- public void switchColumns(int a, int b){
- for(int i=0; i<numberOfStudents; i++) {
- rectangleB = myButton[b][i].getBounds();
- rectangleA = myButton[a][i].getBounds();
- myButton[a][i].setBounds(rectangleB);
- myButton[b][i].setBounds(rectangleA);
- }
- toBeSwitchedX.clear();
- rectangleB = jbX[b].getBounds();
- rectangleA = jbX[a].getBounds();
- jbX[a].setBounds(rectangleB);
- jbX[b].setBounds(rectangleA);
- }
- public void switchRows(int a, int b) {
- for(int i=0; i<numberOfStudents; i++) {
- rectangleB = myButton[i][b].getBounds();
- rectangleA = myButton[i][a].getBounds();
- myButton[i][a].setBounds(rectangleB);
- myButton[i][b].setBounds(rectangleA);
- }
- toBeSwitchedY.clear();
- rectangleB = jbY[b].getBounds();
- rectangleA = jbY[a].getBounds();
- jbY[a].setBounds(rectangleB);
- jbY[b].setBounds(rectangleA);
- }
- class MyGlassPane extends JComponent{
- AlphaComposite composite;
- Graphics2D g2d;
- float alpha;
- int type;
- int i=1;
- int o;
- BasicStroke stroke;
- MyGlassPane() {
- setVisible(true);
- stroke = new BasicStroke(3);
- }
- public void paintComponent(Graphics g){
- g2d = (Graphics2D) g.create();
- alpha = 0.6f;
- type = AlphaComposite.SRC_OVER;
- composite = AlphaComposite.getInstance(type, alpha);
- g2d.setComposite(composite);
- g2d.setStroke(stroke);
- g2d.setColor(Color.black);
- g2d.drawLine((int)(squareSize-3-tileSize),(int)tileSize,0,(int)(squareSize-3));;
- if (i == 1) {
- o = counter;
- i = 0;
- }
- if (o!=counter) {
- i=1;
- this.repaint();
- }
- if (counter == 1) {
- g2d.setColor(Color.white);
- g2d.fillRect(highButton.getX(), 0,(int)tileSize,(int)(highButton.getY()));
- g2d.fillRect((int)(highButton.getX()+tileSize),(int)(highButton.getY()),(int)(squareSize-tileSize),(int)tileSize);
- }
- }
- }
- }
- class Student {
- BigInteger colorOfStudent;
- int[] arrayColorOfStudent;
- int[] arrayOfConnectedStudents;
- ArrayList<Integer> arrayListFromConnections = new ArrayList<Integer>();
- String colorString;
- }
- class MyButton extends JButton {
- double tileSize;
- Color backColor;
- Color color;
- String string;
- int bit;
- int posX;
- int posY;
- int highlight;
- MyButton(String str) {
- str = string;
- setText(str);
- }
- public MyButton(Color c, Color b,double size) {
- setBorder(null);
- setBackground(b);
- color = b;
- tileSize = size;
- }
- public void switchHighlight() {
- if (highlight == 0) {
- highlight = 1;
- if (bit==1){
- setColor(Color.magenta);
- }else {
- setColor(Color.cyan);
- }
- } else {
- highlight = 0;
- if (bit == 1) {
- setColor(Color.red);}
- else {
- setColor(new Color(144,144,144));
- }
- }
- }
- public void setColor(Color c){
- color = c;
- setBackground(c);
- setForeground(Color.white);
- }
- public Color getColor(){
- return color;
- }
- }
- class JustButton extends JButton {
- double tileSize;
- Color backColor;
- Color color = Color.red;
- String string;
- Color col = new Color(188,188,188);
- public JustButton(String str) {
- setBorder(null);
- setBackground(col);
- string = str;
- setText(string);
- }
- public void setBackground() {
- setBackground(Color.red);
- }
- public void unsetBackground() {
- setBackground(col);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement