import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Image; import java.awt.MediaTracker; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.io.InputStream; import java.text.NumberFormat; import java.util.ArrayList; import javax.swing.JFrame; /** * Main window and entry point to WedgeDude application. */ @SuppressWarnings("serial") public class WedgeFrame extends JFrame { public static final int BLOCK_SIZE = 8192; private WedgeWorld wedgeWorld; public WedgeFrame() throws IOException { super("Wedge Dude"); setLayout(new BorderLayout()); this.wedgeWorld = new WedgeWorld(this); add(this.wedgeWorld, BorderLayout.CENTER); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { System.exit(0); } }); } public Image loadImage(String name) throws IOException { InputStream is = WedgeFrame.class.getClassLoader().getResourceAsStream(name); byte[] imageData = readFully(is); Image im = getToolkit().createImage(imageData); MediaTracker mediaTracker = new MediaTracker(this); mediaTracker.addImage(im, 0); try { mediaTracker.waitForID(0); } catch (InterruptedException ie) { } return im; } /** * Read the entire contents of the given input stream, returning the result * as a byte array. */ public static byte[] readFully(InputStream is) throws IOException { if (is == null) return null; ArrayList blocks = new ArrayList(); byte[] buffer = new byte[BLOCK_SIZE]; int blockBytesRead = 0; int bytesRead = is.read(buffer); while (bytesRead != -1) { blockBytesRead += bytesRead; if (blockBytesRead == buffer.length) { blocks.add(buffer); buffer = new byte[BLOCK_SIZE]; blockBytesRead = 0; } bytesRead = is.read(buffer, blockBytesRead, buffer.length - blockBytesRead); } int size = blocks.size() * BLOCK_SIZE + blockBytesRead; byte[] result = new byte[size]; int index = 0; for (byte[] b : blocks) { System.arraycopy(b, 0, result, index, BLOCK_SIZE); index += BLOCK_SIZE; } System.arraycopy(buffer, 0, result, index, blockBytesRead); return result; } public static void main(String[] args) throws Exception { WedgeFrame f = new WedgeFrame(); f.setSize(new Dimension(640, 640)); f.setLocation(200, 200); f.setVisible(true); } }