Guest User

x+n and d+n squares

a guest
Oct 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.90 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.BufferedImage;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.border.LineBorder;
  8.  
  9. import java.util.*;
  10.  
  11. public class GridTest extends JFrame implements ActionListener{
  12.  
  13. private static ArrayList<Integer> primes;
  14. private static int maxPrime = 1000;
  15. private static GridCell nextCell;
  16. private static JPanel gui;
  17. private static GridTest cb;
  18. private int size;
  19. private int fStore;
  20. private int pixels;
  21. private JButton[][] unitSquares;
  22. private JPanel xplusnSquare;
  23. private JPanel dplusnSquare;
  24. private JLabel message;
  25. private static JFrame f = new JFrame("(x+n)(x+n) and (d+n)(d+n)");;
  26. private boolean thingsSet = false;
  27. private JToolBar tools;
  28.  
  29. public GridTest(int sizeSend) {
  30. int size = sizeSend;
  31. initializeVars(size);
  32. initializeGui();
  33. }
  34.  
  35. public void initializeVars(int size){
  36. this.gui = new JPanel(new BorderLayout(0,0));
  37. this.size = size;
  38. this.unitSquares = new JButton[size][size];
  39. this.message = new JLabel(nextCell.print());
  40. }
  41.  
  42. public void setGui(){
  43. if(thingsSet != true){
  44. gui.setBorder(new EmptyBorder(5, 5, 5, 5));
  45. gui.add(tools, BorderLayout.PAGE_START);
  46. }
  47.  
  48. JPanel container = new JPanel();
  49. container.setLayout(new GridLayout(1,2));
  50. container.add(xplusnSquare);
  51. container.add(dplusnSquare);
  52. gui.add(container);
  53. }
  54.  
  55. public final void initializeGui() {
  56. if(tools == null){
  57. tools = new JToolBar();
  58. tools.setFloatable(false);
  59. JButton keepxplusnupButton = new JButton("Higher n same (x+n)");
  60. keepxplusnupButton.setActionCommand("keepxplusnup");
  61. keepxplusnupButton.addActionListener(this);
  62. JButton keepxplusndownButton = new JButton("Lower n same (x+n)");
  63. keepxplusndownButton.setActionCommand("keepxplusndown");
  64. keepxplusndownButton.addActionListener(this);
  65. tools.add(keepxplusnupButton);
  66. tools.add(keepxplusndownButton);
  67. tools.addSeparator();
  68. tools.add(message);
  69. }
  70.  
  71. xplusnSquare = new JPanel(new GridBagLayout());
  72. xplusnSquare.setBorder(new LineBorder(Color.WHITE));
  73. dplusnSquare = new JPanel(new GridBagLayout());
  74. dplusnSquare.setBorder(new LineBorder(Color.WHITE));
  75.  
  76. Insets buttonMargin = new Insets(0,0,0,0);
  77. for (int ii = 0; ii < unitSquares.length; ii++) {
  78. for (int jj = 0; jj < unitSquares[ii].length; jj++) {
  79. JButton b = new JButton();
  80. pixels = 630/nextCell.dplusn();
  81. b.setPreferredSize(new Dimension(pixels, pixels));
  82.  
  83. ImageIcon icon = new ImageIcon(new BufferedImage(pixels, pixels, BufferedImage.TYPE_INT_ARGB));
  84. b.setIcon(icon);
  85. unitSquares[jj][ii] = b;
  86. GridBagConstraints c = new GridBagConstraints();
  87. c.gridx = ii;
  88. c.gridy = jj;
  89.  
  90.  
  91. /*
  92. * This is where I'm drawing the triangles. It's a whole bunch of code so it could probably be moved to
  93. * its own method but whatever.
  94. */
  95.  
  96.  
  97. if(nextCell.xplusn() == 3){
  98. //if it's three then the center will be black and the eight remaining squares will be the triangles
  99. //these don't need sophisticated border drawing to make triangles out of them
  100. b.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black)); //directly left of center
  101. b.setBackground(Color.CYAN);
  102. //if you're in the middle, make it black
  103. if(ii==((size-1)/2) && jj==((size-1)/2)){
  104. b.setBackground(Color.BLACK);
  105. }
  106.  
  107. } else {
  108.  
  109. //drawing the triangles
  110. if(jj==((size-1)/2) && ii==(((size-1)/2)-1)){
  111. b.setBorder(BorderFactory.createMatteBorder(1,0,1,0, Color.black)); //directly left of center
  112. } else if(jj==((size-1)/2) && ii<(((size-1)/2)-1) && ii>0){
  113. b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //left of center to before left edge
  114. } else if(((((size-1)/2)-jj)+(((size-1)/2)-ii)==1) && jj>ii && ii>0){
  115. b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //diagonal left and down
  116. } else if(ii==(((size-1)/2)-1) && jj>((size-1)/2) && jj<size-1){
  117. b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //downwards directly left of center
  118. } else if(ii==0 && jj==((size-1)/2)){
  119. b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //left edge center corner
  120. } else if(ii==0 && jj>((size-1)/2) && jj<(size-2)){
  121. b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //left edge downwards from center
  122. } else if(ii==0 && jj==(size-2)){
  123. b.setBorder(BorderFactory.createMatteBorder(0,1,1,1, Color.black)); //left bottom corner of triangle (2nd last square)
  124. } else if(ii==0 && jj==(size-1)){
  125. b.setBorder(BorderFactory.createMatteBorder(0,1,1,0, Color.black)); //left bottom corner
  126. } else if(ii>0 && ii<(((size-1)/2)-1) && jj==(size-1)){
  127. b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //bottom edge before center
  128. } else if(ii==(((size-1)/2)-1) && jj==(size-1)){
  129. b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //bottom left inner corner before center
  130.  
  131.  
  132. } else if(jj==((size-1)/2) && ii==(((size-1)/2)+1)){
  133. b.setBorder(BorderFactory.createMatteBorder(1,0,1,0, Color.black)); //directly right of center
  134. } else if(jj==((size-1)/2) && ii>(((size-1)/2)-1) && ii<(size-1)){
  135. b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //right of center to before right edge
  136. } else if(((jj-((size-1)/2))+(ii-((size-1)/2))==1) && jj<ii && jj>0){
  137. if(ii==(size-1)){
  138. b.setBorder(BorderFactory.createMatteBorder(1,1,0,1, Color.black)); //diagonal right and up
  139. } else {
  140. b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //diagonal right and up
  141. }
  142. } else if(ii==(((size-1)/2)+1) && jj<((size-1)/2) && jj>0){
  143. b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //upwards directly right of center
  144. } else if(ii==(size-1) && jj==((size-1)/2)){
  145. b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //right edge center corner
  146. } else if(ii==(size-1) && jj<0 && jj>(size-2)){
  147. b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //right edge upwards from center
  148. } else if(jj==0 && ii>(((size-1)/2)+1) && ii<(size-1)){
  149. b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //top right
  150. } else if(ii==(size-1) && jj==0){
  151. b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //diagonal right and up
  152. } else if(ii==(((size-1)/2)+1) && jj==0){
  153. b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //diagonal right and up
  154. } else if(ii==(size-1) && jj>1 && jj<((size-1)/2)){
  155. b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //diagonal right and up
  156.  
  157.  
  158. } else if(jj==(((size-1)/2)+1) && ii==((size-1)/2)){
  159. b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //directly below center
  160. } else if(jj==(size-1) && ii>=((size-1)/2) && ii<(size-2)){
  161. b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //bottom below center
  162. } else if(jj==(size-1) && ii==(size-2)){
  163. b.setBorder(BorderFactory.createMatteBorder(1,0,1,1, Color.black)); //directly below center
  164. } else if(ii-jj==-1 && ii>((size-1)/2) && jj>((size-1)/2)){
  165. b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //diagonal right and down
  166. } else if(jj==(size-1) && ii==(size-1)){
  167. b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //bottom right corner
  168. } else if(jj>((size-1)/2) && jj<(size-1) && ii==(size-1)){
  169. b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //right edge downwards
  170.  
  171.  
  172. } else if(jj==0 && ii==0){
  173. b.setBorder(BorderFactory.createMatteBorder(1,1,0,1, Color.black)); //top corner
  174. } else if(ii==0 && jj>0 && jj<((size-1)/2)){
  175. b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //left top side edge
  176. } else if(jj==0 && ii>0 && ii<(((size-1)/2)+1)){
  177. b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //left top top edge
  178. } else if(ii==jj && ii>0 && ii<((size-1)/2) && jj>0 && jj<((size-1)/2)){
  179. b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //directly below center
  180.  
  181.  
  182. } else {
  183. b.setBorder(BorderFactory.createMatteBorder(0,0,0,0, Color.black)); //inner parts blank
  184. }
  185.  
  186.  
  187.  
  188. /*
  189. * Now I need to set the colours
  190. * nn+2d(n-1)+f-1
  191. * nn is purple (in this case Java.Color.MAGENTA)
  192. * 2d(n-1) is red
  193. * f is blue (in this case Java.Color.CYAN)
  194. */
  195.  
  196. b.setBackground(Color.WHITE);
  197.  
  198. //nn+2d(n-1)+f-1
  199. //If n=1 there's no nn because of the -1 (the black square in the middle
  200. //If n=1 there's no 2d(n-1) since n-1=0
  201. //So if n=1 you have to fill the whole square with f
  202. if(nextCell.xplusn() != 1){
  203. //if x+n=1, it's just the one black square, so it's already done
  204. if(nextCell.n() == 1){
  205. //everything other than the center is f-coloured
  206. if(!(ii==((size-1)/2) && jj==((size-1)/2))){
  207. b.setBackground(Color.CYAN);
  208. }
  209. } else {
  210. int offset = (((nextCell.n())-1)/2)+1;
  211. int nextrowlength = ((nextCell.n())+1)/2;
  212. int n = 0;
  213. if(nextCell.n()%2==0){
  214. n = nextCell.n() - 1;
  215. fStore = (nextCell.f()*-1) + (2*n) + 1;
  216. } else {
  217. n = nextCell.n();
  218. fStore = (nextCell.f()*-1);
  219. }
  220. int toput = fStore;
  221.  
  222. //it needs to be set up so that the n square is around the outside of the center black square
  223. //it'll be (n-1)/2 away from the center
  224. if(ii>=(((size-1)/2)-((n-1)/2))
  225. && ii<=(((size-1)/2)+((n-1)/2))
  226. && jj>=(((size-1)/2)-(n-1)/2)
  227. && jj<=(((size-1)/2)+(n-1)/2)){
  228. //everything here is nn-coloured
  229. b.setBackground(Color.MAGENTA);
  230. } else {
  231. b.setBackground(colouringF(ii,jj,toput,offset,nextrowlength,n));
  232. }
  233. }
  234. }
  235.  
  236. //if you're in the middle, make it black
  237. if(ii==((size-1)/2) && jj==((size-1)/2)){
  238. b.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black)); //diagonal left and down
  239. b.setBackground(Color.BLACK);
  240. }
  241.  
  242. if(b.getBackground() == Color.WHITE){
  243. b.setBackground(Color.RED);
  244. }
  245.  
  246. }
  247.  
  248. xplusnSquare.add(unitSquares[jj][ii], c);
  249. }
  250. }
  251. createdplusnSquare();
  252. setGui();
  253. }
  254.  
  255. public void createdplusnSquare(){
  256. int sizeD = nextCell.dplusn();
  257. for(int ii = 0; ii<sizeD; ii++){
  258. for(int jj = 0; jj<sizeD; jj++){
  259. JButton b = new JButton();
  260. b.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.WHITE));
  261. b.setPreferredSize(new Dimension(pixels, pixels));
  262. b.setBackground(colouringdplusnSquare(ii, jj, sizeD));
  263. ImageIcon icon = new ImageIcon(new BufferedImage(pixels, pixels, BufferedImage.TYPE_INT_ARGB));
  264. b.setIcon(icon);
  265. GridBagConstraints c = new GridBagConstraints();
  266. c.gridx = ii;
  267. c.gridy = jj;
  268. dplusnSquare.add(b, c);
  269. }
  270. }
  271. }
  272.  
  273. public Color colouringdplusnSquare(int ii, int jj, int sizeD){
  274. //if ii and jj are within sizeD and sizeD-nextCell.d(), return black
  275. if(ii>=(nextCell.n()) && jj>=(nextCell.n())){
  276. return Color.BLACK; //dd
  277. } else if((ii==nextCell.n()-1) && (jj==nextCell.n()-1)){
  278. if(nextCell.e()%2==0){
  279. return Color.CYAN; //if e's even the d+1 corner will be blue for f
  280. } else {
  281. return Color.GREEN; //otherwise it's green for e
  282. }
  283. } else if((ii>=nextCell.n() && jj==nextCell.n()-1) || (jj>=nextCell.n() && ii==nextCell.n()-1)){
  284. int eD = nextCell.e();
  285. int fD = (nextCell.f())*-1;
  286. int fdiv2 = fD/2;
  287. int ediv2 = eD/2;
  288. //you want e from the bottom up and the right leftwards
  289. //so it's going to be ii and jj between sizeD (exc) and sizeD-ediv2, right? I think
  290. if((ii<sizeD && ii>=sizeD-ediv2 && jj==nextCell.n()-1) || (jj<sizeD && jj>=sizeD-ediv2 && ii==nextCell.n()-1)){
  291. return Color.GREEN;
  292. } else {
  293. return Color.CYAN;
  294. }
  295. } else if(((ii<=(nextCell.n()-1)) && (jj<=(nextCell.n()-1))) && !(ii==nextCell.n()-1 && jj==nextCell.n()-1) ){
  296. return Color.MAGENTA; //nn-1
  297. }
  298. return Color.RED;
  299. }
  300.  
  301. public Color colouringF(int ii, int jj, int toput, int offset, int nextrowlength, int n){
  302. while(toput>0){
  303. //then somehow I need to figure out how to put f in there
  304. //everything else will be 2d(n-1) so I guess that'll just be another else excluding center
  305. //so first I need to find f%8
  306.  
  307. //fdiv8 is how many to put in each triangle
  308. //the length of the part of the triangle outside of nn will be (n+1)/2
  309. //e.g. if n=19, the closest row of the triangles will be (19+1)/2=10 long
  310. //if f/8 is less than that, you can just go from wherever you drew a line between triangles
  311. //and keep going in the opposite direction
  312. //if f/8 is greater than that, you'll need to fill the whole row for eah triangle and then
  313. //do the same thing you did for f/8<((n+1)/2) but with the next row out
  314. //man this sounds really hard, fucking hell why am I doing this
  315. if(toput <= (nextrowlength*8)){
  316. //let's say you're putting 24 in
  317. //in the first triangle, it'd be the first, the ninth and the seventeenth
  318. //so you'd go "if you're in that set of squares and the distance from here to origin
  319. //times 8 +1 (+1 for first triangle) is less than or equal to toput
  320. if(
  321. ((ii<=((size-1)/2)) && (ii>(((size-1)/2)-offset)) && (jj==(((size-1)/2)-offset)))
  322. &&
  323. (((((((size-1)/2)-ii)*8)+1)<=toput))
  324. ){
  325. return Color.CYAN;
  326. } else if(
  327. ((jj<=(((size-1)/2))-1) && (jj>(((size-1)/2)-(offset+1))) && ii==(((size-1)/2)-offset))
  328. && (
  329. (((((((size-1)/2)-1)-jj)*8)+2)<=toput))
  330. ){
  331. return Color.CYAN;
  332. } else if(
  333. ((jj>=(((size-1)/2))) && (jj<(((size-1)/2)+(offset))) && ii==(((size-1)/2)-offset))
  334. && (
  335. ((((jj-((size-1)/2))*8)+3)<=toput))
  336. ){
  337. return Color.CYAN;
  338. } else if(
  339. ((ii<=(((size-1)/2)-1)) && (ii>(((size-1)/2)-(offset+1))) && jj==(((size-1)/2)+offset))
  340. && (
  341. (((((((size-1)/2)-1)-ii)*8)+4)<=toput))
  342. ){
  343. return Color.CYAN;
  344. } else if(
  345. ((ii>=(((size-1)/2))) && (ii<(((size-1)/2)+(offset))) && jj==(((size-1)/2)+offset))
  346. && (
  347. (((((ii-((size-1)/2)))*8)+5)<=toput))
  348. ){
  349. return Color.CYAN;
  350. } else if(
  351. ((jj>=(((size-1)/2)+1)) && (jj<(((size-1)/2)+1+(offset))) && ii==(((size-1)/2)+offset))
  352. && (
  353. ((((jj-(((size-1)/2)+1))*8)+6)<=toput))
  354. ){
  355. return Color.CYAN;
  356. } else if(
  357. ((jj<=(((size-1)/2))) && (jj>(((size-1)/2)-(offset))) && ii==(((size-1)/2)+offset))
  358. && (
  359. (((((((size-1)/2)-jj))*8)+7)<=toput))
  360. ){
  361. return Color.CYAN;
  362. } else if(
  363. ((ii>(((size-1)/2))) && (ii<(((size-1)/2)+1+(offset))) && jj==(((size-1)/2)-offset))
  364. && (
  365. ((((ii-(((size-1)/2)+1))*8)+8)<=toput))
  366. ){
  367. return Color.CYAN;
  368. } else {
  369. return Color.RED;
  370. }
  371. } else {
  372. //fill the rows with f-colour
  373. if(((ii==(((size-1)/2)-(nextrowlength))) && (jj>=((((size-1)/2))-(nextrowlength))) && (jj<=((((size-1)/2))+(nextrowlength))))
  374. ||((ii==(((size-1)/2)+(nextrowlength))) && (jj>=((((size-1)/2))-(nextrowlength))) && (jj<=((((size-1)/2))+(nextrowlength))))
  375. ||((jj==(((size-1)/2)-(nextrowlength))) && (ii>=((((size-1)/2))-(nextrowlength))) && (ii<=((((size-1)/2))+(nextrowlength))))
  376. ||((jj==(((size-1)/2)+(nextrowlength))) && (ii>=((((size-1)/2))-(nextrowlength))) && (ii<=((((size-1)/2))+(nextrowlength))))
  377. ){
  378. return Color.CYAN;
  379. }
  380. offset++;
  381. nextrowlength++;
  382. toput = toput - ((nextrowlength-1)*8);
  383. }
  384. }
  385. return Color.RED;
  386. }
  387.  
  388. public void actionPerformed(ActionEvent e) {
  389. if ("keepxplusnup".equals(e.getActionCommand())) {
  390. nextCell = nextCell.keepxplusnup();
  391. initializeVars(nextCell.xplusn());
  392. initializeGui();
  393. startThing();
  394. }
  395. if ("keepxplusndown".equals(e.getActionCommand())) {
  396. nextCell = nextCell.keepxplusndown();
  397. initializeVars(nextCell.xplusn());
  398. initializeGui();
  399. startThing();
  400. }
  401. }
  402.  
  403. public static boolean isPrime(int n){
  404. if(n==1 || n==2 || n==4){
  405. return false;
  406. }
  407. for(int i=2; i<n; i++){
  408. if(n%i==0){
  409. return false;
  410. }
  411. }
  412. return true;
  413. }
  414.  
  415. public JComponent getGui(){
  416. return gui;
  417. }
  418.  
  419. public static void main(String[] args) {
  420. Runnable r = new Runnable() {
  421.  
  422. @Override
  423. public void run() {
  424. startThing();
  425. }
  426. };
  427. SwingUtilities.invokeLater(r);
  428. }
  429.  
  430. public static void startThing(){
  431. if(cb != null){ //if you're changing the triangles
  432. System.out.println("Is it stuck in an infinite loop or is it just slow?");
  433. cb = new GridTest(nextCell.xplusn());
  434. f.getContentPane().removeAll();
  435. f.add(cb.getGui());
  436. f.setMinimumSize(f.getSize());
  437. f.pack();
  438. f.setSize(1300,725);
  439. } else { //initial triangles
  440. gui = null;
  441. Scanner scan = new Scanner(System.in);
  442. int a = scan.nextInt();
  443. int b = scan.nextInt();
  444. nextCell = new GridCell(a, b);
  445. cb = new GridTest(nextCell.xplusn());
  446. f.add(cb.getGui());
  447. f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  448. f.setLocationByPlatform(true);
  449. f.pack();
  450. f.setSize(1300,725);
  451. f.setMinimumSize(f.getSize());
  452. f.setVisible(true);
  453. }
  454. }
  455. }
Advertisement
Add Comment
Please, Sign In to add comment