Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.BufferedImage;
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- import javax.swing.border.LineBorder;
- import java.util.*;
- public class GridTest extends JFrame implements ActionListener{
- private static ArrayList<Integer> primes;
- private static int maxPrime = 1000;
- private static ArrayList<GridCell> gridCells;
- private static GridCell nextCell;
- private static JPanel gui;
- private static GridTest cb;
- private int size;
- private int fStore;
- private JButton[][] unitSquares;
- private JPanel xplusnSquare;
- private JLabel message;
- private static JFrame f = new JFrame("(x+n)(x+n)");;
- private boolean thingsSet = false;
- private JToolBar tools;
- public GridTest(int sizeSend) {
- int size = sizeSend;
- initializeVars(size);
- initializeGui();
- }
- public void initializeVars(int size){
- this.gui = new JPanel(new BorderLayout(0,0));
- this.size = size;
- this.unitSquares = new JButton[size][size];
- this.message = new JLabel(nextCell.print());
- }
- public static void initializeCells(){
- primes = new ArrayList<Integer>();
- for(int i=79; i<maxPrime; i++){
- if(isPrime(i)){
- primes.add(i);
- }
- }
- gridCells = new ArrayList<GridCell>();
- for(int j=0; j<primes.size(); j++){
- for(int k=0; k<j; k++){
- if(primes.get(k) != primes.get(j)){
- GridCell testCell = new GridCell(primes.get(k), primes.get(j));
- if(testCell.oddxplusnsquared() && testCell.n()%2==1){
- //We're only dealing with odd n even x
- gridCells.add(testCell);
- }
- }
- }
- }
- }
- public void setGui(){
- if(thingsSet != true){
- gui.setBorder(new EmptyBorder(5, 5, 5, 5));
- gui.add(tools, BorderLayout.PAGE_START);
- }
- gui.add(xplusnSquare);
- }
- public final void initializeGui() {
- if(tools == null){
- tools = new JToolBar();
- tools.setFloatable(false);
- JButton keepxplusnupButton = new JButton("Higher n same (x+n)");
- keepxplusnupButton.setActionCommand("keepxplusnup");
- keepxplusnupButton.addActionListener(this);
- JButton keepxplusndownButton = new JButton("Lower n same (x+n)");
- keepxplusndownButton.setActionCommand("keepxplusndown");
- keepxplusndownButton.addActionListener(this);
- tools.add(keepxplusnupButton);
- tools.add(keepxplusndownButton);
- tools.addSeparator();
- tools.add(message);
- }
- xplusnSquare = new JPanel(new GridBagLayout());
- xplusnSquare.setBorder(new LineBorder(Color.WHITE));
- Insets buttonMargin = new Insets(0,0,0,0);
- for (int ii = 0; ii < unitSquares.length; ii++) {
- for (int jj = 0; jj < unitSquares[ii].length; jj++) {
- JButton b = new JButton();
- int pixels = 650/size;
- b.setPreferredSize(new Dimension(pixels, pixels));
- ImageIcon icon = new ImageIcon(new BufferedImage(pixels, pixels, BufferedImage.TYPE_INT_ARGB));
- b.setIcon(icon);
- unitSquares[jj][ii] = b;
- GridBagConstraints c = new GridBagConstraints();
- c.gridx = ii;
- c.gridy = jj;
- /*
- * This is where I'm drawing the triangles. It's a whole bunch of code so it could probably be moved to
- * its own method but whatever.
- */
- if(nextCell.xplusn() == 3){
- //if it's three then the center will be black and the eight remaining squares will be the triangles
- //these don't need sophisticated border drawing to make triangles out of them
- b.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black)); //directly left of center
- b.setBackground(Color.CYAN);
- //if you're in the middle, make it black
- if(ii==((size-1)/2) && jj==((size-1)/2)){
- b.setBackground(Color.BLACK);
- }
- } else {
- //drawing the triangles
- if(jj==((size-1)/2) && ii==(((size-1)/2)-1)){
- b.setBorder(BorderFactory.createMatteBorder(1,0,1,0, Color.black)); //directly left of center
- } else if(jj==((size-1)/2) && ii<(((size-1)/2)-1) && ii>0){
- b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //left of center to before left edge
- } else if(((((size-1)/2)-jj)+(((size-1)/2)-ii)==1) && jj>ii && ii>0){
- b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //diagonal left and down
- } else if(ii==(((size-1)/2)-1) && jj>((size-1)/2) && jj<size-1){
- b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //downwards directly left of center
- } else if(ii==0 && jj==((size-1)/2)){
- b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //left edge center corner
- } else if(ii==0 && jj>((size-1)/2) && jj<(size-2)){
- b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //left edge downwards from center
- } else if(ii==0 && jj==(size-2)){
- b.setBorder(BorderFactory.createMatteBorder(0,1,1,1, Color.black)); //left bottom corner of triangle (2nd last square)
- } else if(ii==0 && jj==(size-1)){
- b.setBorder(BorderFactory.createMatteBorder(0,1,1,0, Color.black)); //left bottom corner
- } else if(ii>0 && ii<(((size-1)/2)-1) && jj==(size-1)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //bottom edge before center
- } else if(ii==(((size-1)/2)-1) && jj==(size-1)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //bottom left inner corner before center
- } else if(jj==((size-1)/2) && ii==(((size-1)/2)+1)){
- b.setBorder(BorderFactory.createMatteBorder(1,0,1,0, Color.black)); //directly right of center
- } else if(jj==((size-1)/2) && ii>(((size-1)/2)-1) && ii<(size-1)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //right of center to before right edge
- } else if(((jj-((size-1)/2))+(ii-((size-1)/2))==1) && jj<ii && jj>0){
- if(ii==(size-1)){
- b.setBorder(BorderFactory.createMatteBorder(1,1,0,1, Color.black)); //diagonal right and up
- } else {
- b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //diagonal right and up
- }
- } else if(ii==(((size-1)/2)+1) && jj<((size-1)/2) && jj>0){
- b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //upwards directly right of center
- } else if(ii==(size-1) && jj==((size-1)/2)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //right edge center corner
- } else if(ii==(size-1) && jj<0 && jj>(size-2)){
- b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //right edge upwards from center
- } else if(jj==0 && ii>(((size-1)/2)+1) && ii<(size-1)){
- b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //top right
- } else if(ii==(size-1) && jj==0){
- b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //diagonal right and up
- } else if(ii==(((size-1)/2)+1) && jj==0){
- b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //diagonal right and up
- } else if(ii==(size-1) && jj>1 && jj<((size-1)/2)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //diagonal right and up
- } else if(jj==(((size-1)/2)+1) && ii==((size-1)/2)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //directly below center
- } else if(jj==(size-1) && ii>=((size-1)/2) && ii<(size-2)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //bottom below center
- } else if(jj==(size-1) && ii==(size-2)){
- b.setBorder(BorderFactory.createMatteBorder(1,0,1,1, Color.black)); //directly below center
- } else if(ii-jj==-1 && ii>((size-1)/2) && jj>((size-1)/2)){
- b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //diagonal right and down
- } else if(jj==(size-1) && ii==(size-1)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //bottom right corner
- } else if(jj>((size-1)/2) && jj<(size-1) && ii==(size-1)){
- b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //right edge downwards
- } else if(jj==0 && ii==0){
- b.setBorder(BorderFactory.createMatteBorder(1,1,0,1, Color.black)); //top corner
- } else if(ii==0 && jj>0 && jj<((size-1)/2)){
- b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //left top side edge
- } else if(jj==0 && ii>0 && ii<(((size-1)/2)+1)){
- b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //left top top edge
- } else if(ii==jj && ii>0 && ii<((size-1)/2) && jj>0 && jj<((size-1)/2)){
- b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //directly below center
- } else {
- b.setBorder(BorderFactory.createMatteBorder(0,0,0,0, Color.black)); //inner parts blank
- }
- /*
- * Now I need to set the colours
- * nn+2d(n-1)+f-1
- * nn is purple (in this case Java.Color.MAGENTA)
- * 2d(n-1) is red
- * f is blue (in this case Java.Color.CYAN)
- */
- b.setBackground(Color.WHITE);
- //nn+2d(n-1)+f-1
- //If n=1 there's no nn because of the -1 (the black square in the middle
- //If n=1 there's no 2d(n-1) since n-1=0
- //So if n=1 you have to fill the whole square with f
- if(nextCell.xplusn() != 1){
- //if x+n=1, it's just the one black square, so it's already done
- if(nextCell.n() == 1){
- //everything other than the center is f-coloured
- if(!(ii==((size-1)/2) && jj==((size-1)/2))){
- b.setBackground(Color.CYAN);
- }
- } else {
- int offset = (((nextCell.n())-1)/2)+1;
- int nextrowlength = ((nextCell.n())+1)/2;
- int n = 0;
- if(nextCell.n()%2==0){
- n = nextCell.n() - 1;
- fStore = (nextCell.f()*-1) + (2*n) + 1;
- } else {
- n = nextCell.n();
- fStore = (nextCell.f()*-1);
- }
- int toput = fStore;
- //it needs to be set up so that the n square is around the outside of the center black square
- //it'll be (n-1)/2 away from the center
- if(ii>=(((size-1)/2)-((n-1)/2))
- && ii<=(((size-1)/2)+((n-1)/2))
- && jj>=(((size-1)/2)-(n-1)/2)
- && jj<=(((size-1)/2)+(n-1)/2)){
- //everything here is nn-coloured
- b.setBackground(Color.MAGENTA);
- } else {
- b.setBackground(colouringF(ii,jj,toput,offset,nextrowlength,n));
- }
- }
- }
- //if you're in the middle, make it black
- if(ii==((size-1)/2) && jj==((size-1)/2)){
- b.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black)); //diagonal left and down
- b.setBackground(Color.BLACK);
- }
- if(b.getBackground() == Color.WHITE){
- b.setBackground(Color.RED);
- }
- }
- xplusnSquare.add(unitSquares[jj][ii], c);
- }
- }
- setGui();
- }
- public Color colouringF(int ii, int jj, int toput, int offset, int nextrowlength, int n){
- while(toput>0){
- //then somehow I need to figure out how to put f in there
- //everything else will be 2d(n-1) so I guess that'll just be another else excluding center
- //so first I need to find f%8
- //fdiv8 is how many to put in each triangle
- //the length of the part of the triangle outside of nn will be (n+1)/2
- //e.g. if n=19, the closest row of the triangles will be (19+1)/2=10 long
- //if f/8 is less than that, you can just go from wherever you drew a line between triangles
- //and keep going in the opposite direction
- //if f/8 is greater than that, you'll need to fill the whole row for eah triangle and then
- //do the same thing you did for f/8<((n+1)/2) but with the next row out
- //man this sounds really hard, fucking hell why am I doing this
- if(toput <= (nextrowlength*8)){
- //let's say you're putting 24 in
- //in the first triangle, it'd be the first, the ninth and the seventeenth
- //so you'd go "if you're in that set of squares and the distance from here to origin
- //times 8 +1 (+1 for first triangle) is less than or equal to toput
- if(
- ((ii<=((size-1)/2)) && (ii>(((size-1)/2)-offset)) && (jj==(((size-1)/2)-offset)))
- &&
- (((((((size-1)/2)-ii)*8)+1)<=toput))
- ){
- return Color.CYAN;
- } else if(
- ((jj<=(((size-1)/2))-1) && (jj>(((size-1)/2)-(offset+1))) && ii==(((size-1)/2)-offset))
- && (
- (((((((size-1)/2)-1)-jj)*8)+2)<=toput))
- ){
- return Color.CYAN;
- } else if(
- ((jj>=(((size-1)/2))) && (jj<(((size-1)/2)+(offset))) && ii==(((size-1)/2)-offset))
- && (
- ((((jj-((size-1)/2))*8)+3)<=toput))
- ){
- return Color.CYAN;
- } else if(
- ((ii<=(((size-1)/2)-1)) && (ii>(((size-1)/2)-(offset+1))) && jj==(((size-1)/2)+offset))
- && (
- (((((((size-1)/2)-1)-ii)*8)+4)<=toput))
- ){
- return Color.CYAN;
- } else if(
- ((ii>=(((size-1)/2))) && (ii<(((size-1)/2)+(offset))) && jj==(((size-1)/2)+offset))
- && (
- (((((ii-((size-1)/2)))*8)+5)<=toput))
- ){
- return Color.CYAN;
- } else if(
- ((jj>=(((size-1)/2)+1)) && (jj<(((size-1)/2)+1+(offset))) && ii==(((size-1)/2)+offset))
- && (
- ((((jj-(((size-1)/2)+1))*8)+6)<=toput))
- ){
- return Color.CYAN;
- } else if(
- ((jj<=(((size-1)/2))) && (jj>(((size-1)/2)-(offset))) && ii==(((size-1)/2)+offset))
- && (
- (((((((size-1)/2)-jj))*8)+7)<=toput))
- ){
- return Color.CYAN;
- } else if(
- ((ii>(((size-1)/2))) && (ii<(((size-1)/2)+1+(offset))) && jj==(((size-1)/2)-offset))
- && (
- ((((ii-(((size-1)/2)+1))*8)+8)<=toput))
- ){
- return Color.CYAN;
- } else {
- return Color.RED;
- }
- } else {
- //fill the rows with f-colour
- if(((ii==(((size-1)/2)-(nextrowlength))) && (jj>=((((size-1)/2))-(nextrowlength))) && (jj<=((((size-1)/2))+(nextrowlength))))
- ||((ii==(((size-1)/2)+(nextrowlength))) && (jj>=((((size-1)/2))-(nextrowlength))) && (jj<=((((size-1)/2))+(nextrowlength))))
- ||((jj==(((size-1)/2)-(nextrowlength))) && (ii>=((((size-1)/2))-(nextrowlength))) && (ii<=((((size-1)/2))+(nextrowlength))))
- ||((jj==(((size-1)/2)+(nextrowlength))) && (ii>=((((size-1)/2))-(nextrowlength))) && (ii<=((((size-1)/2))+(nextrowlength))))
- ){
- return Color.CYAN;
- }
- offset++;
- nextrowlength++;
- toput = toput - ((nextrowlength-1)*8);
- }
- }
- return Color.RED;
- }
- public void actionPerformed(ActionEvent e) {
- if ("keepxplusnup".equals(e.getActionCommand())) {
- nextCell = nextCell.keepxplusnup();
- initializeVars(nextCell.xplusn());
- initializeGui();
- startThing();
- }
- if ("keepxplusndown".equals(e.getActionCommand())) {
- nextCell = nextCell.keepxplusndown();
- initializeVars(nextCell.xplusn());
- initializeGui();
- startThing();
- }
- }
- public static boolean isPrime(int n){
- if(n==1 || n==2 || n==4){
- return false;
- }
- for(int i=2; i<n; i++){
- if(n%i==0){
- return false;
- }
- }
- return true;
- }
- public JComponent getGui(){
- return gui;
- }
- public static void main(String[] args) {
- Runnable r = new Runnable() {
- @Override
- public void run() {
- startThing();
- }
- };
- SwingUtilities.invokeLater(r);
- }
- public static void startThing(){
- if(cb != null){ //if you're changing the triangles
- cb = new GridTest(nextCell.xplusn());
- f.getContentPane().removeAll();
- f.add(cb.getGui());
- f.setMinimumSize(f.getSize());
- f.pack();
- f.setSize(725,725);
- } else { //initial triangles
- initializeCells();
- gui = null;
- Scanner scan = new Scanner(System.in);
- int a = scan.nextInt();
- int b = scan.nextInt();
- nextCell = new GridCell(a, b);
- cb = new GridTest(nextCell.xplusn());
- f.add(cb.getGui());
- f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- f.setLocationByPlatform(true);
- f.pack();
- f.setSize(725,725);
- f.setMinimumSize(f.getSize());
- f.setVisible(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment