diff --git a/src/array/visualizer/AAFrame.java b/src/array/visualizer/AAFrame.java index f03f9b38..0b5ceda4 100644 --- a/src/array/visualizer/AAFrame.java +++ b/src/array/visualizer/AAFrame.java @@ -5,11 +5,13 @@ package array.visualizer; /** - * * @author S630690 */ -interface AAFrame { - abstract void reposition(); - abstract boolean isVisible(); - abstract void dispose(); +interface AAFrame +{ + void reposition(); + + boolean isVisible(); + + void dispose(); } diff --git a/src/array/visualizer/ArrayController.java b/src/array/visualizer/ArrayController.java index 2b84dd74..855cc948 100644 --- a/src/array/visualizer/ArrayController.java +++ b/src/array/visualizer/ArrayController.java @@ -4,7 +4,7 @@ public class ArrayController { - public final int [] array; + public final int[] array; public final int length; public final ArrayList marked; public long aa; @@ -21,19 +21,21 @@ public ArrayController(int length) public void clearMarked() { - for(int i = 0; i < length; i++) + for (int i = 0; i < length; i++) + { marked.set(i, -5); + } } public int get(int index) { - aa ++; + aa++; return array[index]; } public int compare(int a, int b) { - comps ++; + comps++; return Integer.compare(array[a], array[b]); } } diff --git a/src/array/visualizer/ArrayVisualizer.java b/src/array/visualizer/ArrayVisualizer.java index 63258d27..35e0293a 100644 --- a/src/array/visualizer/ArrayVisualizer.java +++ b/src/array/visualizer/ArrayVisualizer.java @@ -2,10 +2,13 @@ import array.visualizer.sort.*; +import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.Image; +import java.awt.Polygon; import java.util.ArrayList; import javax.sound.midi.MidiChannel; import javax.sound.midi.MidiSystem; @@ -13,25 +16,22 @@ import javax.swing.JFrame; import static array.visualizer.utils.Swaps.*; -import java.awt.BasicStroke; -import java.awt.Graphics2D; -import java.awt.Polygon; + import java.util.logging.Level; import java.util.logging.Logger; import javax.sound.midi.Instrument; import javax.swing.JOptionPane; -public class ArrayVisualizer { +public class ArrayVisualizer +{ + private static final JFrame window = new JFrame(); - static final JFrame window = new JFrame(); + private static ArrayController arrayController = new ArrayController(1000); + private static String heading = ""; + private static int frames; + private static int snd = 0; + private static Font fon = new Font("TimesRoman", Font.PLAIN, (int) (640 / 1280.0 * 25)); - static ArrayController arrayController = new ArrayController(1000); - static String heading = ""; - static int frames; - static int snd = 0; - static long nanos; - static Font fon = new Font("TimesRoman", Font.PLAIN, (int)(640/1280.0*25)); - static boolean CIRCLEDRAW = false; static boolean COLORONLY = false; static boolean PIXELDRAW = false; @@ -41,71 +41,88 @@ public class ArrayVisualizer { static boolean PYRAMIDDRAW = false; static boolean DRAWFLIPPED = false; static boolean XMASDRAW = false; - + static boolean SOUND = false; static double SOUNDMUL = 1.0; static double SLEEPRATIO = 1.0; - static UtilFrame uf; - static ViewPrompt v; - static Synthesizer synth; - static MidiChannel chan; - static Thread sortingThread; + private static UtilFrame uf; + private static ViewPrompt v; + private static Synthesizer synth; + private static MidiChannel chan; + private static Thread sortingThread; static boolean SHUFFLEANIM = true; - - static long starttime = 0; - static long stoptime = 0; - static boolean running = false; - static long sleeptime = 0; - - static long rtupdatefreq = 100; //How frequently to update the real time display in ms - static double realt = 0d; - static long lastrtupdate = 0; - + + private static long startTime = 0; + private static long stopTime = 0; + private static boolean running = false; + private static long sleepTime = 0; + + private static long rtUpdateFreq = 100; //How frequently to update the real time display in ms + private static double realt = 0d; + private static long lastRtUpdate = 0; + static int COLORSTRAT = 1; //0 = Solid, 1 = Rainbow, 2 = Segments static Color COLORSTRAT0col = new Color(0, 204, 0); - static ArrayList COLORSTRAT2cols = new ArrayList(); - - static String[] ComparativeSorts = "Selection!Bubble!Insertion!Double Selection!Cocktail Shaker!Quick!Merge!Merge OOP!Weave Merge!Max Heap!Shell".split("!"); - static String[] DistributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Gravity!Shatter!Counting!Time!Bogo".split("!"); - - static int cx = 0; - static int cy = 0; - - public static double calcVel(){ + static ArrayList COLORSTRAT2cols = new ArrayList<>(); + + static String[] comparativeSorts = "Selection!Bubble!Insertion!Double Selection!Cocktail Shaker!Quick!Merge!Merge OOP!Weave Merge!Max Heap!Shell".split("!"); + static String[] distributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Gravity!Shatter!Counting!Time!Bogo".split("!"); + + private static int cx = 0; + private static int cy = 0; + + private static double addamt = 0.0; + + public static double calcVel() + { double count = 1; - for(int i : arrayController.marked) - if(i!=-5) - count+=0.75; + for (int i : arrayController.marked) + { + if (i != -5) + { + count += 0.75; + } + } return count; } - - public static synchronized void SetSound(boolean val){ + + public static synchronized void setSound(boolean val) + { SOUND = val; } - - static double addamt = 0.0; - public static void sleep(double milis){ - if(milis <= 0) + + public static void sleep(double milis) + { + if (milis <= 0) + { return; - double tmp = (milis*(1000.0/arrayController.length)); - tmp = tmp * (1/SLEEPRATIO); + } + double tmp = (milis * (1000.0 / arrayController.length)); + tmp = tmp * (1 / SLEEPRATIO); addamt += tmp; - if(addamt<1.0) + if (addamt < 1.0) + { return; - try{ + } + try + { long actual = System.nanoTime(); - Thread.sleep((long)addamt); - actual = (System.nanoTime()-actual); - addamt-=(double)actual/1000000.0; - if(running) - sleeptime+=actual; - }catch(Exception ex){ - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Thread.sleep((long) addamt); + actual = (System.nanoTime() - actual); + addamt -= (double) actual / 1000000.0; + if (running) + { + sleepTime += actual; + } + } catch (InterruptedException e) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } } - - public static void main(String[] args) throws Exception { - + + public static void main(String[] args) throws Exception + { + //Segment Colors COLORSTRAT2cols.add(new Color(0, 204, 0)); COLORSTRAT2cols.add(new Color(204, 0, 0)); @@ -120,56 +137,72 @@ public static void main(String[] args) throws Exception { // s++; // } chan = synth.getChannels()[0]; - for(Instrument i : synth.getLoadedInstruments()) - if(i.getName().toLowerCase().trim().contains("sine")){ + for (Instrument i : synth.getLoadedInstruments()) + { + if (i.getName().toLowerCase().trim().contains("sine")) + { chan.programChange(i.getPatch().getProgram()); break; } - - if(chan.getProgram() == 0) + } + + if (chan.getProgram() == 0) + { JOptionPane.showMessageDialog(null, "Could not find a valid instrument. Sound is disabled"); + } //chan.programChange(synth.getLoadedInstruments()[197].getPatch().getProgram()); - - for(int i = 0; i < arrayController.length; i++) + + for (int i = 0; i < arrayController.length; i++) + { arrayController.marked.add(-5); + } rianr(arrayController.array); - + window.setSize(640, 480); window.setLocation(0, 0); window.setVisible(true); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setTitle("Array Visualizer"); - + uf = new UtilFrame(window); - + //AUDIO THREAD new Thread() { @Override public void run() + { + while (true) { - while(true){ - chan.allNotesOff(); - if(SOUND == false){ - continue; - } + chan.allNotesOff(); + if (!SOUND) + { + continue; + } - int tmp = 1; - for(int i : arrayController.marked) - if(i != -5) + int tmp = 1; + for (int i : arrayController.marked) + { + if (i != -5) + { tmp++; - for(int i : arrayController.marked) - if(i != -5){ - int pitch = (int)Math.round((double)arrayController.array[Math.min(Math.max(i, 0), arrayController.length-1)]/arrayController.length*96+16); + } + } + for (int i : arrayController.marked) + { + if (i != -5) + { + int pitch = (int) Math.round((double) arrayController.array[Math.min(Math.max(i, 0), arrayController.length - 1)] / arrayController.length * 96 + 16); //int vel = (int)(((128-pitch)/320.0+0.4) * (128.0/Math.pow(tmp, 0.33))); //int vel = (int)(64.0/Math.pow(tmp, 0.25)); //int vel = (int)((64.0-Math.pow((tmp-1)*10, 0.67d))*SOUNDMUL); - int vel = (int)(64.0/(Math.pow((double)tmp, 0.6)) * ((128-pitch)/64.0+1.0)*0.67*SOUNDMUL); //one day + int vel = (int) (64.0 / (Math.pow((double) tmp, 0.6)) * ((128 - pitch) / 64.0 + 1.0) * 0.67 * SOUNDMUL); //one day chan.noteOn(pitch, vel); } - //((int)((127-(array[Math.min(Math.max(i, 0), array.length-1)]/16.0))/Math.sqrt(calcVel())))*5 - //double tmp = (array[Math.min(Math.max(i, 0), array.length-1)]/32.0+47); - //chan.setPitchBend(8192*2-(int)((tmp-Math.floor(tmp))*8192*2)); + } + //((int)((127-(array[Math.min(Math.max(i, 0), array.length-1)]/16.0))/Math.sqrt(calcVel())))*5 + //double tmp = (array[Math.min(Math.max(i, 0), array.length-1)]/32.0+47); + //chan.setPitchBend(8192*2-(int)((tmp-Math.floor(tmp))*8192*2)); /* ArrayList tmp = new ArrayList(); for(int i : marked) @@ -184,463 +217,590 @@ public void run() double tmpd = (array[Math.min(Math.max(i, 0), array.length-1)]/32.0+47); chan.setPitchBend(8192*2-(int)((tmpd-Math.floor(tmpd))*8192*2)); }while(false);}*/ - try{sleep(1);}catch(Exception ex){ - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + try + { + sleep(1); + } catch (InterruptedException e) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); + e.printStackTrace(); } } } }.start(); - + //DRAW THREAD - new Thread(){ + new Thread() + { @Override - public void run(){ + public void run() + { int cw = 640; int ch = 480; Image img = window.createVolatileImage(cw, ch); //Graphics g = window.getGraphics(); - Graphics2D g = (Graphics2D)img.getGraphics(); - + Graphics2D g = (Graphics2D) img.getGraphics(); + double xscl, yscl; - while(true){ - - if(window.getWidth()!=cw|| window.getHeight()!=ch || window.getX() != cx || window.getY() != cy){ + while (true) + { + + if (window.getWidth() != cw || window.getHeight() != ch || window.getX() != cx || window.getY() != cy) + { uf.reposition(); - if(v != null && v.isVisible()) + if (v != null && v.isVisible()) + { v.reposition(); + } cx = window.getX(); - cy = window.getY(); + cy = window.getY(); } - - if(window.getWidth()!=cw|| window.getHeight()!=ch){ + + if (window.getWidth() != cw || window.getHeight() != ch) + { cw = window.getWidth(); ch = window.getHeight(); img = window.createVolatileImage(cw, ch); - fon = new Font("TimesRoman", Font.PLAIN, (int)(cw/1280.0*25)); - g = (Graphics2D)img.getGraphics(); - + fon = new Font("TimesRoman", Font.PLAIN, (int) (cw / 1280.0 * 25)); + g = (Graphics2D) img.getGraphics(); + } int gamt = 32;//(int)(frames/1000.0%64); g.setColor(new Color(gamt, gamt, gamt)); g.fillRect(0, 0, img.getWidth(null), img.getHeight(null)); - xscl = (double)window.getWidth()/arrayController.length; - yscl = (double)(window.getHeight()-30)/arrayController.length; + xscl = (double) window.getWidth() / arrayController.length; + yscl = (double) (window.getHeight() - 30) / arrayController.length; int amt = 0; - int circamt = arrayController.length/2; - int linkedpixdrawx = 0; - int linkedpixdrawy = 0; + int circamt = arrayController.length / 2; + int linkedPixDrawX = 0; + int linkedPixDrawY= 0; frames++; - - int halfwidth = window.getWidth()/2; - int halfheight = window.getHeight()/2; - int dotw = (int)(2*(window.getWidth()/640.0)); - int doth = (int)(2*(window.getHeight()/480.0)); - + + int halfWidth = window.getWidth() / 2; + int halfHeight = window.getHeight() / 2; + int dotw = (int) (2 * (window.getWidth() / 640.0)); + int doth = (int) (2 * (window.getHeight() / 480.0)); + int sortErrors = 0; - - float strokew = 3f*(window.getWidth()/1920f); + + float strokew = 3f * (window.getWidth() / 1920f); g.setStroke(new BasicStroke(strokew)); - + //CHRISTMAS TREE? - if(XMASDRAW){ - - double trunksize = (double)ch*0.1; - int trunkstart = arrayController.length-(int)(trunksize/yscl); - - double ornamentsize = (double)ch*0.1; - int ornamentend = (int)(ornamentsize/yscl); - + if (XMASDRAW) + { + + double trunkSize = (double) ch * 0.1; + int trunkStart = arrayController.length - (int) (trunkSize / yscl); + + double ornamentSize = (double) ch * 0.1; + int ornamentEnd = (int) (ornamentSize / yscl); + int width = 0; - - for(int i = 0; i < arrayController.length; i++){ + + for (int i = 0; i < arrayController.length; i++) + { //Check for sort errors in array - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; - + } + int j = arrayController.array[i]; - - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else if(jtrunkstart) + } else if (j > trunkStart) + { g.setColor(new Color(102, 34, 0)); - else + } else + { g.setColor(new Color(0, 102, 0)); - - if(jtrunkstart){ //TRUNK - width = (int)(cw*0.2); - }else //TREE - width = (int)((((j-ornamentend)%85)*0.005+0.075)*cw); - - int step = (int)(i*yscl+yscl)-(int)(i*yscl); - g.fillRect(halfwidth-width/2, (int)(i*yscl)+32, width, step); - + } + + if (j < ornamentEnd) + { //ORNAMENT + width = (int) (Math.sin(Math.acos((j * yscl) / ornamentSize * 2.0 - 1.0)) * ornamentSize); + } else if (j > trunkStart) + { //TRUNK + width = (int) (cw * 0.2); + } else //TREE + { + width = (int) ((((j - ornamentEnd) % 85) * 0.005 + 0.075) * cw); + } + + int step = (int) (i * yscl + yscl) - (int) (i * yscl); + g.fillRect(halfWidth - width / 2, (int) (i * yscl) + 32, width, step); + } } //HOOP DRAWING (PYRAMID CIRCLES) - else if(PYRAMIDDRAW && CIRCLEDRAW){ - + else if (PYRAMIDDRAW && CIRCLEDRAW) + { + g.setStroke(new BasicStroke(1.0f)); //significantly increased performance - - double maxdiam = (double)Math.min(cw, ch-32); - double diameter = maxdiam; - double diamstep = Math.min(xscl, yscl); - - for(int i = arrayController.length-1; i >= 0; i--){ + + double diameter = (double) Math.min(cw, ch - 32); + double diamStep = Math.min(xscl, yscl); + + for (int i = arrayController.length - 1; i >= 0; i--) + { //Check for sort errors in array - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; - + } + //Set Proper Color - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); - - int radius = (int)(diameter/2.0); - - if(!DISPARITYDRAW) - g.drawOval(halfwidth-radius, halfheight-radius+15, (int)diameter, (int)diameter); - else{ - int dist = Math.abs(i-arrayController.array[i]); - double disparity = 1.0-((double)Math.min(dist, arrayController.length-dist)/((double)arrayController.length/2.0)); - int actualdiam = (int)(disparity*diameter); - g.drawOval(halfwidth-actualdiam/2, halfheight-radius+15, (int)actualdiam, (int)actualdiam); } - diameter-=diamstep; + + int radius = (int) (diameter / 2.0); + + if (!DISPARITYDRAW) + { + g.drawOval(halfWidth - radius, halfHeight - radius + 15, (int) diameter, (int) diameter); + } else + { + int dist = Math.abs(i - arrayController.array[i]); + double disparity = 1.0 - ((double) Math.min(dist, arrayController.length - dist) / ((double) arrayController.length / 2.0)); + int actualDiam = (int) (disparity * diameter); + g.drawOval(halfWidth - actualDiam / 2, halfHeight - radius + 15, actualDiam, actualDiam); + } + diameter -= diamStep; } } //PYRAMID DRAW METHOD - else if(PYRAMIDDRAW){ - + else if (PYRAMIDDRAW) + { + //Setup - double curpos = 0.0; - + double curPos = 0.0; + //Draw loop - for(int i = 0; i < arrayController.length; i++){ + for (int i = 0; i < arrayController.length; i++) + { //Check for sort errors in array - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; - + } + //Set Proper Color - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); - + } + //Draw - if(!DRAWFLIPPED){ - int len = (int)((double)arrayController.array[i]/(double)arrayController.length*(double)cw); - int step = (int)(curpos+yscl) - (int)curpos; - g.fillRect(halfwidth-len/2, (int)curpos+32, len, step); - curpos += yscl; - }else{ - int len = (int)((double)arrayController.array[i]/(double)arrayController.length*((double)ch-32.0)); - int step = (int)(curpos+xscl) - (int)curpos; - g.fillRect((int)curpos, halfheight-len/2+16, step, len); - curpos += xscl; + if (!DRAWFLIPPED) + { + int len = (int) ((double) arrayController.array[i] / (double) arrayController.length * (double) cw); + int step = (int) (curPos + yscl) - (int) curPos; + g.fillRect(halfWidth - len / 2, (int) curPos + 32, len, step); + curPos += yscl; + } else + { + int len = (int) ((double) arrayController.array[i] / (double) arrayController.length * ((double) ch - 32.0)); + int step = (int) (curPos + xscl) - (int) curPos; + g.fillRect((int) curPos, halfHeight - len / 2 + 16, step, len); + curPos += xscl; } } } //MESH DRAW METHOD - else if(MESHDRAW){ - - int trih = window.getHeight()/20; //Height of triangles to use, Width will be scaled accordingly - - int tripercol = window.getHeight()/trih*2; //Triangles per vertical column - int triperrow = arrayController.length/tripercol; //Triangles per horizontal row - double triw = (double)window.getWidth()/triperrow; //Width of triangles to use - + else if (MESHDRAW) + { + + int trih = window.getHeight() / 20; //Height of triangles to use, Width will be scaled accordingly + + int triPerCol = window.getHeight() / trih * 2; //Triangles per vertical column + int triPerRow = arrayController.length / triPerCol; //Triangles per horizontal row + double triw = (double) window.getWidth() / triPerRow; //Width of triangles to use + double curx = 0; int cury = 0; - + int[] triptsx = new int[3]; int[] triptsy = new int[3]; - - for(int i = 0; i < arrayController.length; i++){ - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; - - if(arrayController.marked.contains(i)/*||marked.contains(i-1)||marked.contains(i-2)||marked.contains(i-3)*/) + } + + if (arrayController.marked.contains(i)/*||marked.contains(i-1)||marked.contains(i-2)||marked.contains(i-3)*/) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); - - //If i/triperrow is even, then triangle points right, else left + } + + //If i/triPerRow is even, then triangle points right, else left boolean direction = false; - if(((i-1)/triperrow)%2==0) + if (((i - 1) / triPerRow) % 2 == 0) + { direction = true; - + } + //Make the triangle - if(!direction){ + if (!direction) + { //Pointing right - triptsx[0] = (int)curx; - triptsx[1] = (int)curx; - curx+=triw; - triptsx[2] = (int)curx; - - if(!COLORONLY) - triptsx[2] = (int)(triptsx[1]+triw*arrayController.array[i]/arrayController.length); - + triptsx[0] = (int) curx; + triptsx[1] = (int) curx; + curx += triw; + triptsx[2] = (int) curx; + + if (!COLORONLY) + { + triptsx[2] = (int) (triptsx[1] + triw * arrayController.array[i] / arrayController.length); + } + triptsy[0] = cury; - triptsy[2] = cury + trih/2; + triptsy[2] = cury + trih / 2; triptsy[1] = cury + trih; - }else{ + } else + { //Pointing left - triptsx[2] = (int)curx; - curx+=triw; - triptsx[0] = (int)curx; - triptsx[1] = (int)curx; - - if(!COLORONLY) - triptsx[2] = (int)(triptsx[0]-triw*arrayController.array[i]/arrayController.length); - + triptsx[2] = (int) curx; + curx += triw; + triptsx[0] = (int) curx; + triptsx[1] = (int) curx; + + if (!COLORONLY) + { + triptsx[2] = (int) (triptsx[0] - triw * arrayController.array[i] / arrayController.length); + } + triptsy[0] = cury; - triptsy[2] = cury + trih/2; + triptsy[2] = cury + trih / 2; triptsy[1] = cury + trih; } - + //Draw it g.fillPolygon(triptsx, triptsy, triptsx.length); - + //If at the end of a row, reset curx - if(i != 0 && i%triperrow == 0){ + if (i != 0 && i % triPerRow == 0) + { curx = 0d; - cury+=trih/2; + cury += trih / 2; } } - } - else if(CIRCLEDRAW) - for(int i = 0; i < arrayController.length; i++){ - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; - - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + } + + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); - - double sinval = Math.sin(i*Math.PI/circamt); - double cosval = Math.cos(i*Math.PI/circamt); - + } + + double sinVal = Math.sin(i * Math.PI / circamt); + double cosVal = Math.cos(i * Math.PI / circamt); + //COLOR ONLY NO LENGTH - if(COLORONLY){ + if (COLORONLY) + { Polygon p = new Polygon(); - p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth+(int)(sinval*(window.getWidth()-64)/2.0), halfheight-(int)(cosval*(window.getHeight()-96)/2.0)); - p.addPoint(halfwidth+(int)(Math.sin((i+1)*Math.PI/circamt)*(window.getWidth()-64)/2.0), halfheight-(int)(Math.cos((i+1)*Math.PI/circamt)*(window.getHeight()-96)/2.0)); + p.addPoint(halfWidth, halfHeight); + p.addPoint(halfWidth + (int) (sinVal * (window.getWidth() - 64) / 2.0), halfHeight - (int) (cosVal * (window.getHeight() - 96) / 2.0)); + p.addPoint(halfWidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * (window.getWidth() - 64) / 2.0), halfHeight - (int) (Math.cos((i + 1) * Math.PI / circamt) * (window.getHeight() - 96) / 2.0)); g.fillPolygon(p); - //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*(window.getWidth()-64)/2.0), halfheight-(int)(cosval*(window.getHeight()-96)/2.0)); + //g.drawLine(halfWidth, halfHeight, halfWidth+(int)(sinVal*(window.getWidth()-64)/2.0), halfHeight-(int)(cosVal*(window.getHeight()-96)/2.0)); } //DISPARITY - else if (DISPARITYDRAW){ - double len = (500d-Math.min(Math.min(Math.abs(i-arrayController.array[i]), Math.abs(i-arrayController.array[i]+1000)), Math.abs(i-arrayController.array[i]-1000)))/500d; - - if(PIXELDRAW){ - int linkedpixX = halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*len)) + dotw/2; - int linkedpixY = halfheight-(int)(cosval*((window.getHeight()-96)/2.0*len)) + doth/2; - - g.fillRect(linkedpixX - dotw/2, linkedpixY - doth/2, dotw, doth); - if(LINKEDPIXELDRAW){ - if(i>0) - g.drawLine(linkedpixX, linkedpixY, linkedpixdrawx, linkedpixdrawy); - linkedpixdrawx = linkedpixX; - linkedpixdrawy = linkedpixY; + else if (DISPARITYDRAW) + { + double len = (500d - Math.min(Math.min(Math.abs(i - arrayController.array[i]), Math.abs(i - arrayController.array[i] + 1000)), Math.abs(i - arrayController.array[i] - 1000))) / 500d; + + if (PIXELDRAW) + { + int linkedPixX = halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * len)) + dotw / 2; + int linkedPixY = halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * len)) + doth / 2; + + g.fillRect(linkedPixX - dotw / 2, linkedPixY - doth / 2, dotw, doth); + if (LINKEDPIXELDRAW) + { + if (i > 0) + { + g.drawLine(linkedPixX, linkedPixY, linkedPixDrawX, linkedPixDrawY); + } + linkedPixDrawX = linkedPixX; + linkedPixDrawY = linkedPixY; } - } - else{ + } else + { Polygon p = new Polygon(); - p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*len)), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*len))); - p.addPoint(halfwidth+(int)(Math.sin((i+1)*Math.PI/circamt)*((window.getWidth()-64)/2.0*len)), halfheight-(int)(Math.cos((i+1)*Math.PI/circamt)*((window.getHeight()-96)/2.0*len))); + p.addPoint(halfWidth, halfHeight); + p.addPoint(halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * len)), halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * len))); + p.addPoint(halfWidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * len)), halfHeight - (int) (Math.cos((i + 1) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * len))); g.fillPolygon(p); } - //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); + //g.drawLine(halfWidth, halfHeight, halfWidth+(int)(sinVal*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfHeight-(int)(cosVal*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); } //PIXELS ONLY - else if(PIXELDRAW){ - g.fillRect(halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(arrayController.array[i]/(double)arrayController.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(arrayController.array[i]/(double)arrayController.length))), dotw, doth); - if(LINKEDPIXELDRAW){ - if(i>0) - g.drawLine(halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(arrayController.array[i]/(double)arrayController.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(arrayController.array[i]/(double)arrayController.length))), linkedpixdrawx, linkedpixdrawy); - linkedpixdrawx = halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(arrayController.array[i]/(double)arrayController.length))); - linkedpixdrawy = halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(arrayController.array[i]/(double)arrayController.length))); + else if (PIXELDRAW) + { + g.fillRect(halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), dotw, doth); + if (LINKEDPIXELDRAW) + { + if (i > 0) + { + g.drawLine(halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), linkedPixDrawX, linkedPixDrawY); + } + linkedPixDrawX = halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); + linkedPixDrawY = halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); } } //LENGTH AND COLOR - else{ + else + { Polygon p = new Polygon(); - p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth+(int)(Math.sin((i)*Math.PI/circamt)*((window.getWidth()-64)/2.0*(arrayController.array[i]/(double)arrayController.length))), halfheight-(int)(Math.cos((i)*Math.PI/circamt)*((window.getHeight()-96)/2.0*(arrayController.array[i]/(double)arrayController.length)))); - p.addPoint(halfwidth+(int)(Math.sin((i+1)*Math.PI/circamt)*((window.getWidth()-64)/2.0*(arrayController.array[Math.min(i+1, arrayController.length-1)]/(double)arrayController.length))), halfheight-(int)(Math.cos((i+1)*Math.PI/circamt)*((window.getHeight()-96)/2.0*(arrayController.array[Math.min(i+1, arrayController.length-1)]/(double)arrayController.length)))); + p.addPoint(halfWidth, halfHeight); + p.addPoint(halfWidth + (int) (Math.sin((i) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfHeight - (int) (Math.cos((i) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length)))); + p.addPoint(halfWidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * (arrayController.array[Math.min(i + 1, arrayController.length - 1)] / (double) arrayController.length))), halfHeight - (int) (Math.cos((i + 1) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * (arrayController.array[Math.min(i + 1, arrayController.length - 1)] / (double) arrayController.length)))); g.fillPolygon(p); - //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); + //g.drawLine(halfWidth, halfHeight, halfWidth+(int)(sinVal*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfHeight-(int)(cosVal*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); } } - else - for(int i = 0; i < arrayController.length; i++){ - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; - - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + } + + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); - + } + int y = 0; - int width = (int)(xscl*i)-amt; - - if(width>0){ - if(COLORONLY){ - y = (int)(window.getHeight()-750*yscl); - g.fillRect(amt, y, width, Math.max((int)(750*yscl), 1)); + int width = (int) (xscl * i) - amt; + + if (width > 0) + { + if (COLORONLY) + { + y = (int) (window.getHeight() - 750 * yscl); + g.fillRect(amt, y, width, Math.max((int) (750 * yscl), 1)); g.setColor(getRevColor()); - g.fillRect((int)(i*xscl), y, width, 6); - } - else if(PIXELDRAW){ - y = (int)(window.getHeight()-arrayController.array[i]*yscl); - g.fillRect(amt-dotw/2, y-doth/2, dotw, doth); - if(LINKEDPIXELDRAW){ - if(i>0) - g.drawLine(amt, y, linkedpixdrawx, linkedpixdrawy); - linkedpixdrawx = amt; - linkedpixdrawy = y; + g.fillRect((int) (i * xscl), y, width, 6); + } else if (PIXELDRAW) + { + y = (int) (window.getHeight() - arrayController.array[i] * yscl); + g.fillRect(amt - dotw / 2, y - doth / 2, dotw, doth); + if (LINKEDPIXELDRAW) + { + if (i > 0) + { + g.drawLine(amt, y, linkedPixDrawX, linkedPixDrawY); + } + linkedPixDrawX = amt; + linkedPixDrawY = y; } - } - else{ - y = (int)(window.getHeight()-arrayController.array[i]*yscl); - g.fillRect(amt, y, width, Math.max((int)(arrayController.array[i]*yscl), 1)); + } else + { + y = (int) (window.getHeight() - arrayController.array[i] * yscl); + g.fillRect(amt, y, width, Math.max((int) (arrayController.array[i] * yscl), 1)); g.setColor(getRevColor()); //g.fillRect(amt, y, width, 6); FILAMENT } } - amt+=width; + amt += width; } - + } + int coltmp = 255;//(int)Math.abs(Math.sin(frames*0.01)*255); g.setColor(new Color(coltmp, coltmp, coltmp)); Font f = g.getFont(); g.setFont(fon); - g.drawString(heading, 10, (int)(cw/1280.0*20)+30); - g.drawString(formatNum(arrayController.comps)+" Comparison"+(arrayController.comps==1?"":"s"), 10, (int)(cw/1280.0*40)+30); - g.drawString(formatNum(arrayController.aa)+" Array Access"+(arrayController.aa==1?"":"es"), 10, (int)(cw/1280.0*60)+30); - int sortpercent = (int)((double)(arrayController.length-sortErrors)/(double)arrayController.length*100.0); - g.drawString(String.format("%d%% Sorted (%d Segment%c)", sortpercent, sortErrors+1, sortErrors==0?' ':'s'), 10, (int)(cw/1280.0*80)+30); + g.drawString(heading, 10, (int) (cw / 1280.0 * 20) + 30); + g.drawString(formatNum(arrayController.comps) + " Comparison" + (arrayController.comps == 1 ? "" : "s"), 10, (int) (cw / 1280.0 * 40) + 30); + g.drawString(formatNum(arrayController.aa) + " Array Access" + (arrayController.aa == 1 ? "" : "es"), 10, (int) (cw / 1280.0 * 60) + 30); + int sortpercent = (int) ((double) (arrayController.length - sortErrors) / (double) arrayController.length * 100.0); + g.drawString(String.format("%d%% Sorted (%d Segment%c)", sortpercent, sortErrors + 1, sortErrors == 0 ? ' ' : 's'), 10, (int) (cw / 1280.0 * 80) + 30); //g.drawString(String.format("%d Segment%c", sortErrors+1, sortErrors==0?' ':'s'), 10, (int)(cw/1280.0*100)+30); - double slpt = (double)sleeptime/1000000000.0; - if(System.currentTimeMillis()-lastrtupdate > rtupdatefreq){ - realt = (double)(running?(System.nanoTime()-starttime-sleeptime):(stoptime-starttime-sleeptime))/1000000.0; - lastrtupdate = System.currentTimeMillis(); + double slpt = (double) sleepTime / 1000000000.0; + if (System.currentTimeMillis() - lastRtUpdate > rtUpdateFreq) + { + realt = (double) (running ? (System.nanoTime() - startTime - sleepTime) : (stopTime - startTime - sleepTime)) / 1000000.0; + lastRtUpdate = System.currentTimeMillis(); } - g.drawString(String.format("Real Time: %.2fms", realt), 10, (int)(cw/1280.0*100)+30); - g.drawString(String.format("Sleep Time: %.2fs", slpt), 10, (int)(cw/1280.0*120)+30); + g.drawString(String.format("Real Time: %.2fms", realt), 10, (int) (cw / 1280.0 * 100) + 30); + g.drawString(String.format("Sleep Time: %.2fs", slpt), 10, (int) (cw / 1280.0 * 120) + 30); g.setFont(f); Graphics g2 = window.getGraphics(); g2.setColor(Color.BLACK); g2.drawImage(img, 0, 0, null); } } - + //0 = Solid, 1 = Rainbow, 2 = Segments - public Color getIntColor(int i, int segnum) { - if(COLORSTRAT == 1) - return Color.getHSBColor(((float)i/arrayController.length), 1.0F, 0.8F); - else if(COLORSTRAT == 2){ - return COLORSTRAT2cols.get(segnum%COLORSTRAT2cols.size()); + public Color getIntColor(int i, int segNum) + { + if (COLORSTRAT == 1) + { + return Color.getHSBColor(((float) i / arrayController.length), 1.0F, 0.8F); + } else if (COLORSTRAT == 2) + { + return COLORSTRAT2cols.get(segNum % COLORSTRAT2cols.size()); } return COLORSTRAT0col; } - public Color getRevColor(){ - return getIntColor((int)(Math.sin(frames/66.67)*arrayController.length), 0); + + public Color getRevColor() + { + return getIntColor((int) (Math.sin(frames / 66.67) * arrayController.length), 0); } }.start(); - + uf.setVisible(false); v = new ViewPrompt(window); - while(v.isVisible()) Thread.sleep(1); + while (v.isVisible()) + { + Thread.sleep(1); + } uf.setVisible(true); - + //keep on keeping on - while(window.isActive())Thread.sleep(100); + while (window.isActive()) + { + Thread.sleep(100); + } } - - public static void refresharray() throws Exception { - clearmarked(); - if(running){ - stoptime = System.nanoTime(); + + public static void refreshArray() throws InterruptedException + { + clearMarked(); + if (running) + { + stopTime = System.nanoTime(); running = false; Thread.sleep(1000); } - + Thread.sleep(1000); boolean solved = true; - for(int i = 0; i < arrayController.length; i++){ - if(arrayController.array[i]!=i) + for (int i = 0; i < arrayController.length; i++) + { + if (arrayController.array[i] != i) + { solved = false; + } arrayController.marked.set(0, i); } - for(int i = 0; i < arrayController.length; i++) + for (int i = 0; i < arrayController.length; i++) + { arrayController.array[i] = i; + } //System.out.println(solved); arrayController.marked.set(0, -5); heading = ""; arrayController.aa = 0; arrayController.comps = 0; shuffle(arrayController.array); - clearmarked(); + clearMarked(); Thread.sleep(500); - starttime = System.nanoTime(); - sleeptime = 0; + startTime = System.nanoTime(); + sleepTime = 0; running = true; } - - public static int getDigit(int a, int power, int radix){ + + public static int getDigit(int a, int power, int radix) + { return (int) (a / Math.pow(radix, power)) % radix; } - - public static int[] rianr(int [] arr) { + + public static void rianr(int[] arr) + { for (int i = 0; i < arr.length; i++) + { arr[i] = i; + } shuffle(arr); - return arr; } - public static void clearmarked(){ + public static void clearMarked() + { arrayController.clearMarked(); } - public static void shuffle(int[] array) { + public static void shuffle(int[] array) + { String tmp = heading; heading = "Shuffling..."; - for(int i = 0; i < array.length; i++){ - swap(arrayController, i, (int)(Math.random()*array.length)); - arrayController.aa-=2; - if(SHUFFLEANIM) + for (int i = 0; i < array.length; i++) + { + swap(arrayController, i, (int) (Math.random() * array.length)); + arrayController.aa -= 2; + if (SHUFFLEANIM) + { sleep(1); + } } heading = tmp; } - public static int sleepTime(double d) { - return (int)(arrayController.length*d)/4; + public static int sleepTime(double d) + { + return (int) (arrayController.length * d) / 4; } - - public synchronized static void RunAllSorts(){ - if(sortingThread != null) - while(sortingThread.isAlive()) try { - Thread.sleep(100); - } catch (InterruptedException ex) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + + public synchronized static void runAllSorts() + { + if (sortingThread != null) + { + while (sortingThread.isAlive()) + { + try + { + Thread.sleep(100); + } catch (InterruptedException ex) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + } + } } - SetSound(true); - sortingThread = new Thread(){ + setSound(true); + sortingThread = new Thread() + { @Override - public void run(){ - try{ + public void run() + { + try + { for (Sort sort : new Sort[]{ new SelectionSort(), new BubbleSort(), @@ -659,153 +819,213 @@ public void run(){ new RadixMSD(4), new RadixLSDInPlace(2), new RadixLSDInPlace(10)} - ) + ) { chan.allNotesOff(); - refresharray(); + refreshArray(); heading = sort.name(); sort.sort(arrayController); } - }catch (Exception ex){ - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + } catch (InterruptedException e) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } - SetSound(false); - stoptime = System.nanoTime(); + setSound(false); + stopTime = System.nanoTime(); running = false; chan.allNotesOff(); - clearmarked(); + clearMarked(); } }; sortingThread.start(); } - - public static void ReportComparativeSort(int n){ - if(sortingThread != null && sortingThread.isAlive()) + + public static void reportComparativeSort(int n) + { + if (sortingThread != null && sortingThread.isAlive()) + { return; - + } + final int num = n; - SetSound(true); - sortingThread = new Thread(){ + setSound(true); + sortingThread = new Thread() + { @Override - public void run(){ - try{ - refresharray(); + public void run() + { + try + { + refreshArray(); Sort sort; switch (num) { case 0: - sort = new SelectionSort();break; + sort = new SelectionSort(); + break; case 1: - sort = new BubbleSort();break; + sort = new BubbleSort(); + break; case 2: - sort = new InsertionSort();break; + sort = new InsertionSort(); + break; case 3: - sort = new DoubleSelection();break; + sort = new DoubleSelection(); + break; case 4: - sort = new CocktailShaker();break; + sort = new CocktailShaker(); + break; case 5: - sort = new QuickSort();break; + sort = new QuickSort(); + break; case 6: - sort = new MergeSort();break; + sort = new MergeSort(); + break; case 7: - sort = new MergeSortOOP();break; + sort = new MergeSortOOP(); + break; case 8: - sort = new WeaveMerge();break; + sort = new WeaveMerge(); + break; case 9: - sort = new MaxHeapSort();break; + sort = new MaxHeapSort(); + break; case 10: - sort = new ShellSort();break; + sort = new ShellSort(); + break; default: - sort = null; break; + sort = null; + break; } if (sort != null) { heading = sort.name(); sort.sort(arrayController); } - }catch(Exception ex) + } catch (InterruptedException e) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } - SetSound(false); - stoptime = System.nanoTime(); + setSound(false); + stopTime = System.nanoTime(); running = false; } }; sortingThread.start(); } - - public static void ReportDistributiveSort(int n){ - if(sortingThread != null && sortingThread.isAlive()) + + public static void reportDistributiveSort(int n) + { + if (sortingThread != null && sortingThread.isAlive()) + { return; + } int bas = 10; - if(n != 3 && n != 5 && n != 7) - if(n != 4) - try{bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Base for Sort"));}catch(Exception ex){ - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + if (n != 3 && n != 5 && n != 7) + { + if (n != 4) + { + try + { + bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Base for Sort")); + } catch (NumberFormatException e) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } - else - try{bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Size of Partitions"));}catch(Exception ex){ - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + } else + { + try + { + bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Size of Partitions")); + } catch (NumberFormatException e) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } - + } + } + final int base = Math.max(bas, 2); final int num = n; - SetSound(true); - sortingThread = new Thread(){ + setSound(true); + sortingThread = new Thread() + { @Override - public void run(){ - try{ - refresharray(); - Sort sort; - switch (num) { - case 0: - sort = new RadixLSD(base);break; - case 1: - sort = new RadixMSD(base);break; - case 2: - sort = new RadixLSDInPlace(base);break; - case 3: - sort = new GravitySort();break; - case 4: - sort = new ShatterSorts(base);break; - case 5: - sort = new CountingSort();break; - case 6: - sort = new TimeSort(base);break; - case 7: - sort = new BogoSort();break; - default: - sort = null; break; - } - if (sort != null) + public void run() { - heading = sort.name(); - sort.sort(arrayController); - } - }catch(Exception ex){ - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); - } - SetSound(false); - stoptime = System.nanoTime(); - running = false; + try + { + refreshArray(); + Sort sort; + switch (num) + { + case 0: + sort = new RadixLSD(base); + break; + case 1: + sort = new RadixMSD(base); + break; + case 2: + sort = new RadixLSDInPlace(base); + break; + case 3: + sort = new GravitySort(); + break; + case 4: + sort = new ShatterSorts(base); + break; + case 5: + sort = new CountingSort(); + break; + case 6: + sort = new TimeSort(base); + break; + case 7: + sort = new BogoSort(); + break; + default: + sort = null; + break; + } + if (sort != null) + { + heading = sort.name(); + sort.sort(arrayController); + } + } catch (InterruptedException e) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); + } + setSound(false); + stopTime = System.nanoTime(); + running = false; } }; sortingThread.start(); } - - public static String formatNum(long a){ - if(a<0) + + public static String formatNum(long a) + { + if (a < 0) + { return "OVERFLOW"; - if(a>1000L*1000L*1000L*1000L) - return String.format("%.2fT", (double)a/1000000000000.0); - if(a>1000L*1000L*1000L) - return String.format("%.2fB", (double)a/1000000000.0); - if(a>1000L*1000L) - return String.format("%.2fM", (double)a/1000000.0); - else if(a>1000L) - return String.format("%.2fK", (double)a/1000.0); - else - return ""+a; + } + if (a > 1000L * 1000L * 1000L * 1000L) + { + return String.format("%.2fT", (double) a / 1000000000000.0); + } + if (a > 1000L * 1000L * 1000L) + { + return String.format("%.2fB", (double) a / 1000000000.0); + } + if (a > 1000L * 1000L) + { + return String.format("%.2fM", (double) a / 1000000.0); + } else if (a > 1000L) + { + return String.format("%.2fK", (double) a / 1000.0); + } else + { + return "" + a; + } } } diff --git a/src/array/visualizer/SortPrompt.java b/src/array/visualizer/SortPrompt.java index 5dd395f0..7121c6fc 100644 --- a/src/array/visualizer/SortPrompt.java +++ b/src/array/visualizer/SortPrompt.java @@ -5,32 +5,37 @@ package array.visualizer; import static array.visualizer.ArrayVisualizer.*; + import javax.swing.JFrame; /** - * * @author S630690 */ -public class SortPrompt extends javax.swing.JFrame implements AAFrame{ +public class SortPrompt extends javax.swing.JFrame implements AAFrame +{ + + private JFrame f; - JFrame f; /** * Creates new form SortPrompt */ - public SortPrompt(JFrame f) { + public SortPrompt(JFrame f) + { this.f = f; setAlwaysOnTop(true); setUndecorated(true); initComponents(); - jList2.setListData(ComparativeSorts); - jList1.setListData(DistributiveSorts); - setLocation(f.getX()+(f.getWidth()-getWidth())/2, f.getY()+(f.getHeight()-getHeight())/2); + jList2.setListData(comparativeSorts); + jList1.setListData(distributiveSorts); + setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); setVisible(true); } - public void reposition(){ - setLocation(f.getX()+(f.getWidth()-getWidth())/2, f.getY()+(f.getHeight()-getHeight())/2); + + public void reposition() + { + setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); } - + /** * This method is called from within the constructor to initialize the form. @@ -39,7 +44,8 @@ public void reposition(){ */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents - private void initComponents() { + private void initComponents() + { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); @@ -57,40 +63,66 @@ private void initComponents() { jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jList2.setModel(new javax.swing.AbstractListModel() { - String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; - public int getSize() { return strings.length; } - public Object getElementAt(int i) { return strings[i]; } + jList2.setModel(new javax.swing.AbstractListModel() + { + String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; + + public int getSize() + { + return strings.length; + } + + public Object getElementAt(int i) + { + return strings[i]; + } }); - jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() { - public void valueChanged(javax.swing.event.ListSelectionEvent evt) { + jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() + { + public void valueChanged(javax.swing.event.ListSelectionEvent evt) + { jList2ValueChanged(evt); } }); jScrollPane1.setViewportView(jList2); jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jScrollPane2.addMouseListener(new java.awt.event.MouseAdapter() { - public void mouseClicked(java.awt.event.MouseEvent evt) { + jScrollPane2.addMouseListener(new java.awt.event.MouseAdapter() + { + public void mouseClicked(java.awt.event.MouseEvent evt) + { jScrollPane2MouseClicked(evt); } }); - jList1.setModel(new javax.swing.AbstractListModel() { - String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; - public int getSize() { return strings.length; } - public Object getElementAt(int i) { return strings[i]; } + jList1.setModel(new javax.swing.AbstractListModel() + { + String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; + + public int getSize() + { + return strings.length; + } + + public Object getElementAt(int i) + { + return strings[i]; + } }); - jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() { - public void valueChanged(javax.swing.event.ListSelectionEvent evt) { + jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() + { + public void valueChanged(javax.swing.event.ListSelectionEvent evt) + { jList1ValueChanged(evt); } }); jScrollPane2.setViewportView(jList1); jButton1.setText("Run All"); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton1ActionPerformed(evt); } }); @@ -98,72 +130,82 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(23, 23, 23) - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jLabel2) - .addGap(32, 32, 32)) - .addGroup(layout.createSequentialGroup() - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1) - .addGap(82, 82, 82)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(23, 23, 23) + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel2) + .addGap(32, 32, 32)) + .addGroup(layout.createSequentialGroup() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1) + .addGap(82, 82, 82)) ); layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel1) - .addComponent(jLabel2)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE) - .addComponent(jScrollPane1)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1) - .addGap(0, 0, 0)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1) + .addComponent(jLabel2)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE) + .addComponent(jScrollPane1)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1) + .addGap(0, 0, 0)) ); pack(); }// //GEN-END:initComponents - private void jScrollPane2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPane2MouseClicked + private void jScrollPane2MouseClicked(java.awt.event.MouseEvent evt) + {//GEN-FIRST:event_jScrollPane2MouseClicked // TODO add your handling code here: }//GEN-LAST:event_jScrollPane2MouseClicked - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: - new Thread(){ - public void run(){ - RunAllSorts(); + new Thread() + { + public void run() + { + runAllSorts(); } }.start(); dispose(); }//GEN-LAST:event_jButton1ActionPerformed - private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged + private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) + {//GEN-FIRST:event_jList1ValueChanged // TODO add your handling code here: final int tmp = evt.getFirstIndex(); - new Thread(){ - public void run(){ - ReportDistributiveSort(tmp); + new Thread() + { + public void run() + { + reportDistributiveSort(tmp); } }.start(); dispose(); }//GEN-LAST:event_jList1ValueChanged - private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList2ValueChanged + private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) + {//GEN-FIRST:event_jList2ValueChanged // TODO add your handling code here: final int tmp = evt.getFirstIndex(); - new Thread(){ - public void run(){ - ReportComparativeSort(tmp); + new Thread() + { + public void run() + { + reportComparativeSort(tmp); } }.start(); dispose(); diff --git a/src/array/visualizer/UtilFrame.java b/src/array/visualizer/UtilFrame.java index 641f8408..5a86d323 100644 --- a/src/array/visualizer/UtilFrame.java +++ b/src/array/visualizer/UtilFrame.java @@ -5,37 +5,41 @@ package array.visualizer; import static array.visualizer.ArrayVisualizer.*; + import java.awt.Toolkit; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.JFrame; import javax.swing.JOptionPane; /** - * * @author S630690 */ -public class UtilFrame extends javax.swing.JFrame { +public class UtilFrame extends javax.swing.JFrame +{ JFrame f; AAFrame v; + /** * Creates new form UtilFrame */ - public UtilFrame(JFrame f) { + public UtilFrame(JFrame f) + { this.f = f; setUndecorated(true); initComponents(); - setLocation(f.getX()+f.getWidth(), f.getY()+29); + setLocation(f.getX() + f.getWidth(), f.getY() + 29); setAlwaysOnTop(true); setVisible(true); } - - public void reposition(){ + + public void reposition() + { toFront(); - setLocation(Math.min((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()-getWidth(), f.getX()+f.getWidth()), f.getY()+29); - if(v!= null && v.isVisible()) + setLocation(Math.min((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() - getWidth(), f.getX() + f.getWidth()), f.getY() + 29); + if (v != null && v.isVisible()) + { v.reposition(); + } } /** @@ -45,7 +49,8 @@ public void reposition(){ */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents - private void initComponents() { + private void initComponents() + { jCheckBox1 = new javax.swing.JCheckBox(); jButton1 = new javax.swing.JButton(); @@ -59,38 +64,48 @@ private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); jButton1.setText("Change Sort"); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton1ActionPerformed(evt); } }); jButton2.setText("Change View"); - jButton2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton2ActionPerformed(evt); } }); jButton3.setText("Change Spd"); - jButton3.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton3.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton3ActionPerformed(evt); } }); jCheckBox2.setSelected(true); jCheckBox2.setText("Shuffle Anim"); - jCheckBox2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jCheckBox2.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jCheckBox2ActionPerformed(evt); } }); jCheckBox3.setSelected(true); jCheckBox3.setText("Linked Dots"); - jCheckBox3.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jCheckBox3.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jCheckBox3ActionPerformed(evt); } }); @@ -98,73 +113,87 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jCheckBox3) - .addComponent(jCheckBox2) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) - .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) - .addGap(0, 8, Short.MAX_VALUE)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jCheckBox3) + .addComponent(jCheckBox2) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) + .addGap(0, 8, Short.MAX_VALUE)) ); layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jButton1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton2) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton3) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jCheckBox2) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jCheckBox3) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jButton1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton3) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jCheckBox2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jCheckBox3) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: //CHANGE SORT - if(v != null && v.isVisible()){ - boolean tmp = v instanceof SortPrompt; - v.dispose(); - if(tmp) - return; + if (v != null && v.isVisible()) + { + boolean tmp = v instanceof SortPrompt; + v.dispose(); + if (tmp) + { + return; + } } v = new SortPrompt(f); }//GEN-LAST:event_jButton1ActionPerformed - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: //CHANGE VIEW - if(v != null && v.isVisible()){ - boolean tmp = v instanceof ViewPrompt; - v.dispose(); - if(tmp) - return; + if (v != null && v.isVisible()) + { + boolean tmp = v instanceof ViewPrompt; + v.dispose(); + if (tmp) + { + return; + } } v = new ViewPrompt(f); }//GEN-LAST:event_jButton2ActionPerformed - private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed + private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: - try{ - SLEEPRATIO = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Speed Multiplier", SLEEPRATIO)); - }catch(Exception e) {} + try + { + SLEEPRATIO = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Speed Multiplier", SLEEPRATIO)); + } catch (Exception e) + { + } }//GEN-LAST:event_jButton3ActionPerformed - private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed + private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jCheckBox2ActionPerformed SHUFFLEANIM = jCheckBox2.isSelected(); }//GEN-LAST:event_jCheckBox2ActionPerformed - private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox3ActionPerformed + private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jCheckBox3ActionPerformed LINKEDPIXELDRAW = jCheckBox3.isSelected(); }//GEN-LAST:event_jCheckBox3ActionPerformed diff --git a/src/array/visualizer/ViewPrompt.java b/src/array/visualizer/ViewPrompt.java index 80d42617..063ad3bb 100644 --- a/src/array/visualizer/ViewPrompt.java +++ b/src/array/visualizer/ViewPrompt.java @@ -5,29 +5,33 @@ package array.visualizer; import static array.visualizer.ArrayVisualizer.*; + import javax.swing.JFrame; /** - * * @author S630690 */ -public class ViewPrompt extends javax.swing.JFrame implements AAFrame { +public class ViewPrompt extends javax.swing.JFrame implements AAFrame +{ JFrame f; + /** * Creates new form ViewPrompt */ - public ViewPrompt(JFrame f) { + public ViewPrompt(JFrame f) + { this.f = f; setAlwaysOnTop(true); setUndecorated(true); initComponents(); - setLocation(f.getX()+(f.getWidth()-getWidth())/2, f.getY()+(f.getHeight()-getHeight())/2); + setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); setVisible(true); } - - public void reposition(){ - setLocation(f.getX()+(f.getWidth()-getWidth())/2, f.getY()+(f.getHeight()-getHeight())/2); + + public void reposition() + { + setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); } /** @@ -37,7 +41,8 @@ public void reposition(){ */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents - private void initComponents() { + private void initComponents() + { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); @@ -61,43 +66,55 @@ private void initComponents() { jButton1.setText("Classic"); jButton1.setMaximumSize(new java.awt.Dimension(87, 23)); jButton1.setMinimumSize(new java.awt.Dimension(87, 23)); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton1ActionPerformed(evt); } }); jButton2.setText("Color Circle"); - jButton2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton2ActionPerformed(evt); } }); jButton3.setText("Spiral"); - jButton3.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton3.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton3ActionPerformed(evt); } }); jButton4.setText("Color Rect"); - jButton4.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton4.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton4ActionPerformed(evt); } }); jButton5.setText("Dots"); - jButton5.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton5.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton5ActionPerformed(evt); } }); jButton6.setText("Spiral Dots"); - jButton6.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton6.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton6ActionPerformed(evt); } }); @@ -105,57 +122,73 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setText("Select View Type"); jButton7.setText("D. Circle"); - jButton7.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton7.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton7ActionPerformed(evt); } }); jButton8.setText("D. C. Dots"); - jButton8.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton8.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton8ActionPerformed(evt); } }); jButton9.setText("TriMesh"); - jButton9.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton9.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton9ActionPerformed(evt); } }); jButton10.setText("VW Tris"); - jButton10.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton10.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton10ActionPerformed(evt); } }); jButton11.setText("V. Pyr"); - jButton11.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton11.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton11ActionPerformed(evt); } }); jButton12.setText("H. Pyr"); - jButton12.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton12.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton12ActionPerformed(evt); } }); jButton13.setText("Hoops"); - jButton13.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton13.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton13ActionPerformed(evt); } }); jButton14.setText("D. Hoops"); - jButton14.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton14.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton14ActionPerformed(evt); } }); @@ -163,75 +196,76 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(18, 18, 18) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) - .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(0, 0, Short.MAX_VALUE) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE) - .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) - .addComponent(jButton14, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - .addContainerGap()) - .addGroup(layout.createSequentialGroup() - .addGap(65, 65, 65) - .addComponent(jLabel1) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(0, 0, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE) + .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) + .addComponent(jButton14, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addContainerGap()) + .addGroup(layout.createSequentialGroup() + .addGap(65, 65, 65) + .addComponent(jLabel1) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(7, 7, 7) - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jButton4)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton2) - .addComponent(jButton5)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jButton6) - .addComponent(jButton3)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton7) - .addComponent(jButton8)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton9) - .addComponent(jButton10)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton11) - .addComponent(jButton12)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton13) - .addComponent(jButton14)) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(7, 7, 7) + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jButton4)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton2) + .addComponent(jButton5)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jButton6) + .addComponent(jButton3)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton7) + .addComponent(jButton8)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton9) + .addComponent(jButton10)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton11) + .addComponent(jButton12)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton13) + .addComponent(jButton14)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents - private void setAllFieldsFalse(){ + private void setAllFieldsFalse() + { CIRCLEDRAW = false; COLORONLY = false; PIXELDRAW = false; @@ -240,14 +274,16 @@ private void setAllFieldsFalse(){ PYRAMIDDRAW = false; DRAWFLIPPED = false; } - - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); dispose(); }//GEN-LAST:event_jButton1ActionPerformed - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; @@ -255,28 +291,32 @@ private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS dispose(); }//GEN-LAST:event_jButton2ActionPerformed - private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed + private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; dispose(); }//GEN-LAST:event_jButton3ActionPerformed - private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed + private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton4ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); COLORONLY = true; dispose(); }//GEN-LAST:event_jButton4ActionPerformed - private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed + private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton5ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); PIXELDRAW = true; dispose(); }//GEN-LAST:event_jButton5ActionPerformed - private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed + private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton6ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; @@ -284,7 +324,8 @@ private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS dispose(); }//GEN-LAST:event_jButton6ActionPerformed - private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed + private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton7ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; @@ -292,7 +333,8 @@ private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS dispose(); }//GEN-LAST:event_jButton7ActionPerformed - private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed + private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton8ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; @@ -301,33 +343,38 @@ private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS dispose(); }//GEN-LAST:event_jButton8ActionPerformed - private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed + private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton9ActionPerformed setAllFieldsFalse(); MESHDRAW = true; COLORONLY = true; dispose(); }//GEN-LAST:event_jButton9ActionPerformed - private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed + private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton10ActionPerformed setAllFieldsFalse(); MESHDRAW = true; dispose(); }//GEN-LAST:event_jButton10ActionPerformed - private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed + private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton11ActionPerformed setAllFieldsFalse(); PYRAMIDDRAW = true; dispose(); }//GEN-LAST:event_jButton11ActionPerformed - private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed + private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton12ActionPerformed setAllFieldsFalse(); PYRAMIDDRAW = true; DRAWFLIPPED = true; dispose(); }//GEN-LAST:event_jButton12ActionPerformed - private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed + private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton13ActionPerformed setAllFieldsFalse(); PYRAMIDDRAW = true; CIRCLEDRAW = true; @@ -336,7 +383,8 @@ private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR dispose(); }//GEN-LAST:event_jButton13ActionPerformed - private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed + private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton14ActionPerformed setAllFieldsFalse(); PYRAMIDDRAW = true; CIRCLEDRAW = true; diff --git a/src/array/visualizer/sort/BitonicSort.java b/src/array/visualizer/sort/BitonicSort.java index 578826b3..a59b9c97 100644 --- a/src/array/visualizer/sort/BitonicSort.java +++ b/src/array/visualizer/sort/BitonicSort.java @@ -7,27 +7,32 @@ import array.visualizer.ArrayController; /** - * * @author S630690 */ public class BitonicSort implements Sort { - public static void bitonicSort(final ArrayController ac){ + private static void bitonicSort(final ArrayController ac) + { bitonicMerge(ac, 0, ac.length, true); } - - public static void bitonicMerge(final ArrayController ac, int start, int end, boolean dir){ - int mid = (start+end)/2; - if(start==mid) + + private static void bitonicMerge(final ArrayController ac, int start, int end, boolean dir) + { + int mid = (start + end) / 2; + if (start == mid) + { return; + } bitonicMerge(ac, start, mid, true); bitonicMerge(ac, mid, end, false); - + int low = start; int high = end; - - if(dir) - for(int i = 0; i < end-start; i++); + + if (dir) + { + for (int i = 0; i < end - start; i++) ; + } } @Override diff --git a/src/array/visualizer/sort/BogoSort.java b/src/array/visualizer/sort/BogoSort.java index 96145292..da0272ef 100644 --- a/src/array/visualizer/sort/BogoSort.java +++ b/src/array/visualizer/sort/BogoSort.java @@ -5,32 +5,36 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Swaps.*; -public class BogoSort implements Sort { - public static boolean bogoIsSorted(final ArrayController ac){ - for(int i = 1; i < ac.length; i++){ +public class BogoSort implements Sort +{ + private static boolean bogoIsSorted(final ArrayController ac) + { + for (int i = 1; i < ac.length; i++) + { ac.comps++; ac.aa++; ac.marked.set(1, i); - ac.marked.set(2, i-1); + ac.marked.set(2, i - 1); sleep(1); - if(ac.array[i] 0; i--){ - for(int j = 0; j < i; j++){ +public class BubbleSort implements Sort +{ + private static void bubbleSort(final ArrayController ac) + { + for (int i = ac.length - 1; i > 0; i--) + { + for (int j = 0; j < i; j++) + { sleep(0.005); - if(ac.array[j]>ac.array[j+1]){ + if (ac.array[j] > ac.array[j + 1]) + { ac.comps++; - swap(ac, j, j+1, 0.01); - }else{ - ac.marked.set(1, j+1); + swap(ac, j, j + 1, 0.01); + } else + { + ac.marked.set(1, j + 1); ac.marked.set(2, -5); } } diff --git a/src/array/visualizer/sort/CocktailShaker.java b/src/array/visualizer/sort/CocktailShaker.java index ac311d92..c64874e6 100644 --- a/src/array/visualizer/sort/CocktailShaker.java +++ b/src/array/visualizer/sort/CocktailShaker.java @@ -9,22 +9,30 @@ import static array.visualizer.utils.Swaps.*; /** - * * @author S630690 */ -public class CocktailShaker implements Sort { - public static void cocktailShakerSort(final ArrayController ac){ +public class CocktailShaker implements Sort +{ + private static void cocktailShakerSort(final ArrayController ac) + { int i = 0; - while(i< ac.length/2){ - for(int j = i; j < ac.length-i-1; j++){ + while (i < ac.length / 2) + { + for (int j = i; j < ac.length - i - 1; j++) + { ac.comps++; - if(ac.array[j]>ac.array[j+1]) - swap(ac, j, j+1, 0.022); + if (ac.array[j] > ac.array[j + 1]) + { + swap(ac, j, j + 1, 0.022); + } } - for(int j = ac.length-i-1; j > i; j--){ + for (int j = ac.length - i - 1; j > i; j--) + { ac.comps++; - if(ac.array[j]ac.array[biggest]) + if (ac.array[i] > ac.array[biggest]) + { biggest = i; - if(ac.array[i]0&&ac.array[pos]<=ac.array[pos-1]){ - ac.comps+=2; - swap(ac, pos, pos-1, 0.02); + while (pos > 0 && ac.array[pos] <= ac.array[pos - 1]) + { + ac.comps += 2; + swap(ac, pos, pos - 1, 0.02); pos--; } } } - - public static void insertionSort(final ArrayController ac, int start, int end, double slpamt) { + + public static void insertionSort(final ArrayController ac, int start, int end, double slpamt) + { int pos; - for(int i = start; i < end; i++){ + for (int i = start; i < end; i++) + { pos = i; ac.marked.set(1, i); ac.marked.set(2, -5); - while(pos>start&&ac.array[pos]<=ac.array[pos-1]){ - ac.comps+=2; - swap(ac, pos, pos-1); - sleep(slpamt); - pos--; + while (pos > start && ac.array[pos] <= ac.array[pos - 1]) + { + ac.comps += 2; + swap(ac, pos, pos - 1); + sleep(slpamt); + pos--; } } } diff --git a/src/array/visualizer/sort/MaxHeapSort.java b/src/array/visualizer/sort/MaxHeapSort.java index 899d4510..6eedf832 100644 --- a/src/array/visualizer/sort/MaxHeapSort.java +++ b/src/array/visualizer/sort/MaxHeapSort.java @@ -4,62 +4,80 @@ import static array.visualizer.utils.Swaps.*; -public class MaxHeapSort implements Sort { - - static int SLP = 1; - - static void maxheapifyrec(final ArrayController ac, int pos, boolean max){ - if(pos>= ac.length) +public class MaxHeapSort implements Sort +{ + + private static int SLP = 1; + + private static void maxHeapifyRec(final ArrayController ac, int pos, boolean max) + { + if (pos >= ac.length) + { return; - - int child1 = pos*2+1; - int child2 = pos*2+2; - - maxheapifyrec(ac, child1, max); - maxheapifyrec(ac, child2, max); - - if(child2>= ac.length){ - if(child1>= ac.length) + } + + int child1 = pos * 2 + 1; + int child2 = pos * 2 + 2; + + maxHeapifyRec(ac, child1, max); + maxHeapifyRec(ac, child2, max); + + if (child2 >= ac.length) + { + if (child1 >= ac.length) + { return; //Done, no children + } ac.comps++; - if(ac.array[child1]>ac.array[pos]) + if (ac.array[child1] > ac.array[pos]) + { swap(ac, pos, child1, SLP); + } return; } - + //Find largest child int lrg = child1; ac.comps++; - if(ac.array[child2]>ac.array[child1]) + if (ac.array[child2] > ac.array[child1]) + { lrg = child2; - + } + //Swap with largest child ac.comps++; - if(ac.array[lrg]>ac.array[pos]){ + if (ac.array[lrg] > ac.array[pos]) + { swap(ac, pos, lrg, SLP); - percdwn(ac, lrg, true, ac.length); - return; + percDwn(ac, lrg, true, ac.length); } } - - static void percdwn(final ArrayController ac, int pos, boolean max, int len){ - int child1 = pos*2+1; - int child2 = pos*2+2; - - if(child2 >= len){ - if(child1 >= len) //Done + + private static void percDwn(final ArrayController ac, int pos, boolean max, int len) + { + int child1 = pos * 2 + 1; + int child2 = pos * 2 + 2; + + if (child2 >= len) + { + if (child1 >= len) //Done + { return; - else{ + } else + { //Single Child ac.comps++; - if((max && (ac.array[child1]>ac.array[pos])) || (!max && (ac.array[child1] ac.array[pos])) || (!max && (ac.array[child1] < ac.array[pos]))) + { swap(ac, pos, child1, SLP); + } return; } } ac.comps++; - if(ac.array[child1]>ac.array[child2]){ + if (ac.array[child1] > ac.array[child2]) + { //Ensure child1 is the smallest for easy programming int tmp = child1; child1 = child2; @@ -67,21 +85,24 @@ static void percdwn(final ArrayController ac, int pos, boolean max, int len){ } ac.comps++; - if(max && (ac.array[child2]>ac.array[pos])){ + if (max && (ac.array[child2] > ac.array[pos])) + { swap(ac, pos, child2, SLP); - percdwn(ac, child2, max, len); - } - else if (!max && (ac.array[child1] 0; i--){ + + private static void maxHeapSort(final ArrayController ac) + { + maxHeapifyRec(ac, 0, true); + for (int i = ac.length - 1; i > 0; i--) + { swap(ac, 0, i, SLP); - percdwn(ac, 0, true, i); + percDwn(ac, 0, true, i); } } @@ -94,6 +115,6 @@ public String name() @Override public void sort(ArrayController ac) { - maxheapsort(ac); + maxHeapSort(ac); } } diff --git a/src/array/visualizer/sort/MergeSort.java b/src/array/visualizer/sort/MergeSort.java index 794a5488..3712c0b3 100644 --- a/src/array/visualizer/sort/MergeSort.java +++ b/src/array/visualizer/sort/MergeSort.java @@ -5,62 +5,56 @@ package array.visualizer.sort; import array.visualizer.ArrayController; -import array.visualizer.ArrayVisualizer; import static array.visualizer.utils.Swaps.*; -import java.util.logging.Level; -import java.util.logging.Logger; /** - * * @author S630690 */ -public class MergeSort implements Sort { - static void merge(final ArrayController ac, int min, int max, int mid){ - try { - //radixLSDsortnd(2, min, max); - - - int i=min; - while(i<=mid){ - if(ac.array[i]>ac.array[mid+1]){ - ac.comps++; - swap(ac, i, mid+1, 1.5); - push(ac, mid+1, max); - } - i++; - } - - } catch (Exception ex) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); +public class MergeSort implements Sort +{ + private static void merge(final ArrayController ac, int min, int max, int mid) + { + //radixLSDSortNd(2, min, max); + for (int i = min; i <= mid; i++) + { + if (ac.array[i] > ac.array[mid + 1]) + { + ac.comps++; + swap(ac, i, mid + 1, 1.5); + push(ac, mid + 1, max); + } } - } - - static void push(final ArrayController ac, int s, int e){ - - for(int i=s;iac.array[i+1]){ + } + + private static void push(final ArrayController ac, int s, int e) + { + for (int i = s; i < e; i++) + { + if (ac.array[i] > ac.array[i + 1]) + { ac.comps++; - swap(ac, i, i+1, 0.0175); - } - } - - - } - - public static void mergeSort(final ArrayController ac, int min, int max){ - if(max-min==0){//only one element. - //no swap - } - else if(max-min==1){//only two elements and swaps them - if(ac.array[min]>ac.array[max]) - swap(ac, min, max); - } - else{ - int mid=((int) Math.floor((min+max)/2));//The midpoint + swap(ac, i, i + 1, 0.0175); + } + } + } + private static void mergeSort(final ArrayController ac, int min, int max) + { + if (max - min == 0) + {//only one element. + //no swap + } else if (max - min == 1) + {//only two elements and swaps them + if (ac.array[min] > ac.array[max]) + { + swap(ac, min, max); + } + } else + { + int mid = (min + max) / 2;//The midpoint mergeSort(ac, min, mid);//sort the left side - mergeSort(ac, mid+1, max);//sort the right side + mergeSort(ac, mid + 1, max);//sort the right side merge(ac, min, max, mid);//combines them } } diff --git a/src/array/visualizer/sort/MergeSortOOP.java b/src/array/visualizer/sort/MergeSortOOP.java index 6c814c8a..fa4cd0c8 100644 --- a/src/array/visualizer/sort/MergeSortOOP.java +++ b/src/array/visualizer/sort/MergeSortOOP.java @@ -9,64 +9,75 @@ import static array.visualizer.ArrayVisualizer.*; /** - * * @author S630690 */ -public class MergeSortOOP implements Sort { - public static void mergeSortOP(final ArrayController ac) { +public class MergeSortOOP implements Sort +{ + private static void mergeSortOP(final ArrayController ac) + { int start = 0; int end = ac.length; - int mid = (end+start)/2; + int mid = (end + start) / 2; mergeOP(ac, start, mid, end); } - public static void mergeOP(final ArrayController ac, int start, int mid, int end){ - if(start==mid) + private static void mergeOP(final ArrayController ac, int start, int mid, int end) + { + if (start == mid) + { return; - mergeOP(ac, start, (mid+start)/2, mid); - mergeOP(ac, mid, (mid+end)/2, end); - - int[] tmp = new int[end-start]; - + } + mergeOP(ac, start, (mid + start) / 2, mid); + mergeOP(ac, mid, (mid + end) / 2, end); + + int[] tmp = new int[end - start]; + int low = start; int high = mid; - for(int nxt = 0; nxt < tmp.length; nxt++){ - if(low >= mid && high >= end) + for (int nxt = 0; nxt < tmp.length; nxt++) + { + if (low >= mid && high >= end) + { break; - if(low < mid && high >= end){ - tmp[nxt]=ac.array[low]; - low++; - ac.comps+=2; } - else if(low >= mid && high < end){ - tmp[nxt]=ac.array[high]; + if (low < mid && high >= end) + { + tmp[nxt] = ac.array[low]; + low++; + ac.comps += 2; + } else if (low >= mid && high < end) + { + tmp[nxt] = ac.array[high]; high++; - ac.comps+=2; - } - else if(ac.array[low]=array.length/10) - sleep(0.65); + sleep(0.65); } //System.arraycopy(tmp, 0, array, start, tmp.length); ac.marked.set(2, -5); - for(int i = 0; i < tmp.length; i++){ - ac.array[start+i]=tmp[i]; + for (int i = 0; i < tmp.length; i++) + { + ac.array[start + i] = tmp[i]; ac.aa++; - ac.marked.set(1, start+i); - if(end-start>= ac.length/100) + ac.marked.set(1, start + i); + if (end - start >= ac.length / 100) + { sleep(0.5); + } } } diff --git a/src/array/visualizer/sort/MinHeapSort.java b/src/array/visualizer/sort/MinHeapSort.java index 522c2710..88836bef 100644 --- a/src/array/visualizer/sort/MinHeapSort.java +++ b/src/array/visualizer/sort/MinHeapSort.java @@ -2,11 +2,10 @@ import array.visualizer.ArrayController; -import static array.visualizer.utils.Swaps.swap; +public class MinHeapSort implements Sort +{ -public class MinHeapSort implements Sort { - - static void minheapsort(ArrayController ac) + private static void minHeapSort(ArrayController ac) { } @@ -19,6 +18,6 @@ public String name() @Override public void sort(ArrayController ac) { - minheapsort(ac); + minHeapSort(ac); } } diff --git a/src/array/visualizer/sort/QuickSort.java b/src/array/visualizer/sort/QuickSort.java index b52ce4f1..5cc3d2fc 100644 --- a/src/array/visualizer/sort/QuickSort.java +++ b/src/array/visualizer/sort/QuickSort.java @@ -10,48 +10,55 @@ import static array.visualizer.utils.Swaps.*; /** - * * @author S630690 */ -public class QuickSort implements Sort { - public static void quickSort(final ArrayController ac, int p, int r) +public class QuickSort implements Sort +{ + private static void quickSort(final ArrayController ac, int p, int r) { - if(pp && ac.array[j] > x){ + while (j > p && ac.array[j] > x) + { j--; ac.marked.set(2, j); sleep(0.45); - ac.comps+=2; + ac.comps += 2; } if (i < j) + { swap(ac, i, j); - else + } else + { return j; + } } } @@ -64,6 +71,6 @@ public String name() @Override public void sort(ArrayController ac) { - quickSort(ac, 0, ac.length-1); + quickSort(ac, 0, ac.length - 1); } } diff --git a/src/array/visualizer/sort/RadixLSD.java b/src/array/visualizer/sort/RadixLSD.java index c554b8b2..1598840b 100644 --- a/src/array/visualizer/sort/RadixLSD.java +++ b/src/array/visualizer/sort/RadixLSD.java @@ -9,13 +9,14 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Analysis.*; import static array.visualizer.utils.Transcriptions.*; + import java.util.ArrayList; /** - * * @author S630690 */ -public class RadixLSD implements Sort { +public class RadixLSD implements Sort +{ private final int radix; @@ -24,53 +25,46 @@ public RadixLSD(int radix) this.radix = radix; } - public static void radixLSDsort_(final ArrayController ac, int radix) { - clearmarked(); - int highestpower = analyze(ac, radix); + private static void radixLSDSort(final ArrayController ac, int radix) + { + int highestPower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; - for(int i = 0; i < radix; i++) - registers[i] = new ArrayList(); - for(int p = 0; p <= highestpower; p++){ - for(int i = 0; i < ac.length; i++){ - ac.marked.set(1, i); - sleep(1); - registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); - } - //transcribe(registers, array); - transcribe(registers, ac); + for (int i = 0; i < radix; i++) + { + registers[i] = new ArrayList<>(); } - } - - public static void radixLSDsort(final ArrayController ac, int radix){ - clearmarked(); - int highestpower = analyze(ac, radix); - ArrayList[] registers = new ArrayList[radix]; - for(int i = 0; i < radix; i++) - registers[i] = new ArrayList(); - for(int p = 0; p <= highestpower; p++){ - for(int i = 0; i < ac.length; i++){ + for (int p = 0; p <= highestPower; p++) + { + for (int i = 0; i < ac.length; i++) + { ac.aa++; ac.marked.set(1, i); - if(i%2==0) + if (i % 2 == 0) + { sleep(1); + } registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); } fancyTranscribe(ac, registers); } } - - public static void radixLSDsortnd(final ArrayController ac, int radix, int min, int max) { - clearmarked(); - int highestpower = analyze(ac, radix); + + public static void radixLSDSortNd(final ArrayController ac, int radix, int min, int max) + { + int highestPower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; - for(int i = 0; i < radix; i++) - registers[i] = new ArrayList(); - for(int p = 0; p <= highestpower; p++){ - for(int i = min; i < max; i++){ + for (int i = 0; i < radix; i++) + { + registers[i] = new ArrayList<>(); + } + for (int p = 0; p <= highestPower; p++) + { + for (int i = min; i < max; i++) + { registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); } //transcribe(registers, array); - transcribend(ac, registers, min); + transcribeNd(ac, registers, min); } } @@ -83,6 +77,6 @@ public String name() @Override public void sort(ArrayController ac) { - radixLSDsort(ac, radix); + radixLSDSort(ac, radix); } } diff --git a/src/array/visualizer/sort/RadixLSDInPlace.java b/src/array/visualizer/sort/RadixLSDInPlace.java index 0ccf72e9..e5f4318f 100644 --- a/src/array/visualizer/sort/RadixLSDInPlace.java +++ b/src/array/visualizer/sort/RadixLSDInPlace.java @@ -6,7 +6,8 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Swaps.*; -public class RadixLSDInPlace implements Sort { +public class RadixLSDInPlace implements Sort +{ private final int radix; public RadixLSDInPlace(int radix) @@ -14,29 +15,39 @@ public RadixLSDInPlace(int radix) this.radix = radix; } - public static void inPlaceRadixLSDSort(final ArrayController ac, int radix){ - int pos = 0; - int[] vregs = new int[radix-1]; - int maxpower = analyze(ac, radix); + private static void inPlaceRadixLSDSort(final ArrayController ac, int radix) + { + int[] vregs = new int[radix - 1]; + int maxPower = analyze(ac, radix); double smul = Math.sqrt(radix); - for(int p = 0; p <= maxpower; p++){ - for(int i = 0; i < vregs.length; i++) - vregs[i]= ac.length-1; - pos = 0; - for(int i = 0; i < ac.length; i++){ + for (int p = 0; p <= maxPower; p++) + { + int pos = 0; + for (int i = 0; i < vregs.length; i++) + { + vregs[i] = ac.length - 1; + } + for (int i = 0; i < ac.length; i++) + { int digit = getDigit(ac.array[pos], p, radix); - if(digit==0) { + if (digit == 0) + { pos++; ac.marked.set(0, pos); - } else { - for(int j = 0; j 0; j--) - vregs[j-1]--; + } else + { + for (int j = 0; j < vregs.length; j++) + { + ac.marked.set(j + 1, vregs[j]); + } + swapUpToNM(ac, pos, vregs[digit - 1], 0.0011 * smul); + for (int j = digit - 1; j > 0; j--) + { + vregs[j - 1]--; + } } } - + } } diff --git a/src/array/visualizer/sort/RadixMSD.java b/src/array/visualizer/sort/RadixMSD.java index 0542569f..7f2b43f2 100644 --- a/src/array/visualizer/sort/RadixMSD.java +++ b/src/array/visualizer/sort/RadixMSD.java @@ -9,13 +9,14 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Transcriptions.*; import static array.visualizer.utils.Analysis.*; + import java.util.ArrayList; /** - * * @author S630690 */ -public class RadixMSD implements Sort { +public class RadixMSD implements Sort +{ private final int radix; public RadixMSD(int radix) @@ -23,34 +24,39 @@ public RadixMSD(int radix) this.radix = radix; } - public static void radixMSDSort(final ArrayController ac, int radix) { - clearmarked(); - int highestpower = analyze(ac, radix); - int[] tmp = new int[ac.length]; - System.arraycopy(ac.array, 0, tmp, 0, ac.length); - radixMSDRec(ac, 0, ac.length, radix, highestpower); + private static void radixMSDSort(final ArrayController ac, int radix) + { + int highestPower = analyze(ac, radix); + radixMSDRec(ac, 0, ac.length, radix, highestPower); } - - public static void radixMSDRec(final ArrayController ac, int min, int max, int radix, int pow){ - if(min >= max || pow < 0) + + private static void radixMSDRec(final ArrayController ac, int min, int max, int radix, int pow) + { + if (min >= max || pow < 0) + { return; + } ac.marked.set(2, max); ac.marked.set(3, min); ArrayList[] registers = new ArrayList[radix]; - for(int i = 0; i < radix; i++) - registers[i] = new ArrayList(); - for(int i = min; i < max; i++){ + for (int i = 0; i < radix; i++) + { + registers[i] = new ArrayList<>(); + } + for (int i = min; i < max; i++) + { ac.marked.set(1, i); registers[getDigit(ac.array[i], pow, radix)].add(ac.array[i]); ac.aa++; } - transcribermsd(ac, registers, min); - + transcribeRmsd(ac, registers, min); + int sum = 0; - for(int i = 0; i < registers.length; i++){ - radixMSDRec(ac, sum+min, sum+min+registers[i].size(), radix, pow-1); - sum+=registers[i].size(); - registers[i].clear(); + for (ArrayList register : registers) + { + radixMSDRec(ac, sum + min, sum + min + register.size(), radix, pow - 1); + sum += register.size(); + register.clear(); } } diff --git a/src/array/visualizer/sort/SelectionSort.java b/src/array/visualizer/sort/SelectionSort.java index 74610794..aacc2ce9 100644 --- a/src/array/visualizer/sort/SelectionSort.java +++ b/src/array/visualizer/sort/SelectionSort.java @@ -10,22 +10,26 @@ import static array.visualizer.utils.Swaps.*; /** - * * @author S630690 */ -public class SelectionSort implements Sort { - - public static void selectionSort(final ArrayController ac) { - for (int i = 0; i < ac.length - 1; i++) { - int lowestindex = i; - for (int j = i + 1; j < ac.length; j++) { - if (ac.array[j] < ac.array[lowestindex]){ - lowestindex = j; +public class SelectionSort implements Sort +{ + + private static void selectionSort(final ArrayController ac) + { + for (int i = 0; i < ac.length - 1; i++) + { + int lowestIndex = i; + for (int j = i + 1; j < ac.length; j++) + { + if (ac.array[j] < ac.array[lowestIndex]) + { + lowestIndex = j; } sleep(0.01); ac.comps++; } - swap(ac, i, lowestindex); + swap(ac, i, lowestIndex); } } diff --git a/src/array/visualizer/sort/ShatterSorts.java b/src/array/visualizer/sort/ShatterSorts.java index 80962613..e0150216 100644 --- a/src/array/visualizer/sort/ShatterSorts.java +++ b/src/array/visualizer/sort/ShatterSorts.java @@ -8,13 +8,14 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Transcriptions.*; + import java.util.ArrayList; /** - * * @author S630690 */ -public class ShatterSorts implements Sort { +public class ShatterSorts implements Sort +{ private final int base; public ShatterSorts(int base) @@ -22,45 +23,60 @@ public ShatterSorts(int base) this.base = base; } - public static void shatterPartition(final ArrayController ac, int num) { - int shatters = (int)Math.ceil(ac.length/(double)num); + private static void shatterPartition(final ArrayController ac, int num) + { + int shatters = (int) Math.ceil(ac.length / (double) num); ArrayList[] registers = new ArrayList[shatters]; - for(int i = 0; i < shatters; i++) - registers[i] = new ArrayList(); - for(int i = 0; i < ac.length; i++){ - registers[ac.array[i]/num].add(ac.array[i]); + for (int i = 0; i < shatters; i++) + { + registers[i] = new ArrayList<>(); + } + for (int i = 0; i < ac.length; i++) + { + registers[ac.array[i] / num].add(ac.array[i]); ac.aa++; } transcribe(registers, ac); } - - public static void shatterSort(final ArrayController ac, int num) { - int shatters = (int)Math.ceil(ac.length/(double)num); + + private static void shatterSort(final ArrayController ac, int num) + { + int shatters = (int) Math.ceil(ac.length / (double) num); shatterPartition(ac, num); int[] tmp = new int[num]; - for(int i = 0; i < shatters; i++){ - for(int j = 0; j < num; j++){ - if(i*num+j>= ac.length) + for (int i = 0; i < shatters; i++) + { + for (int j = 0; j < num; j++) + { + if (i * num + j >= ac.length) + { tmp[j] = -1; - else - tmp[j]=ac.array[i*num+j]; + } else + { + tmp[j] = ac.array[i * num + j]; + } ac.aa++; } - for(int j = 0; j < tmp.length; j++){ - int tmpj = tmp[j]; - if(i*num+(tmpj%num)>= ac.length || tmpj == -1) + for (int tmpj : tmp) + { + if (i * num + (tmpj % num) >= ac.length || tmpj == -1) + { break; - ac.array[i*num+(tmpj%num)]=tmpj; + } + ac.array[i * num + (tmpj % num)] = tmpj; ac.aa++; - ac.marked.set(1, i*num+(tmpj%num)); + ac.marked.set(1, i * num + (tmpj % num)); sleep(sleepTime(0.02)); } } } - - public static void simpleShatterSort(final ArrayController ac, int num, int rate) { - for(int i = num; i > 1; i = i/rate) + + public static void simpleShatterSort(final ArrayController ac, int num, int rate) + { + for (int i = num; i > 1; i = i / rate) + { shatterPartition(ac, i); + } shatterPartition(ac, 1); } diff --git a/src/array/visualizer/sort/ShellSort.java b/src/array/visualizer/sort/ShellSort.java index 8c91544a..75bf6af9 100644 --- a/src/array/visualizer/sort/ShellSort.java +++ b/src/array/visualizer/sort/ShellSort.java @@ -5,34 +5,44 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Swaps.*; -public class ShellSort implements Sort { - public static void shellSort(final ArrayController ac, int gap, int divrate){ - double sleepamt = 1d; - while(gap>0){ - for(int j = 0; j <= gap-1; j++){ - for(int i = j+gap; i < ac.length; i+=gap){ +public class ShellSort implements Sort +{ + private static void shellSort(final ArrayController ac, int gap, int divRate) + { + double sleepAmt = 1d; + while (gap > 0) + { + for (int j = 0; j <= gap - 1; j++) + { + for (int i = j + gap; i < ac.length; i += gap) + { int pos = i; - int prev = pos-gap; - while(prev>=0){ - if(ac.array[pos] < ac.array[prev]){ + int prev = pos - gap; + while (prev >= 0) + { + if (ac.array[pos] < ac.array[prev]) + { ac.comps++; swap(ac, pos, prev); - sleep(sleepamt); - }else{ - ac.aa+=2; + sleep(sleepAmt); + } else + { + ac.aa += 2; break; } pos = prev; - prev = pos-gap; + prev = pos - gap; } } } - - if(gap==1) //Done + + if (gap == 1) //Done + { break; - - gap = Math.max(gap/divrate, 1); //Ensure that we do gap 1 - //sleepamt /= divrate; + } + + gap = Math.max(gap / divRate, 1); //Ensure that we do gap 1 + //sleepAmt /= divRate; } } diff --git a/src/array/visualizer/sort/Sort.java b/src/array/visualizer/sort/Sort.java index dc0ff2e5..e03c9009 100644 --- a/src/array/visualizer/sort/Sort.java +++ b/src/array/visualizer/sort/Sort.java @@ -5,5 +5,6 @@ public interface Sort { String name(); + void sort(ArrayController ac); } diff --git a/src/array/visualizer/sort/TimeSort.java b/src/array/visualizer/sort/TimeSort.java index 89820eb0..400fe979 100644 --- a/src/array/visualizer/sort/TimeSort.java +++ b/src/array/visualizer/sort/TimeSort.java @@ -10,14 +10,16 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.sort.InsertionSort.*; + import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; + /** - * * @author S630690 */ -public class TimeSort implements Sort { +public class TimeSort implements Sort +{ private final int magnitude; public TimeSort(int magnitude) @@ -25,36 +27,46 @@ public TimeSort(int magnitude) this.magnitude = magnitude; } - public static void timeSort(final ArrayController ac, int magnitude) + private static void timeSort(final ArrayController ac, int magnitude) { final int A = magnitude; next = 0; - ArrayList threads = new ArrayList(); + ArrayList threads = new ArrayList<>(); final int[] tmp = ac.array.clone(); - for(int i = 0; i < ac.length; i++){ + for (int i = 0; i < ac.length; i++) + { ac.marked.set(0, i); sleep(1); final int c = i; - threads.add(new Thread(){ - public void run() { + threads.add(new Thread() + { + public void run() + { int a = tmp[c]; - try { - sleep(a*A); - } catch (InterruptedException ex) { + try + { + sleep(a * A); + } catch (InterruptedException ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } report(ac, a); } }); } - for(Thread t : threads) + for (Thread t : threads) + { t.start(); + } sleep(ac.length * A); insertionSort(ac, 0, ac.length, 0.2d); - + } - static volatile int next = 0; - public static synchronized void report(final ArrayController ac, int a){ + + private static volatile int next = 0; + + private static synchronized void report(final ArrayController ac, int a) + { ac.marked.set(0, next); ac.array[next] = a; ac.aa++; diff --git a/src/array/visualizer/sort/WeaveMerge.java b/src/array/visualizer/sort/WeaveMerge.java index ddf69e27..6a264657 100644 --- a/src/array/visualizer/sort/WeaveMerge.java +++ b/src/array/visualizer/sort/WeaveMerge.java @@ -5,62 +5,61 @@ package array.visualizer.sort; import array.visualizer.ArrayController; -import array.visualizer.ArrayVisualizer; import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.sort.InsertionSort.*; import static array.visualizer.utils.Swaps.*; -import java.util.logging.Level; -import java.util.logging.Logger; /** - * * @author S630690 */ -public class WeaveMerge implements Sort { - static void weaveMerge(final ArrayController ac, int min, int max, int mid){ - try { - //radixLSDsortnd(2, min, max); - - int i=1; - int target = (mid-min); - while(i<=target){ - //swapUpTo(mid+(i-min), min+(i-min)*2, 0.01); - swapUpTo(ac, mid+i, min+i*2-1, 0.01); - i++; - sleep(1); - } - insertionSort(ac, min, max+1, 0.15); - //sleep(100); - - } catch (Exception ex) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); +public class WeaveMerge implements Sort +{ + private static void weaveMerge(final ArrayController ac, int min, int max, int mid) + { + //radixLSDSortNd(2, min, max); + int i = 1; + int target = (mid - min); + while (i <= target) + { + //swapUpTo(mid+(i-min), min+(i-min)*2, 0.01); + swapUpTo(ac, mid + i, min + i * 2 - 1, 0.01); + i++; + sleep(1); } + insertionSort(ac, min, max + 1, 0.15); + //sleep(100); } - - static void push(final ArrayController ac, int s, int e){ - - for(int i=s;iac.array[i+1]){ + + static void push(final ArrayController ac, int s, int e) + { + for (int i = s; i < e; i++) + { + if (ac.array[i] > ac.array[i + 1]) + { ac.comps++; - swap(ac, i, i+1); + swap(ac, i, i + 1); } - } + } } - - public static void weaveMergeSort(final ArrayController ac, int min, int max){ - if(max-min==0){//only one element. - //no swap - } - else if(max-min==1){//only two elements and swaps them - if(ac.array[min]>ac.array[max]) + + private static void weaveMergeSort(final ArrayController ac, int min, int max) + { + if (max - min == 0) + {//only one element. + //no swap + } else if (max - min == 1) + {//only two elements and swaps them + if (ac.array[min] > ac.array[max]) + { swap(ac, min, max); - } - else{ - int mid=((int) Math.floor((min+max)/2));//The midpoint + } + } else + { + int mid = (min + max) / 2;//The midpoint weaveMergeSort(ac, min, mid);//sort the left side - weaveMergeSort(ac, mid+1, max);//sort the right side + weaveMergeSort(ac, mid + 1, max);//sort the right side weaveMerge(ac, min, max, mid);//combines them } } diff --git a/src/array/visualizer/utils/Analysis.java b/src/array/visualizer/utils/Analysis.java index fc3b1544..72cf0f6f 100644 --- a/src/array/visualizer/utils/Analysis.java +++ b/src/array/visualizer/utils/Analysis.java @@ -9,37 +9,35 @@ import static array.visualizer.ArrayVisualizer.*; /** - * * @author S630690 */ -public class Analysis { - public static int analyze(final ArrayController ac, int base) { +public class Analysis +{ + public static int analyze(final ArrayController ac, int base) + { int a = 0; - for(int i = 0; i < ac.length; i++){ + for (int i = 0; i < ac.length; i++) + { ac.marked.set(1, i); ac.aa++; sleep(1); - if((int)(Math.log(ac.array[i])/Math.log(base))>a){ - a=(int)(Math.log(ac.array[i])/Math.log(base)); + if ((int) (Math.log(ac.array[i]) / Math.log(base)) > a) + { + a = (int) (Math.log(ac.array[i]) / Math.log(base)); } } return a; } - public static int analyze_sneaky(int[] array, int base) { + public static int analyzeMax(final ArrayController ac) + { int a = 0; - for(int i = 0; i < array.length; i++) - if((int)(Math.log(array[i])/Math.log(base))>a){ - a=(int)(Math.log(array[i])/Math.log(base)); + for (int i = 0; i < ac.length; i++) + { + if (ac.array[i] > a) + { + a = ac.array[i]; } - return a; - } - - public static int analyzemax(final ArrayController ac) { - int a = 0; - for(int i = 0; i < ac.length; i++){ - if(ac.array[i]>a) - a=ac.array[i]; ac.marked.set(1, i); ac.aa++; sleep(1.0); diff --git a/src/array/visualizer/utils/Searches.java b/src/array/visualizer/utils/Searches.java index e1432eac..02feaafb 100644 --- a/src/array/visualizer/utils/Searches.java +++ b/src/array/visualizer/utils/Searches.java @@ -9,34 +9,43 @@ import static array.visualizer.ArrayVisualizer.*; /** - * * @author S630690 */ -public class Searches { - public static void linearSearch(final ArrayController ac, int find) throws Exception { - for(int i = 0; i < ac.length; i++){ +public class Searches +{ + public static void linearSearch(final ArrayController ac, int find) + { + for (int i = 0; i < ac.length; i++) + { ac.aa++; ac.comps++; - if(ac.array[i]==find) + if (ac.array[i] == find) + { break; + } ac.marked.set(0, i); sleep(5); } sleep(1000); } - - public static void binarySearch(final ArrayController ac, int find) throws Exception { - int at = ac.length/2; - int change = ac.length/4; - while(ac.array[at]!=find && change > 0){ + + public static void binarySearch(final ArrayController ac, int find) + { + int at = ac.length / 2; + int change = ac.length / 4; + while (ac.array[at] != find && change > 0) + { ac.marked.set(0, ac.array[at]); - ac.comps+=2; + ac.comps += 2; ac.aa++; - Thread.sleep(1000); - if(ac.array[at] 0) - for(int i = pos; i < to; i++) - swap(ac, i, i+1, pause); - else - for(int i = pos; i > to; i--) - swap(ac, i, i-1, pause); + + public static void swapUpTo(final ArrayController ac, int pos, int to, double pause) + { + if (to - pos > 0) + { + for (int i = pos; i < to; i++) + { + swap(ac, i, i + 1, pause); + } + } else + { + for (int i = pos; i > to; i--) + { + swap(ac, i, i - 1, pause); + } + } } - - public static void swapUpToNM(final ArrayController ac, int pos, int to, double pause){ - if(to - pos > 0) - for(int i = pos; i < to; i++) - swapnm(ac, i, i+1, pause); - else - for(int i = pos; i > to; i--) - swapnm(ac, i, i-1, pause); + + public static void swapUpToNM(final ArrayController ac, int pos, int to, double pause) + { + if (to - pos > 0) + { + for (int i = pos; i < to; i++) + { + swapNm(ac, i, i + 1, pause); + } + } else + { + for (int i = pos; i > to; i--) + { + swapNm(ac, i, i - 1, pause); + } + } } - - public static void swapUp(final ArrayController ac, int pos, double pause) { - for(int i = pos; i < ac.length; i++) - swap(ac, i, i+1, pause); + + public static void swapUp(final ArrayController ac, int pos, double pause) + { + for (int i = pos; i < ac.length; i++) + { + swap(ac, i, i + 1, pause); + } } } diff --git a/src/array/visualizer/utils/Transcriptions.java b/src/array/visualizer/utils/Transcriptions.java index 834befba..edf5138d 100644 --- a/src/array/visualizer/utils/Transcriptions.java +++ b/src/array/visualizer/utils/Transcriptions.java @@ -11,101 +11,126 @@ import java.util.ArrayList; /** - * * @author S630690 */ -public class Transcriptions { - public static void transcribe(ArrayList[] registers, final ArrayController ac) { +public class Transcriptions +{ + public static void transcribe(ArrayList[] registers, final ArrayController ac) + { int total = 0; - for(int ai = 0; ai < registers.length; ai++){ - for(int i = 0; i < registers[ai].size(); i++){ + for (ArrayList register : registers) + { + for (Integer aRegister : register) + { sleep(1); - ac.array[total] = registers[ai].get(i); + ac.array[total] = aRegister; ac.marked.set(1, total); total++; ac.aa++; } - registers[ai].clear(); + register.clear(); } } - - public static void transcribenm(ArrayList[] registers, int[] array) { + + public static void transcribeNm(ArrayList[] registers, int[] array) + { int total = 0; - for(int ai = 0; ai < registers.length; ai++){ - for(int i = 0; i < registers[ai].size(); i++){ - array[total] = registers[ai].get(i); + for (ArrayList register : registers) + { + for (Integer aRegister : register) + { + array[total] = aRegister; total++; } - registers[ai].clear(); + register.clear(); } } - - public static void transcribend(final ArrayController ac, ArrayList[] registers, int min) { + + public static void transcribeNd(final ArrayController ac, ArrayList[] registers, int min) + { int total = 0; - for(int ai = 0; ai < registers.length; ai++){ - for(int i = 0; i < registers[ai].size(); i++){ - sleep((min+i)%5/4); - ac.array[total+min] = registers[ai].get(i); - ac.marked.set(1, total+min); + for (ArrayList register : registers) + { + for (int i = 0; i < register.size(); i++) + { + sleep((min + i) % 5 / 4); + ac.array[total + min] = register.get(i); + ac.marked.set(1, total + min); total++; ac.aa++; } - registers[ai].clear(); + register.clear(); } } - - public static void transcribermsd(final ArrayController ac, ArrayList[] registers, int min) { + + public static void transcribeRmsd(final ArrayController ac, ArrayList[] registers, int min) + { int total = 0; - for(ArrayList ai : registers) - total+=ai.size(); + for (ArrayList ai : registers) + { + total += ai.size(); + } int tmp = 0; - for(int ai = registers.length-1; ai >= 0; ai--){ - for(int i = registers[ai].size()-1; i >= 0; i--){ - sleep(1+2/registers[ai].size()); - ac.array[total+min-tmp-1] = registers[ai].get(i); - ac.marked.set(1, total+min-tmp-1); + for (int ai = registers.length - 1; ai >= 0; ai--) + { + for (int i = registers[ai].size() - 1; i >= 0; i--) + { + sleep(1 + 2 / registers[ai].size()); + ac.array[total + min - tmp - 1] = registers[ai].get(i); + ac.marked.set(1, total + min - tmp - 1); tmp++; ac.aa++; } } } - - public static void transcribe(ArrayList[] registers, final ArrayController ac, int start) { + + public static void transcribe(ArrayList[] registers, final ArrayController ac, int start) + { int total = start; - for(int ai = 0; ai < registers.length; ai++){ - for(int i = 0; i < registers[ai].size(); i++){ + for (ArrayList register : registers) + { + for (Integer aRegister : register) + { sleep(1); - ac.array[total] = registers[ai].get(i); + ac.array[total] = aRegister; ac.marked.set(1, total); total++; ac.aa++; } - registers[ai].clear(); + register.clear(); } } - public static void fancyTranscribe(final ArrayController ac, ArrayList[] registers) { + public static void fancyTranscribe(final ArrayController ac, ArrayList[] registers) + { int[] tmp = new int[ac.length]; - boolean[] tmpwrite = new boolean[ac.length]; + boolean[] tmpWrite = new boolean[ac.length]; int radix = registers.length; - transcribenm(registers, tmp); - for(int i = 0; i < tmp.length; i++){ - int register = i%radix; - if(register == 0) + transcribeNm(registers, tmp); + for (int i = 0; i < tmp.length; i++) + { + int register = i % radix; + if (register == 0) + { sleep(radix);//radix - int pos = (int)(((double)register*((double)tmp.length/radix))+((double)i/radix)); - if(tmpwrite[pos]==false){ - ac.array[pos]=tmp[pos]; + } + int pos = (int) (((double) register * ((double) tmp.length / radix)) + ((double) i / radix)); + if (!tmpWrite[pos]) + { + ac.array[pos] = tmp[pos]; ac.aa++; - tmpwrite[pos] = true; + tmpWrite[pos] = true; } ac.marked.set(register, pos); } - for(int i = 0; i < tmpwrite.length; i++) - if(tmpwrite[i]==false){ - ac.array[i]=tmp[i]; + for (int i = 0; i < tmpWrite.length; i++) + { + if (!tmpWrite[i]) + { + ac.array[i] = tmp[i]; ac.aa++; } - clearmarked(); + } + clearMarked(); } }