Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n =import javax.swing.*;
- import java.awt.*;
- import java.util.Random;
- import java.util.ArrayList;
- import java.awt.event.*;
- public class ColorsAndCombinations
- {
- public static JFrame window;
- public static JPanel display;
- public static JPanel controls;
- public static int board[];
- public static int boardStatistics[] = {0,0,0,0};
- public static ArrayList<int[]> selected = new ArrayList<int[]>();
- public static class ButtonListener implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent event)
- {
- if(event.getActionCommand().equals("generate"))
- {
- board = null;
- for(int i = 0; i < boardStatistics.length; ++i)
- {
- boardStatistics[i] = 0;
- }
- selected.clear();
- populateBoardRandom();
- getStatistics();
- getCombinations();
- window.repaint();
- }
- }
- }
- public static class DisplayBoard extends Component
- {
- public void paint(Graphics graphics)
- {
- int size = getWidth() / board.length;
- int offset = getHeight() - size;
- offset /= 2;
- graphics.translate(0,offset);
- int i = 0;
- for(int element : board)
- {
- switch(element)
- {
- case 0:
- graphics.setColor(Color.RED);
- graphics.fillRect(i * size,0,size,size);
- break;
- case 1:
- graphics.setColor(Color.BLUE);
- graphics.fillRect(i * size,0,size,size);
- break;
- case 2:
- graphics.setColor(Color.PINK);
- graphics.fillRect(i * size,0,size,size);
- break;
- case 3:
- graphics.setColor(Color.GREEN);
- graphics.fillRect(i * size, 0, size, size);
- break;
- }
- graphics.setColor(Color.BLACK);
- graphics.drawRect(i * size,0,size,size);
- ++i;
- }
- for(i = 0; i < selected.size(); ++i)
- {
- int scale = selected.get(i)[1] - selected.get(i)[0] + 1;
- graphics.setColor(Color.YELLOW);
- graphics.drawRect(size * selected.get(i)[0],0,size*scale,size);
- graphics.drawLine(size * selected.get(i)[0],0,(selected.get(i)[1]+1) * size ,size);
- graphics.drawLine(size * selected.get(i)[0],size,size * (selected.get(i)[1] +1), 0);
- }
- graphics.setColor(Color.RED);
- graphics.drawString(boardStatistics[0] + " " ,0,size + 25);
- graphics.setColor(Color.BLUE);
- graphics.drawString(boardStatistics[1] + " " ,25,size + 25);
- graphics.setColor(Color.PINK);
- graphics.drawString(boardStatistics[2] + " " ,50,size + 25);
- graphics.setColor(Color.GREEN);
- graphics.drawString(boardStatistics[3] + " " ,75,size + 25);
- }
- @Override
- public Dimension getPreferredSize()
- {
- return new Dimension(display.getWidth(),display.getHeight());
- }
- }
- public static void createWindow(String name, int resX, int resY, boolean resizeable)
- {
- window = new JFrame(name);
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setSize(resX,resY);
- window.setMinimumSize(new Dimension(resX,resY));
- window.setResizable(resizeable);
- window.setVisible(true);
- }
- public static void addControls()
- {
- controls = new JPanel();
- JButton generate = new JButton("Generate");
- generate.setActionCommand("generate");
- generate.addActionListener(new ButtonListener());
- controls.add(generate);
- window.add(controls,BorderLayout.PAGE_END);
- }
- public static void addDisplay()
- {
- display = new JPanel();
- display.add(new DisplayBoard());
- window.add(display,BorderLayout.CENTER);
- }
- public static void populateBoardStatic()
- {
- board = new int[]{0,0,0,1,3,3,3,3,2,1,1,1};
- }
- public static void populateBoardRandom()
- {
- Random rand = new Random();
- board = new int[12];
- for(int i = 0; i < board.length; ++i)
- {
- board[i] = rand.nextInt(4);
- }
- }
- public static void selectCombination(int indexOne, int indexTwo)
- {
- selected.add(new int[]{indexOne,indexTwo});
- }
- public static void getStatistics()
- {
- for(int i = 0; i < board.length; ++i)
- {
- if (board[i] == 0)
- boardStatistics[0]++;
- if (board[i] == 1)
- boardStatistics[1]++;
- if (board[i] == 2)
- boardStatistics[2]++;
- if (board[i] == 3)
- boardStatistics[3]++;
- }
- }
- public static void getCombinations()
- {
- for(int i = 0; i < board.length - 1; ++i)
- {
- int offset = 1;
- int c = 1; //c shouldn't be initialized to 0
- offset = c - 1 - i; //calculate offset outside of while loop, inside of for loop
- while(board[i] == board[c])
- {
- c++;
- }
- }
- }
- public static void main(String[] args)
- {
- populateBoardStatic();
- createWindow("Colors and Combinations",800,600,true);
- addControls();
- addDisplay();
- window.pack();
- getCombinations();
- getStatistics();
- window.repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement