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 GridCell nextCell;
- private static GridCell natransform;
- private static GridCell bigncell;
- private static GridCell displayCell;
- private static JPanel gui;
- private static GridTest cb;
- private int size;
- private int fStore;
- private int pixels;
- private JButton[][] unitSquares;
- private JPanel xplusnSquare;
- private JPanel dplusnSquare;
- private JLabel message;
- private static JFrame f = new JFrame("(x+n)(x+n) and (d+n)(d+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(displayCell.print());
- }
- public void setGui(){
- if(thingsSet != true){
- gui.setBorder(new EmptyBorder(5, 5, 5, 5));
- gui.add(tools, BorderLayout.PAGE_START);
- }
- JPanel container = new JPanel();
- container.setLayout(new GridLayout(1,2));
- container.add(xplusnSquare);
- container.add(dplusnSquare);
- gui.add(container);
- }
- 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);
- JButton naButton = new JButton("na (if x+n odd)");
- naButton.setActionCommand("na");
- naButton.addActionListener(this);
- JButton bignButton = new JButton("bigN (if x+n odd)");
- bignButton.setActionCommand("bign");
- bignButton.addActionListener(this);
- JButton origButton = new JButton("Original");
- origButton.setActionCommand("orig");
- origButton.addActionListener(this);
- tools.add(keepxplusnupButton);
- tools.add(keepxplusndownButton);
- tools.add(naButton);
- tools.add(bignButton);
- tools.addSeparator();
- tools.add(message);
- tools.addSeparator();
- tools.add(origButton);
- }
- xplusnSquare = new JPanel(new GridBagLayout());
- xplusnSquare.setBorder(new LineBorder(Color.WHITE));
- dplusnSquare = new JPanel(new GridBagLayout());
- dplusnSquare.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();
- pixels = 630/displayCell.dplusn();
- 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(displayCell.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(displayCell.xplusn() != 1){
- //if x+n=1, it's just the one black square, so it's already done
- if(displayCell.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 = (((displayCell.n())-1)/2)+1;
- int nextrowlength = ((displayCell.n())+1)/2;
- int n = 0;
- if(displayCell.n()%2==0){
- n = displayCell.n() - 1;
- fStore = (displayCell.f()*-1) + (2*n) + 1;
- } else {
- n = displayCell.n();
- fStore = (displayCell.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);
- }
- }
- createdplusnSquare();
- setGui();
- }
- public void createdplusnSquare(){
- int sizeD = displayCell.dplusn();
- for(int ii = 0; ii<sizeD; ii++){
- for(int jj = 0; jj<sizeD; jj++){
- JButton b = new JButton();
- b.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.WHITE));
- b.setPreferredSize(new Dimension(pixels, pixels));
- b.setBackground(colouringdplusnSquare(ii, jj, sizeD));
- ImageIcon icon = new ImageIcon(new BufferedImage(pixels, pixels, BufferedImage.TYPE_INT_ARGB));
- b.setIcon(icon);
- GridBagConstraints c = new GridBagConstraints();
- c.gridx = ii;
- c.gridy = jj;
- dplusnSquare.add(b, c);
- }
- }
- }
- public Color colouringdplusnSquare(int ii, int jj, int sizeD){
- //if ii and jj are within sizeD and sizeD-displayCell.d(), return black
- if(ii>=(displayCell.n()) && jj>=(displayCell.n())){
- return Color.BLACK; //dd
- } else if((ii==displayCell.n()-1) && (jj==displayCell.n()-1)){
- if(displayCell.e()%2==0){
- return Color.CYAN; //if e's even the d+1 corner will be blue for f
- } else {
- return Color.GREEN; //otherwise it's green for e
- }
- } else if((ii>=displayCell.n() && jj==displayCell.n()-1) || (jj>=displayCell.n() && ii==displayCell.n()-1)){
- int eD = displayCell.e();
- int fD = (displayCell.f())*-1;
- int fdiv2 = fD/2;
- int ediv2 = eD/2;
- //you want e from the bottom up and the right leftwards
- //so it's going to be ii and jj between sizeD (exc) and sizeD-ediv2, right? I think
- if((ii<sizeD && ii>=sizeD-ediv2 && jj==displayCell.n()-1) || (jj<sizeD && jj>=sizeD-ediv2 && ii==displayCell.n()-1)){
- return Color.GREEN;
- } else {
- return Color.CYAN;
- }
- } else if(((ii<=(displayCell.n()-1)) && (jj<=(displayCell.n()-1))) && !(ii==displayCell.n()-1 && jj==displayCell.n()-1) ){
- return Color.MAGENTA; //nn-1
- }
- return Color.RED;
- }
- 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 static void initializeCell(GridCell newOne){
- nextCell = newOne;
- int natransforma = nextCell.a()*nextCell.n();
- int natransformb = natransforma + (2*nextCell.x()) + 2;
- natransform = new GridCell(natransforma, natransformb);
- bigncell = new GridCell(1, newOne.c());
- }
- public void actionPerformed(ActionEvent e) {
- if ("keepxplusnup".equals(e.getActionCommand())) {
- GridCell newOne = nextCell.keepxplusnup();
- displayCell = newOne;
- initializeCell(newOne);
- initializeVars(nextCell.xplusn());
- initializeGui();
- startThing();
- }
- if ("keepxplusndown".equals(e.getActionCommand())) {
- GridCell newOne = nextCell.keepxplusndown();
- displayCell = newOne;
- initializeCell(newOne);
- initializeVars(nextCell.xplusn());
- initializeGui();
- startThing();
- }
- if ("na".equals(e.getActionCommand())) {
- if(natransform.xplusn()%2==0){
- System.out.println("x+n is even");
- return;
- }
- displayCell = natransform;
- initializeVars(displayCell.xplusn());
- initializeGui();
- startThing();
- }
- if ("bign".equals(e.getActionCommand())) {
- if(bigncell.xplusn()%2==0){
- System.out.println("x+n is even");
- return;
- }
- displayCell = bigncell;
- initializeVars(displayCell.xplusn());
- initializeGui();
- startThing();
- }
- if ("orig".equals(e.getActionCommand())) {
- displayCell = nextCell;
- initializeVars(displayCell.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(displayCell.xplusn());
- f.getContentPane().removeAll();
- f.add(cb.getGui());
- f.setMinimumSize(f.getSize());
- f.pack();
- f.setSize(1300,725);
- } else { //initial triangles
- gui = null;
- Scanner scan = new Scanner(System.in);
- int a = 0;
- int b = 0;
- while(a==0 || b==0){
- System.out.println("a:");
- a = scan.nextInt();
- System.out.println("b:");
- b = scan.nextInt();
- if(a%2==0 || b%2==0 || a>b){
- System.out.println("Invalid a and/or b");
- a=0;
- b=0;
- }
- }
- GridCell newOne = new GridCell(a, b);
- if(newOne.xplusn()%2==0){
- System.out.println("x+n is even, defaulting");
- newOne = new GridCell(1,1);
- }
- initializeCell(newOne);
- displayCell = newOne;
- cb = new GridTest(displayCell.xplusn());
- f.add(cb.getGui());
- f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- f.setLocationByPlatform(true);
- f.pack();
- f.setSize(1300,725);
- f.setMinimumSize(f.getSize());
- f.setVisible(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment