Guest User

Triangles with colours in Java (only odd n even x)

a guest
Oct 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.33 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 = 100;
  15. private static ArrayList<GridCell> gridCells;
  16. private static int currentIndex;
  17. private static JPanel gui;
  18. private static GridTest cb;
  19. private int size;
  20. private JButton[][] unitSquares;
  21. private JPanel xplusnSquare;
  22. private JLabel message;
  23. private static JFrame f = new JFrame("(x+n)(x+n)");;
  24. private boolean thingsSet = false;
  25. private JToolBar tools;
  26.  
  27. public GridTest(int sizeSend) {
  28. int size = sizeSend;
  29. initializeVars(size);
  30. initializeGui();
  31. }
  32.  
  33. public void initializeVars(int size){
  34. this.gui = new JPanel(new BorderLayout(0,0));
  35. this.size = size;
  36. this.unitSquares = new JButton[size][size];
  37. this.message = new JLabel(gridCells.get(currentIndex).print());
  38. }
  39.  
  40. public static void initializeCells(){
  41. currentIndex = 0;
  42. primes = new ArrayList<Integer>();
  43. for(int i=3; i<maxPrime; i++){
  44. if(isPrime(i)){
  45. primes.add(i);
  46. }
  47. }
  48. gridCells = new ArrayList<GridCell>();
  49. for(int j=0; j<primes.size(); j++){
  50. for(int k=0; k<j; k++){
  51. if(primes.get(k) != primes.get(j)){
  52. GridCell testCell = new GridCell(primes.get(k), primes.get(j));
  53. if(testCell.oddxplusnsquared() && testCell.n()%2==1){
  54. //We're only dealing with odd n even x
  55. gridCells.add(testCell);
  56. }
  57. }
  58. }
  59. }
  60. }
  61.  
  62. public void setGui(){
  63. if(thingsSet != true){
  64. gui.setBorder(new EmptyBorder(5, 5, 5, 5));
  65. gui.add(tools, BorderLayout.PAGE_START);
  66. }
  67. gui.add(xplusnSquare);
  68. }
  69.  
  70. public final void initializeGui() {
  71. if(tools == null){
  72. tools = new JToolBar();
  73. tools.setFloatable(false);
  74. JButton changeButton = new JButton("Change");
  75. changeButton.setActionCommand("change");
  76. changeButton.addActionListener(this);
  77. tools.add(changeButton);
  78. tools.addSeparator();
  79. tools.add(message);
  80. }
  81.  
  82. xplusnSquare = new JPanel(new GridBagLayout());
  83. xplusnSquare.setBorder(new LineBorder(Color.WHITE));
  84.  
  85. Insets buttonMargin = new Insets(0,0,0,0);
  86. for (int ii = 0; ii < unitSquares.length; ii++) {
  87. for (int jj = 0; jj < unitSquares[ii].length; jj++) {
  88. JButton b = new JButton();
  89. int pixels = 650/size;
  90. b.setPreferredSize(new Dimension(pixels, pixels));
  91.  
  92. ImageIcon icon = new ImageIcon(new BufferedImage(pixels, pixels, BufferedImage.TYPE_INT_ARGB));
  93. b.setIcon(icon);
  94. unitSquares[jj][ii] = b;
  95. GridBagConstraints c = new GridBagConstraints();
  96. c.gridx = ii;
  97. c.gridy = jj;
  98.  
  99.  
  100. /*
  101. * This is where I'm drawing the triangles. It's a whole bunch of code so it could probably be moved to
  102. * its own method but whatever.
  103. */
  104.  
  105.  
  106. if(gridCells.get(currentIndex).xplusn() == 3){
  107. //if it's three then the center will be black and the eight remaining squares will be the triangles
  108. //these don't need sophisticated border drawing to make triangles out of them
  109. b.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black)); //directly left of center
  110. b.setBackground(Color.CYAN);
  111. //if you're in the middle, make it black
  112. if(ii==((size-1)/2) && jj==((size-1)/2)){
  113. b.setBackground(Color.BLACK);
  114. }
  115.  
  116. } else {
  117.  
  118. //drawing the triangles
  119. if(jj==((size-1)/2) && ii==(((size-1)/2)-1)){
  120. b.setBorder(BorderFactory.createMatteBorder(1,0,1,0, Color.black)); //directly left of center
  121. } else if(jj==((size-1)/2) && ii<(((size-1)/2)-1) && ii>0){
  122. b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //left of center to before left edge
  123. } else if(((((size-1)/2)-jj)+(((size-1)/2)-ii)==1) && jj>ii && ii>0){
  124. b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //diagonal left and down
  125. } else if(ii==(((size-1)/2)-1) && jj>((size-1)/2) && jj<size-1){
  126. b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //downwards directly left of center
  127. } else if(ii==0 && jj==((size-1)/2)){
  128. b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //left edge center corner
  129. } else if(ii==0 && jj>((size-1)/2) && jj<(size-2)){
  130. b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //left edge downwards from center
  131. } else if(ii==0 && jj==(size-2)){
  132. b.setBorder(BorderFactory.createMatteBorder(0,1,1,1, Color.black)); //left bottom corner of triangle (2nd last square)
  133. } else if(ii==0 && jj==(size-1)){
  134. b.setBorder(BorderFactory.createMatteBorder(0,1,1,0, Color.black)); //left bottom corner
  135. } else if(ii>0 && ii<(((size-1)/2)-1) && jj==(size-1)){
  136. b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //bottom edge before center
  137. } else if(ii==(((size-1)/2)-1) && jj==(size-1)){
  138. b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //bottom left inner corner before center
  139.  
  140.  
  141. } else if(jj==((size-1)/2) && ii==(((size-1)/2)+1)){
  142. b.setBorder(BorderFactory.createMatteBorder(1,0,1,0, Color.black)); //directly right of center
  143. } else if(jj==((size-1)/2) && ii>(((size-1)/2)-1) && ii<(size-1)){
  144. b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //right of center to before right edge
  145. } else if(((jj-((size-1)/2))+(ii-((size-1)/2))==1) && jj<ii && jj>0){
  146. if(ii==(size-1)){
  147. b.setBorder(BorderFactory.createMatteBorder(1,1,0,1, Color.black)); //diagonal right and up
  148. } else {
  149. b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //diagonal right and up
  150. }
  151. } else if(ii==(((size-1)/2)+1) && jj<((size-1)/2) && jj>0){
  152. b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //upwards directly right of center
  153. } else if(ii==(size-1) && jj==((size-1)/2)){
  154. b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //right edge center corner
  155. } else if(ii==(size-1) && jj<0 && jj>(size-2)){
  156. b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //right edge upwards from center
  157. } else if(jj==0 && ii>(((size-1)/2)+1) && ii<(size-1)){
  158. b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //top right
  159. } else if(ii==(size-1) && jj==0){
  160. b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //diagonal right and up
  161. } else if(ii==(((size-1)/2)+1) && jj==0){
  162. b.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.black)); //diagonal right and up
  163. } else if(ii==(size-1) && jj>1 && jj<((size-1)/2)){
  164. b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //diagonal right and up
  165.  
  166.  
  167. } else if(jj==(((size-1)/2)+1) && ii==((size-1)/2)){
  168. b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //directly below center
  169. } else if(jj==(size-1) && ii>=((size-1)/2) && ii<(size-2)){
  170. b.setBorder(BorderFactory.createMatteBorder(0,0,1,0, Color.black)); //bottom below center
  171. } else if(jj==(size-1) && ii==(size-2)){
  172. b.setBorder(BorderFactory.createMatteBorder(1,0,1,1, Color.black)); //directly below center
  173. } else if(ii-jj==-1 && ii>((size-1)/2) && jj>((size-1)/2)){
  174. b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //diagonal right and down
  175. } else if(jj==(size-1) && ii==(size-1)){
  176. b.setBorder(BorderFactory.createMatteBorder(0,0,1,1, Color.black)); //bottom right corner
  177. } else if(jj>((size-1)/2) && jj<(size-1) && ii==(size-1)){
  178. b.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.black)); //right edge downwards
  179.  
  180.  
  181. } else if(jj==0 && ii==0){
  182. b.setBorder(BorderFactory.createMatteBorder(1,1,0,1, Color.black)); //top corner
  183. } else if(ii==0 && jj>0 && jj<((size-1)/2)){
  184. b.setBorder(BorderFactory.createMatteBorder(0,1,0,0, Color.black)); //left top side edge
  185. } else if(jj==0 && ii>0 && ii<(((size-1)/2)+1)){
  186. b.setBorder(BorderFactory.createMatteBorder(1,0,0,0, Color.black)); //left top top edge
  187. } else if(ii==jj && ii>0 && ii<((size-1)/2) && jj>0 && jj<((size-1)/2)){
  188. b.setBorder(BorderFactory.createMatteBorder(1,0,0,1, Color.black)); //directly below center
  189.  
  190.  
  191. } else {
  192. b.setBorder(BorderFactory.createMatteBorder(0,0,0,0, Color.black)); //inner parts blank
  193. }
  194.  
  195.  
  196.  
  197. /*
  198. * Now I need to set the colours
  199. * nn+2d(n-1)+f-1
  200. * nn is purple (in this case Java.Color.MAGENTA)
  201. * 2d(n-1) is red
  202. * f is blue (in this case Java.Color.CYAN)
  203. */
  204.  
  205. b.setBackground(Color.WHITE);
  206.  
  207. //nn+2d(n-1)+f-1
  208. //If n=1 there's no nn because of the -1 (the black square in the middle
  209. //If n=1 there's no 2d(n-1) since n-1=0
  210. //So if n=1 you have to fill the whole square with f
  211. if(gridCells.get(currentIndex).xplusn() != 1){
  212. //if x+n=1, it's just the one black square, so it's already done
  213. if(gridCells.get(currentIndex).n() == 1){
  214. //everything other than the center is f-coloured
  215. if(!(ii==((size-1)/2) && jj==((size-1)/2))){
  216. b.setBackground(Color.CYAN);
  217. }
  218. } else {
  219. int offset = (((gridCells.get(currentIndex).n())-1)/2)+1;
  220.  
  221. //if n!=1 n's still going to be odd because that's how I set it up
  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)-((gridCells.get(currentIndex).n()-1)/2))
  225. && ii<=(((size-1)/2)+((gridCells.get(currentIndex).n()-1)/2))
  226. && jj>=(((size-1)/2)-(gridCells.get(currentIndex).n()-1)/2)
  227. && jj<=(((size-1)/2)+(gridCells.get(currentIndex).n()-1)/2)){
  228. //everything here is nn-coloured
  229. b.setBackground(Color.MAGENTA);
  230. } else {
  231. //then somehow I need to figure out how to put f in there
  232. //everything else will be 2d(n-1) so I guess that'll just be another else excluding center
  233. //so first I need to find f%8
  234. int fmod8 = (gridCells.get(currentIndex).f()*-1)%8;
  235. int fminmod = (gridCells.get(currentIndex).f()*-1)-fmod8;
  236. int fdiv8 = fminmod/8;
  237. int nextrowlength = ((gridCells.get(currentIndex).n())+1)/2;
  238. int n = gridCells.get(currentIndex).n(); //should've done this already
  239. //fdiv8 is how many to put in each triangle
  240. //the length of the part of the triangle outside of nn will be (n+1)/2
  241. //e.g. if n=19, the closest row of the triangles will be (19+1)/2=10 long
  242. //if f/8 is less than that, you can just go from wherever you drew a line between triangles
  243. //and keep going in the opposite direction
  244. //if f/8 is greater than that, you'll need to fill the whole row for eah triangle and then
  245. //do the same thing you did for f/8<((n+1)/2) but with the next row out
  246. //man this sounds really hard, fucking hell why am I doing this
  247. int toput = (gridCells.get(currentIndex).f()*-1);
  248. if(fdiv8 <= nextrowlength){
  249. //let's say you're putting 24 in
  250. //in the first triangle, it'd be the first, the ninth and the seventeenth
  251. //so you'd go "if you're in that set of squares and the distance from here to origin
  252. //times 8 +1 (+1 for first triangle) is less than or equal to toput
  253. if(
  254. (
  255. (ii<=((size-1)/2))
  256. &&
  257. (ii>(((size-1)/2)-offset))
  258. &&
  259. (jj==(((size-1)/2)-offset))
  260. )
  261. &&
  262. (
  263. (
  264. (
  265. (
  266. (
  267. (
  268. (size-1)
  269. /2)
  270. -ii)
  271. *8)
  272. +1)
  273. <=toput)
  274. )){
  275. b.setBackground(Color.CYAN);
  276. } else if(
  277. ( (jj<=(((size-1)/2))-1) && (jj>(((size-1)/2)-(offset+1))) && ii==(((size-1)/2)-offset))
  278. && (
  279. ( ((((((size-1)/2)-1)-jj)*8)+2)
  280. <=toput))
  281. ){
  282. b.setBackground(Color.CYAN);
  283. } else if(
  284. ( (jj>=(((size-1)/2))) && (jj<(((size-1)/2)+(offset))) && ii==(((size-1)/2)-offset))
  285. && (
  286. ( (((jj-((size-1)/2))*8)+3)
  287. <=toput))
  288. ){
  289. b.setBackground(Color.CYAN);
  290. } else if(
  291. ( (ii<=(((size-1)/2)-1)) && (ii>(((size-1)/2)-(offset+1))) && jj==(((size-1)/2)+offset))
  292. && (
  293. ( ((((((size-1)/2)-1)-ii)*8)+4)
  294. <=toput))
  295. ){
  296. b.setBackground(Color.CYAN);
  297. } else if(
  298. ( (ii>=(((size-1)/2))) && (ii<(((size-1)/2)+(offset))) && jj==(((size-1)/2)+offset))
  299. && (
  300. ( ((((ii-((size-1)/2)))*8)+5)
  301. <=toput))
  302. ){
  303. b.setBackground(Color.CYAN);
  304. } else if(
  305. ( (jj>=(((size-1)/2)+1)) && (jj<(((size-1)/2)+1+(offset))) && ii==(((size-1)/2)+offset))
  306. && (
  307. ( (((jj-(((size-1)/2)+1))*8)+6)
  308. <=toput))
  309. ){
  310. b.setBackground(Color.CYAN);
  311. } else if(
  312. ( (jj<=(((size-1)/2))) && (jj>(((size-1)/2)-(offset))) && ii==(((size-1)/2)+offset))
  313. && (
  314. ( ((((((size-1)/2)-jj))*8)+7)
  315. <=toput))
  316. ){
  317. b.setBackground(Color.CYAN);
  318. } else if(
  319. ( (ii>(((size-1)/2))) && (ii<(((size-1)/2)+1+(offset))) && jj==(((size-1)/2)-offset))
  320. && (
  321. ( (((ii-(((size-1)/2)+1))*8)+8)
  322. <=toput))
  323. ){
  324. b.setBackground(Color.CYAN);
  325. }
  326. } else {
  327. offset++;
  328. //in a perfect world, I would make this some kind of recursive function
  329. //I probably could but why the fuck would I spend that much time on something like this
  330. //if you're reading this and this looks like poor design to you, (a) I agree and (b) fuck you, either
  331. //fix it for me or live with it
  332. //this GUI can only really handle primes up to 97 before taking too long/being too big, and I've
  333. //gone through each example and found that the furthest f/8 goes in terms of rows away from nn
  334. //is a third row, so we only need a couple if statements to make the thing work
  335. //don't like it? fix it yourself. I'm done putting every waking moment into VQC without any help.
  336.  
  337. //fill the rows with f-colour
  338. if(((ii==(((size-1)/2)-((n+1)/2))) && (jj>=((((size-1)/2))-((n+1)/2))) && (jj<=((((size-1)/2))+((n+1)/2))))
  339. ||((ii==(((size-1)/2)+((n+1)/2))) && (jj>=((((size-1)/2))-((n+1)/2))) && (jj<=((((size-1)/2))+((n+1)/2))))
  340. ||((jj==(((size-1)/2)-((n+1)/2))) && (ii>=((((size-1)/2))-((n+1)/2))) && (ii<=((((size-1)/2))+((n+1)/2))))
  341. ||((jj==(((size-1)/2)+((n+1)/2))) && (ii>=((((size-1)/2))-((n+1)/2))) && (ii<=((((size-1)/2))+((n+1)/2))))
  342. ){
  343. b.setBackground(Color.CYAN);
  344. }
  345. //then take nextrowlength away from fdiv8
  346. fdiv8-=nextrowlength;
  347. //then add one to nextrowlength
  348. nextrowlength++;
  349. //then check again if fdiv8>nextrowlength
  350. if(fdiv8 <= nextrowlength){
  351. toput = toput - ((nextrowlength-1)*8);
  352. if(
  353. ( (ii<=((size-1)/2)) && (ii>(((size-1)/2)-offset)) && jj==(((size-1)/2)-offset))
  354. && (
  355. ( (((((size-1)/2)-ii)*8)+1)
  356. <=toput)
  357. )){
  358. b.setBackground(Color.CYAN);
  359. } else if(
  360. ( (jj<=(((size-1)/2))-1) && (jj>(((size-1)/2)-(offset+1))) && ii==(((size-1)/2)-offset))
  361. && (
  362. ( ((((((size-1)/2)-1)-jj)*8)+2)
  363. <=toput))
  364. ){
  365. b.setBackground(Color.CYAN);
  366. } else if(
  367. ( (jj>=(((size-1)/2))) && (jj<(((size-1)/2)+(offset))) && ii==(((size-1)/2)-offset))
  368. && (
  369. ( (((jj-((size-1)/2))*8)+3)
  370. <=toput))
  371. ){
  372. b.setBackground(Color.CYAN);
  373. } else if(
  374. ( (ii<=(((size-1)/2)-1)) && (ii>(((size-1)/2)-(offset+1))) && jj==(((size-1)/2)+offset))
  375. && (
  376. ( ((((((size-1)/2)-1)-ii)*8)+4)
  377. <=toput))
  378. ){
  379. b.setBackground(Color.CYAN);
  380. } else if(
  381. ( (ii>=(((size-1)/2))) && (ii<(((size-1)/2)+(offset))) && jj==(((size-1)/2)+offset))
  382. && (
  383. ( ((((ii-((size-1)/2)))*8)+5)
  384. <=toput))
  385. ){
  386. b.setBackground(Color.CYAN);
  387. } else if(
  388. ( (jj>=(((size-1)/2)+1)) && (jj<(((size-1)/2)+1+(offset))) && ii==(((size-1)/2)+offset))
  389. && (
  390. ( (((jj-(((size-1)/2)+1))*8)+6)
  391. <=toput))
  392. ){
  393. b.setBackground(Color.CYAN);
  394. } else if(
  395. ( (jj<=(((size-1)/2))) && (jj>(((size-1)/2)-(offset))) && ii==(((size-1)/2)+offset))
  396. && (
  397. ( ((((((size-1)/2)-jj))*8)+7)
  398. <=toput))
  399. ){
  400. b.setBackground(Color.CYAN);
  401. } else if(
  402. ( (ii>(((size-1)/2))) && (ii<(((size-1)/2)+1+(offset))) && jj==(((size-1)/2)-offset))
  403. && (
  404. ( (((ii-(((size-1)/2)+1))*8)+8)
  405. <=toput))
  406. ){
  407. b.setBackground(Color.CYAN);
  408. }
  409.  
  410. } else {
  411. offset++;
  412. //fill the rows with f-colour
  413. if(((ii==(((size-1)/2)-(((n+1)/2)+1))) && (jj>=((((size-1)/2))-(((n+1)/2)+1))) && (jj<=((((size-1)/2))+(((n+1)/2)+1))))
  414. ||((ii==(((size-1)/2)+(((n+1)/2)+1))) && (jj>=((((size-1)/2))-(((n+1)/2)+1))) && (jj<=((((size-1)/2))+(((n+1)/2)+1))))
  415. ||((jj==(((size-1)/2)-(((n+1)/2)+1))) && (ii>=((((size-1)/2))-(((n+1)/2)+1))) && (ii<=((((size-1)/2))+(((n+1)/2)+1))))
  416. ||((jj==(((size-1)/2)+(((n+1)/2)+1))) && (ii>=((((size-1)/2))-(((n+1)/2)+1))) && (ii<=((((size-1)/2))+(((n+1)/2)+1))))
  417. ){
  418. b.setBackground(Color.CYAN);
  419. }
  420. //then take nextrowlength away from fdiv8
  421. fdiv8-=nextrowlength;
  422. //then add one to nextrowlength
  423. nextrowlength++;
  424. //then check again if fdiv8>nextrowlength
  425. if(fdiv8 <= nextrowlength){
  426. toput = toput - ((nextrowlength-1)*8);
  427. if(
  428. ( (ii<=((size-1)/2)) && (ii>(((size-1)/2)-offset)) && jj==(((size-1)/2)-offset))
  429. && (
  430. ( (((((size-1)/2)-ii)*8)+1)
  431. <=toput))
  432. ){
  433. b.setBackground(Color.CYAN);
  434. } else if(
  435. ( (jj<=(((size-1)/2))-1) && (jj>(((size-1)/2)-(offset+1))) && ii==(((size-1)/2)-offset))
  436. && (
  437. ( ((((((size-1)/2)-1)-jj)*8)+2)
  438. <=toput))
  439. ){
  440. b.setBackground(Color.CYAN);
  441. } else if(
  442. ( (jj>=(((size-1)/2))) && (jj<(((size-1)/2)+(offset))) && ii==(((size-1)/2)-offset))
  443. && (
  444. ( (((jj-((size-1)/2))*8)+3)
  445. <=toput))
  446. ){
  447. b.setBackground(Color.CYAN);
  448. } else if(
  449. ( (ii<=(((size-1)/2)-1)) && (ii>(((size-1)/2)-(offset+1))) && jj==(((size-1)/2)+offset))
  450. && (
  451. ( ((((((size-1)/2)-1)-ii)*8)+4)
  452. <=toput))
  453. ){
  454. b.setBackground(Color.CYAN);
  455. } else if(
  456. ( (ii>=(((size-1)/2))) && (ii<(((size-1)/2)+(offset))) && jj==(((size-1)/2)+offset))
  457. && (
  458. ( ((((ii-((size-1)/2)))*8)+5)
  459. <=toput))
  460. ){
  461. b.setBackground(Color.CYAN);
  462. } else if(
  463. ( (jj>=(((size-1)/2)+1)) && (jj<(((size-1)/2)+1+(offset))) && ii==(((size-1)/2)+offset))
  464. && (
  465. ( (((jj-(((size-1)/2)+1))*8)+6)
  466. <=toput))
  467. ){
  468. b.setBackground(Color.CYAN);
  469. } else if(
  470. ( (jj<=(((size-1)/2))) && (jj>(((size-1)/2)-(offset))) && ii==(((size-1)/2)+offset))
  471. && (
  472. ( ((((((size-1)/2)-jj))*8)+7)
  473. <=toput))
  474. ){
  475. b.setBackground(Color.CYAN);
  476. } else if(
  477. ( (ii>(((size-1)/2))) && (ii<(((size-1)/2)+1+(offset))) && jj==(((size-1)/2)-offset))
  478. && (
  479. ( (((ii-(((size-1)/2)+1))*8)+8)
  480. <=toput))
  481. ){
  482. b.setBackground(Color.CYAN);
  483. }
  484. } else {
  485. //fill the rows with f-colour
  486. if(((ii==(((size-1)/2)-(((n+1)/2)+2))) && (jj>=((((size-1)/2))-(((n+1)/2)+2))) && (jj<=((((size-1)/2))+(((n+1)/2)+2))))
  487. ||((ii==(((size-1)/2)+(((n+1)/2)+2))) && (jj>=((((size-1)/2))-(((n+1)/2)+2))) && (jj<=((((size-1)/2))+(((n+1)/2)+2))))
  488. ||((jj==(((size-1)/2)-(((n+1)/2)+2))) && (ii>=((((size-1)/2))-(((n+1)/2)+2))) && (ii<=((((size-1)/2))+(((n+1)/2)+2))))
  489. ||((jj==(((size-1)/2)+(((n+1)/2)+2))) && (ii>=((((size-1)/2))-(((n+1)/2)+2))) && (ii<=((((size-1)/2))+(((n+1)/2)+2))))
  490. ){
  491. b.setBackground(Color.CYAN);
  492. }
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499.  
  500. //if you're in the middle, make it black
  501. if(ii==((size-1)/2) && jj==((size-1)/2)){
  502. b.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black)); //diagonal left and down
  503. b.setBackground(Color.BLACK);
  504. }
  505.  
  506. if(b.getBackground() == Color.WHITE){
  507. b.setBackground(Color.RED);
  508. }
  509.  
  510. }
  511.  
  512. xplusnSquare.add(unitSquares[jj][ii], c);
  513. }
  514. }
  515. setGui();
  516. }
  517.  
  518. public void actionPerformed(ActionEvent e) {
  519. if ("change".equals(e.getActionCommand())) {
  520. initializeVars(13);
  521. initializeGui();
  522. startThing();
  523. }
  524. }
  525.  
  526. public static boolean isPrime(int n){
  527. if(n==1 || n==2 || n==4){
  528. return false;
  529. }
  530. for(int i=2; i<n; i++){
  531. if(n%i==0){
  532. return false;
  533. }
  534. }
  535. return true;
  536. }
  537.  
  538. public JComponent getGui(){
  539. return gui;
  540. }
  541.  
  542. public static void main(String[] args) {
  543. Runnable r = new Runnable() {
  544.  
  545. @Override
  546. public void run() {
  547. startThing();
  548. }
  549. };
  550. SwingUtilities.invokeLater(r);
  551. }
  552.  
  553. public static void startThing(){
  554. if(cb != null){ //if you're changing the triangles
  555. currentIndex++;
  556. if(currentIndex == gridCells.size()){
  557. currentIndex = 0;
  558. }
  559. cb = new GridTest(gridCells.get(currentIndex).xplusn());
  560. f.getContentPane().removeAll();
  561. f.add(cb.getGui());
  562. f.setMinimumSize(f.getSize());
  563. f.pack();
  564. f.setSize(725,725);
  565. } else { //initial triangles
  566. initializeCells();
  567. gui = null;
  568. cb = new GridTest(gridCells.get(currentIndex).xplusn());
  569. f.add(cb.getGui());
  570. f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  571. f.setLocationByPlatform(true);
  572. f.pack();
  573. f.setSize(725,725);
  574. f.setMinimumSize(f.getSize());
  575. f.setVisible(true);
  576. }
  577. }
  578. }
Advertisement
Add Comment
Please, Sign In to add comment