Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
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); } } }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
auctions relist for n minutes
PHP | 10 hours ago | 0.79 KB
class-avia-instagram
PHP | 12 hours ago | 46.97 KB
42:SOURCE
14 hours ago | 0.01 KB
42
14 hours ago | 0.34 KB
The Desperate Mission 2
15 hours ago | 0.74 KB
The Desperate Mission 1
15 hours ago | 0.65 KB
Rebel Dawn 14
15 hours ago | 2.58 KB
Rebel Dawn 13
15 hours ago | 0.46 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!