Guest User

Triangles in Java

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