// // StackingPlot.java // // Just the plot itself. // import java.awt.*; import java.awt.image.*; import java.io.*; public class StackingPlot { static final String captionText = "Perfect Shuffle Plot"; private Font captionFont = new Font("serif", Font.ITALIC+Font.BOLD, 36); private Font unitFont = new Font("serif", Font.PLAIN, 12); protected BufferedImage buf; private long highNumber = -1; public StackingPlot(int xExtent, int yExtent){ newBuffer( xExtent, yExtent); } public void updateBuffer( int width, int height){ if( width == buf.getWidth() && height == buf.getHeight()) return; newBuffer(width, height); try { String fileName = new String("../../shuffles.bin_unsignedlonglong"); long len = (new File(fileName)).length()/8; highNumber = (2*(len-1)); DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream(fileName) )); System.out.println("First few perfect shuffles ..."); in.mark(1024); for (int i = 4; i < 56; i+= 2) System.out.println(i + " : " + in.readLong()); in.reset(); System.out.println("file includes sets of size 4 - " + highNumber ); int sumValue = 0; int maxValue = 0; int emptyPixel = 0; // accumulate on each element in the column double idh = (double)height/((double)highNumber); double dw = (double)len/(double)width; double dwh = (double)len/(double)width/(double)height; long el = 0; for(int c = 0; c < width; c++){ int [] col = new int[height]; for(long bottomOfNextCol = (long)((double)(c+1)/(double)width*(double)len) ; el < bottomOfNextCol; el++) col[ height - 1 - (int)(idh*in.readLong())]++; // normalize colors, keep statistics int gray = Color.lightGray.getRGB(); for(int e = 0; e < height; e++){ int colEl = col[e]; sumValue += colEl; maxValue = (maxValue > colEl) ? maxValue : colEl; float hue = (((float)colEl)/100.f); hue = (hue < 1.f) ? hue : 1.f; // max 1. if( hue == 0.f ){ col[e] = gray; emptyPixel++; } else col[e] = Color.HSBtoRGB(hue, 1.f, 1.f); } // add column to buffer buf.setRGB(c, 0, 1, height, col, 0, 1); } colorBar( 32, width/2, 0, 10, buf.getGraphics()); // statistics System.out.println("Total Number of Plotted Points: " + sumValue); System.out.println("Empty pixels: " + emptyPixel ); System.out.println("Maximum pixel value: " + maxValue); } catch(EOFException e) { System.out.println("End of stream encountered"); } catch( FileNotFoundException e) { System.out.println("File not found: " + e.getMessage()); } catch (IOException e) { System.out.println("IO Exception: " + e.getMessage()); } } public void colorBar( int x1, int x2, int y1, int y2, Graphics g) { for (int bar = x1; bar < x2; bar++){ float h = ((float)(bar - x1))/(float)((x2 - x1)); g.setColor(Color.getHSBColor(h, 1.f,1.f)); g.drawLine(bar, y1, bar, y2); } g.setColor(Color.black); g.setFont(unitFont); g.drawString("0", x1, y2 + 10); g.drawString("100+", x2, y2 + 10); } public void newBuffer(int width, int height) { buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB ); } public void paintCaption(int xSpan, int ySpan, Graphics g){ g.setColor(Color.blue); g.setFont (captionFont); g.drawString(captionText, xSpan/4, 100); } public void paintGrid( int x0, int y0, int x, int y, Graphics g ){ g.setColor(Color.black); g.drawLine(x0,y0,x,y0); // top g.drawLine(x0,y0,x0, y); // left g.drawLine(x0,y,x,y); // bottom g.drawLine(x,y0, x, y); // right int dx = x - x0; int dy = y - y0; int div = 10; // divisions int mdiv = 2; // minor sub divisions int tickWidth = 5; int mtickWidth = 3; // vertical marks int dtick; dtick = dy/div; for( int i = 0; i < div; i++){ int tick = (int)((float)i/(float)div*dy) + y0; g.drawLine(x0, tick, x0 + tickWidth, tick ); for( int j = 1; j < mdiv; j++){ int subTick = (int)(tick + (float)j/(float)mdiv*dtick); g.drawLine(x0, subTick, x0 + mtickWidth, subTick); } } // horizontal marks dtick = dx/div; for( int i = 0; i <= div; i++){ int tick = (int)((float)i/(float)div*dx) + x0; g.drawLine(tick, y, tick, y - tickWidth ); for( int j = 1; j < mdiv && i != div; j++){ int subTick = (int)(tick + (float)j/(float)mdiv*dtick); g.drawLine(subTick, y, subTick, y - mtickWidth); } } // units g.setColor(Color.black); g.setFont(unitFont); g.drawString("4", x0, y + 10); g.drawString(Long.toString(highNumber), x, y + 10); } public void plot(int xSpan, int ySpan, Graphics g, ImageObserver obs ){ updateBuffer(xSpan, ySpan); // // TODO: Draw the buffered graph to the screen g.drawImage(buf, 15 , 30, obs); paintCaption(xSpan, ySpan, g); paintGrid(15, 30, xSpan+15, ySpan+30, g); } }