id
stringlengths
36
36
text
stringlengths
1
1.25M
2a2203ac-adbd-40ce-9679-6f9bd581da61
private static void drawTiled1dAnimated() { final int size = 1000; final int scale = 400; final int frames = 75; double p = 1.0 / 3; int seed = (int) (Integer.MAX_VALUE * Math.random()); int n = 6; Perlin2d pn = new Perlin2d(p, n, seed); final double[][] y = pn.createTiledArray(size, frames); final BufferedImage[] images = new BufferedImage[frames]; for (int i = 0; i < frames; i++) { BufferedImage b = new BufferedImage(scale, scale, BufferedImage.TYPE_INT_RGB); Graphics bg = b.getGraphics(); bg.setColor(Color.red); for (int j = 0; j < size; j++) { bg.fillOval((int) (j * 1.0 * scale / size), (int) (y[j][i] * scale), 2, 2); } images[i] = b; } //save as .gif Driver.saveGif(images, "1d tiled animation - " + seed + ".gif"); //display final JPanel panel = new JPanel() { public int count; @Override public void paintComponent(Graphics g) { g.drawImage(images[count / 10], 0, 0, null); count = (count + 1) % (frames * 10); } }; panel.setPreferredSize(new Dimension(scale, scale)); Driver.display(panel); }
d298f7c7-09a6-45fe-84a0-c7a664967585
@Override public void paintComponent(Graphics g) { g.drawImage(images[count / 10], 0, 0, null); count = (count + 1) % (frames * 10); }
e50e598f-9a86-4883-b50c-f59463f6b351
private static void drawTiled2d() { int width = 800; int height = 600; double p = 1.0 / 2; int seed = (int) (Integer.MAX_VALUE * Math.random()); int n = 4; Perlin2d pn = new Perlin2d(p, n, seed); double[][] y = pn.createTiledArray(width, height); //draw final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int v = (int) (255 * .5 * y[i][j]) + 255 / 2; //result.setRGB(i, j, new Color(v, v, v).getRGB()); result.setRGB(i, j, new Color(255 - v, 255 - v, v).getRGB()); } } //save as .png Driver.savePng(result, "2d tiled - " + seed + ".png"); //display final JPanel panel = new JPanel() { @Override public void paintComponent(Graphics g) { g.drawImage(result, 0, 0, null); } }; panel.setPreferredSize(new Dimension(width, height)); Driver.display(panel); }
6b5aedf3-91a2-43ef-9804-5d9d070c1a7a
@Override public void paintComponent(Graphics g) { g.drawImage(result, 0, 0, null); }
5b5f2791-06e4-4d10-84df-e9545e61c4de
public static void display(JPanel panel) { final JFrame frame = new JFrame(); frame.add(panel); frame.setVisible(true); Dimension panelDim = panel.getPreferredSize(); frame.setSize(new Dimension(panelDim.width + frame.getInsets().left + frame.getInsets().right, panelDim.height + frame.getInsets().top + frame.getInsets().bottom)); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); new Thread("Draw loop") { @Override public void run() { while (true) { try { sleep(10); } catch (InterruptedException ex) { Logger.getLogger(Perlin2d.class.getName()).log(Level.SEVERE, null, ex); } frame.repaint(); } } }.start(); }
a5ab2a89-a73b-44c4-9d72-641faa5d54c4
@Override public void run() { while (true) { try { sleep(10); } catch (InterruptedException ex) { Logger.getLogger(Perlin2d.class.getName()).log(Level.SEVERE, null, ex); } frame.repaint(); } }
3408ffb3-6f1a-4ce9-8efe-03bdfe644b40
public static void savePng(BufferedImage image, String fname) { new File("output images/").mkdir(); try { ImageIO.write(image, "png", new File("output images/" + fname)); } catch (IOException ex) { Logger.getLogger(Perlin2d.class.getName()).log(Level.SEVERE, null, ex); } }
7c95ade7-d19e-40aa-b3d4-4ae7c8c702ef
public static void saveGif(BufferedImage[] images, String fname) { new File("output images/").mkdir(); try { new File("output images").mkdir(); ImageOutputStream output = new FileImageOutputStream(new File("output images/" + fname)); GifSequenceWriter writer = new GifSequenceWriter(output, images[0].getType(), 100, true); for (BufferedImage bi : images) { writer.writeToSequence(bi); } writer.close(); output.close(); } catch (FileNotFoundException ex) { Logger.getLogger(Perlin2d.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Perlin2d.class.getName()).log(Level.SEVERE, null, ex); } }
08acf71c-cb70-4965-9868-915a6a6970cc
public double interpolate(double a, double b, double fractional);
4c0b4987-85a5-4e3d-be61-e266bb77e212
@Override public double interpolate(double a, double b, double fractional) { double ft = fractional * 3.1415927; double f = (1 - Math.cos(ft)) * .5; return a * (1 - f) + b * f; }
46234616-1436-4009-84c0-8fd3916e3f4e
@Override public double interpolate(double a, double b, double fractional) { return a * (1 - fractional) + b * fractional; }
596ca6fb-5e9f-477c-affe-ec6578b448a2
void applyForces(double seconds);
cba8dbda-ae10-4e3a-a3c2-350925743b0e
void addForce(Vector2D force);
8e5e2c44-f138-4ab1-a448-1926b36a98e9
Vector2D getVel();
0db08509-8dbf-47cf-90fe-68fcba8f95c2
void setVel(Vector2D vel);
f6aa74dd-b14d-486a-9f4d-3c67f5e0b48f
void applyDrag(double seconds);
149a7341-ac9c-48c9-a6a9-d416f96c9295
Rectangle getCollisionBox();
6e488177-d5b5-4227-b33c-8c2c6c45dd3d
void move(int x, int y);
863b9f01-ce9d-46df-adf5-f7400337e674
public Renderer() { }
e9780746-bdab-4927-8b2b-9048bcf26f8b
public void invalidate() { stale = true; }
6b03d275-5d69-426f-bbe6-3be95dd1bcc0
public void invalidate(Rectangle r) { updateComp(r); updateImage(r); }
23590c1c-e0df-47ad-b615-847870a6c51d
public void setWorld(Level w) { world = w; finComp = new BufferedImage( w.getBounds().width, w.getBounds().height, BufferedImage.TYPE_4BYTE_ABGR); precomp = new double[w.getBounds().width] [w.getBounds().height] [Util.CHANNELS]; stale = true; }
67e01a80-d2bc-4f3d-96b0-46e2875c1b5b
public void addStaticProp(Drawable prop) { staticProps.add(prop); }
658311e5-b3f5-4947-a2f6-e031be756220
public void addDynProp(Drawable prop) { dynProps.add(prop); }
c013f74c-079a-43ad-a300-c34d34eec6a0
private void updateComp() { // System.out.println("Updating everything."); double[][][] worldP = world.getPixels(); //convert array from double[] to int for (int x = 0; x < worldP.length; x++) { for (int y = 0; y < worldP[0].length; y++) { for (int ch = 0; ch < Util.CHANNELS; ch++) { precomp[x][y][ch] = worldP[x][y][ch]; } } } for (Drawable d : staticProps) { // System.out.println("adding light at " + ((Light) d).getPos()); if (d.isDrawn()) { add(d.getPixels(), d.getBounds()); } } stale = false; }
eeb868c3-8544-4a41-95af-8ceb10e8fc1d
private void updateComp(Rectangle bounds) { // System.out.println("Updating region."); double[][][] worldP = world.getPixels(); //convert array from double[] to int int left = Math.max(0, bounds.x); int bottom = Math.max(0, bounds.y); int right = Math.min(worldP.length, bounds.x + bounds.width); int top = Math.min(worldP[0].length, bounds.y + bounds.height); for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { for (int ch = 0; ch < Util.CHANNELS; ch++) { precomp[x][y][ch] = worldP[x][y][ch]; } } } for (Drawable d : staticProps) { // System.out.println("adding light at " + ((Light) d).getPos()); if (d.isDrawn()) { add(d.getPixels(), d.getBounds(), bounds); } } }
03127b99-5e49-48fa-82a8-4fca5abcaf2c
private void add(double[][][] pixels, Rectangle bound) { Rectangle worldBounds = world.getBounds(); int left = Math.max(bound.x, worldBounds.x); int bottom = Math.max(bound.y, worldBounds.y); int right = (Math.min(bound.x + bound.width, worldBounds.x + worldBounds.width)); int top = (Math.min(bound.y + bound.height, worldBounds.y + worldBounds.height)); for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { precomp[x][y][Util.R] += pixels[x - bound.x] [y - bound.y] [Util.R]; precomp[x][y][Util.G] += pixels[x - bound.x] [y - bound.y] [Util.G]; precomp[x][y][Util.B] += pixels[x - bound.x] [y - bound.y] [Util.B]; } } }
1eaafaa4-d75a-469b-8474-6fc844098e7b
private void add(double[][][] pixels, Rectangle bound, Rectangle region) { Rectangle worldBounds = world.getBounds(); int left = Math.max(bound.x, worldBounds.x); int bottom = Math.max(bound.y, worldBounds.y); int right = (Math.min(bound.x + bound.width, worldBounds.x + worldBounds.width)); int top = (Math.min(bound.y + bound.height, worldBounds.y + worldBounds.height)); //only update the relevant region left = Math.max(left, region.x); right = Math.min(right, region.x + region.width); bottom = Math.max(bottom, region.y); top = Math.min(top, region.y + region.height); for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { precomp[x][y][Util.R] += pixels[x - bound.x] [y - bound.y] [Util.R]; precomp[x][y][Util.G] += pixels[x - bound.x] [y - bound.y] [Util.G]; precomp[x][y][Util.B] += pixels[x - bound.x] [y - bound.y] [Util.B]; } } }
9dfbcea0-aca7-40df-a832-6031649de757
private void updateImage() { Util.pixelsToImage(precomp, finComp); }
c6e78747-7d6e-4a17-9729-63e85745a491
private void updateImage(Rectangle region) { Util.pixelsToImage(precomp, finComp, region); }
ac5451c2-53b6-4ea4-9288-252acf76bad2
public void draw(Graphics g, Component comp, int offsetX, int offsetY) { if (stale) { updateComp(); updateImage(); } Rectangle bounds = world.getBounds(); if (screen == null || screen.getWidth() != comp.getWidth() || screen.getHeight() != comp.getHeight()) { screen = new BufferedImage( comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); gScreen = screen.createGraphics(); } //black out the background gScreen.setPaint(Color.black); gScreen.fillRect(0, 0, screen.getWidth(), screen.getHeight()); //draw bg that covers screen gScreen.drawImage(finComp, bounds.x - offsetX, bounds.y - bounds.height + offsetY + comp.getHeight(), null); mergeDynProps(offsetX, offsetY); g.drawImage(screen, 0, 0, null); }
8eb32a35-c685-43ba-911c-ff1b0d43e1b6
private void mergeDynProps(int offsetX, int offsetY) { for (Drawable prop : dynProps) { mergeDynProp(prop, offsetX, offsetY); } }
398fdd28-4069-4e6d-bc73-640899b6d674
private void mergeDynProp(Drawable prop, int offsetX, int offsetY) { byte[] pixels = ((DataBufferByte) screen .getRaster() .getDataBuffer()) .getData(); Rectangle r = prop.getBounds(); byte[] hPixels = prop.getData(); int col = 0; int row = 0; double mix; double mixa; int pi; for (int i = 0; i < hPixels.length; i += Util.CHANNELS) { pi = col + ((r.x - offsetX) << 2) + (row * screen.getWidth() << 2) + ((screen.getHeight() - (r.y - offsetY + r.height)) * screen.getWidth() << 2); if (pi < 0 || pi > pixels.length) { System.out.println("caught a " + pi); break; } if (hPixels[i] == (byte) -1) { pixels[pi + Util.B] = hPixels[i + Util.B]; pixels[pi + Util.G] = hPixels[i + Util.G]; pixels[pi + Util.R] = hPixels[i + Util.R]; } else { mix = (double) (hPixels[i] & Util.B_MAX) / Util.B_MAX; mixa = 1.0 - mix; pixels[pi + Util.B] = (byte) ((hPixels[i + Util.B] & Util.B_MAX) * mix + (pixels[pi + Util.B] & Util.B_MAX) * mixa); pixels[pi + Util.G] = (byte) ((hPixels[i + Util.G] & Util.B_MAX) * mix + (pixels[pi + Util.G] & Util.B_MAX) * mixa); pixels[pi + Util.R] = (byte) ((hPixels[i + Util.R] & Util.B_MAX) * mix + (pixels[pi + Util.R] & Util.B_MAX) * mixa); } col += Util.CHANNELS; if (col >= r.width * Util.CHANNELS) { col = 0; row++; } //System.out.println("Row: " + row + "; Col: " + col); } }
9d9256a2-7906-4830-8db0-0e8a25354ecb
private Texture(String path) { this.path = path; }
b784ef4a-9cb5-4bdf-b524-d9d9d0ad5604
public String getName() { return path; }
2104c47d-4cf8-46b8-8295-663bca44c337
public Engine() { addKeyListener(new Controller()); setBackground(Color.black); setPreferredSize(new Dimension(WIDTH, HEIGHT)); setFocusable(true); //init world final int defWidth = 150; final int defHeight = 15; final Random gen = new Random(); //50% random, 25% hills, 25% platform if (gen.nextBoolean()) { world = RandomLevel.genWorldRandom(defWidth, defHeight, tp); } else { if (gen.nextBoolean()) { world = RandomLevel.genWorldHills(defWidth, defHeight, tp); } else { world = RandomLevel.genWorldPlatform(defWidth, defHeight, tp); } } // world = new Level("house.txt", tp); //init hero hero = new Hero(); hero.setImage(tp.get(Texture.hero)); hero.setPos(world.getStart()); renderer.addDynProp(hero); renderer.setWorld(world); //init test light Point[] bgLights = world.getAll(Texture.bgLightDead); Light tempLight; final int cso = 15; //Cell size offset. final int rRange = 100; final int rMin = 75; for (Point light : bgLights) { tempLight = new Light(light.x + cso, light.y + cso, world); tempLight.setRadius((short) (Math.random() * rRange + rMin)); renderer.addStaticProp(tempLight); triggers.add(tempLight); } //Don't start this loop until setup is complete! if (thread == null) { running = true; thread = new Thread(this); thread.start(); } }
835da74f-78e9-4919-9891-861e608f5f65
public void paintComponent(Graphics page) { super.paintComponent(page); setForeground(Color.cyan); renderer.draw(page, this, offX, offY); // hero.draw(this, page, offX, offY); for (Entity effect : effects) { effect.draw(this, page, offX, offY); /*if (effect instanceof Burst) { Burst pop = (Burst) effect; pop.draw(this, page, offX, offY); }*/ } //Draw hero position information Rectangle r = hero.getBounds(); page.drawString("Position: x = " + (r.x + (r.width >> 1)), SCORE_PLACE_X, SCORE_PLACE_Y); final int tab = 100; final int lineHeight = 20; page.drawString("y = " + (r.y + (r.height >> 1)), SCORE_PLACE_X + tab, SCORE_PLACE_Y); page.drawString("Frame: " + frame, SCORE_PLACE_X, SCORE_PLACE_Y + lineHeight); if (!running) { page.drawString("Game over :(", WIDTH / 2, HEIGHT / 2); } }
f619b62f-f283-4e0f-96fd-d553ebd89b25
public void simulate(Dynamic obj, long timeStep) { /* Actions: * 1. apply drag * 2. add forces to object * - gravity (unless resting on block) * - arrow keys * * 3. apply forces to object. (done by the object) * * 4. a) move in x axis * 5. a) resolve collisions in x axis * * 4. b) move in y axis * 5. b) resolve collisions in y axis */ double seconds = timeStep / (double) NS_PER_S; // 1. apply drag obj.applyDrag(seconds); // 2. add forces obj.addForce(new Vector2D(0, GRAVITY)); obj.addForce(keyForce); // 3. apply forces obj.applyForces(seconds); // get velocity in preparation for step 4 Vector2D vel = obj.getVel(); hero.setImage(tp.get(Texture.hero)); // 4. a) move x obj.move((int) (vel.x * seconds), 0); // 5. a) resolve collisions in x Point bad = getWorldCollision(obj, Texture.brick); // bad = null; if (bad != null) { int resolution = world.escapeX(obj, vel.x * seconds, bad); obj.move(resolution, 0); obj.getVel().setX(0); } // 4. b) move y obj.move(0, (int) (vel.y * seconds)); // 5. b) resolve collisions in y bad = getWorldCollision(obj, Texture.brick); // bad = null; if (bad != null) { int resolution = world.escapeY(obj, vel.y * seconds, bad); if (vel.y < 0) { hero.setOnGround(true); hero.setImage(tp.get(Texture.heroGround)); } obj.move(0, resolution); obj.getVel().setY(0); } else { hero.setOnGround(false); } }
26c8a1f7-7aec-4578-a40d-c3057012210d
private Point getWorldCollision(Dynamic obj, Texture target) { Rectangle r = obj.getCollisionBox(); Point lowerLeft = new Point(r.x, r.y); Point upperRight = new Point(r.x + r.width - 1, r.y + r.height - 1); int cell = World.CELL_SIZE; //If x is negative, offset by 1. It sucks but is necessary. if (lowerLeft.x < 0) { lowerLeft.x -= cell; } if (upperRight.x < 0) { upperRight.x -= cell; } //Convert pixel coordinates to world coordinates. lowerLeft.x /= cell; lowerLeft.y /= cell; upperRight.x /= cell; upperRight.y /= cell; //Test for overlap with the target block type for (int x = (lowerLeft.x); x <= (upperRight.x); x++) { for (int y = (lowerLeft.y); y <= (upperRight.y); y++) { if (world.getCell(x, y) == target) { return new Point(x, y); } } } return null; }
86ac3877-f8cf-4e0d-b6c5-400303e05079
public void keyPressed(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: offY += SCROLL_SPEED; break; case KeyEvent.VK_DOWN: offY -= SCROLL_SPEED; break; case KeyEvent.VK_LEFT: offX -= SCROLL_SPEED; break; case KeyEvent.VK_RIGHT: offX += SCROLL_SPEED; break; case KeyEvent.VK_W: up = true; break; case KeyEvent.VK_S: down = true; break; case KeyEvent.VK_A: left = true; break; case KeyEvent.VK_D: right = true; break; case KeyEvent.VK_SPACE: if (hero.isOnGround()) { hero.setVel(new Vector2D(hero.getVel().x, JUMP)); } break; default: // ignore other characters } //add actual forces updateForces(); }
e3df3d15-123b-424e-8fe0-f5cde1819a6e
public void keyReleased(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_W: up = false; break; case KeyEvent.VK_S: down = false; break; case KeyEvent.VK_A: left = false; break; case KeyEvent.VK_D: right = false; break; default: // ignore other characters } //add actual forces updateForces(); }
7baf6eb0-de22-4c21-b212-c17a6dd8e9d0
private void updateForces() { keyForce.set(0.0, 0.0); if (up && !down) { keyForce.offset(0, SPEED); } else if (down && !up) { keyForce.offset(0, -SPEED); } if (left && !right) { keyForce.offset(-SPEED, 0); } else if (right && !left) { keyForce.offset(SPEED, 0); } }
ae210305-9d56-4fa3-8d34-b852823f8d05
public void run() { long start = System.nanoTime(); long elapsed; long wait; final int minTimeBetweenFrames = 5; while (running) { //Timing //elapsed is the number of nanoseconds since last frame elapsed = System.nanoTime() - start; elapsed = Math.min(MAX_FRAME, elapsed); start = System.nanoTime(); Point pos = hero.getPos(); frame++; //Physics! simulate(hero, elapsed); //Move camera updateCamPos(); //Test 'victory' conditions if (pos.y < LOWER_BOUND) { running = false; } //Test world events (like touching a light) testTriggers(triggers); //Delete (release) dead effects. deleteDeadEntities(); //redraw everything repaint(); //"elapsed" is reassigned to the number of nanoseconds //since this frame began elapsed = System.nanoTime() - start; //wait is how much time is remaining //before the next frame needs to be drawn. wait = targetTime - elapsed / NS_PER_MS; //wait at least 5 ms between frames. if (wait < 0) { wait = minTimeBetweenFrames; } try { Thread.sleep(wait); } catch (InterruptedException e) { e.printStackTrace(); } } //end while } // end run
e4e321d0-bee8-445a-8b42-1294adc75191
private void testTriggers(ArrayList<Trigger> trigs) { for (Trigger trigger : trigs) { if (trigger.isTriggered(hero.getBounds())) { trigger.triggerAction(); Entity burst = new Burst(tp); burst.setPos(((Light) trigger).getPos()); effects.add(burst); if (trigger instanceof Drawable) { renderer.invalidate(((Drawable) trigger).getBounds()); } } } }
b178bb41-7150-4075-93a7-a2b71cef6a4a
private void deleteDeadEntities() { ArrayList<Entity> deadFX = new ArrayList<Entity>(); for (Entity effect : effects) { if (effect instanceof Burst) { Burst pop = (Burst) effect; if (pop.isDead()) { deadFX.add((Entity) pop); } } } effects.removeAll(deadFX); }
cd8c58f0-5cbb-434c-a8dc-77c4ac85787e
private void updateCamPos() { final int marginX = (int) (getWidth() * 0.25); final int marginTop = (int) (getHeight() * 0.2); final int marginBottom = (int) (getHeight() * 0.2); Point pos = hero.getPos(); Dimension dim = hero.getSize(); // update x motion // If too far left if (pos.x - marginX < offX) { offX = pos.x - marginX; //if too far right } else if (pos.x + marginX > offX + getWidth() - dim.width) { offX = pos.x + marginX - getWidth() + dim.width; } // update y motion //If too low if (pos.y - marginBottom < offY) { offY = pos.y - marginBottom; //If too high } else if (pos.y + marginTop > offY + getHeight() - dim.height) { offY = pos.y + marginTop - getHeight() + dim.height; } }
776ceeb6-94c8-40b3-ba6c-799e429d2471
double[][][] getPixels();
7bcf80f8-d217-41ca-a31c-a840f077a613
byte[] getData();
61b8715d-7ed0-4e75-80c1-23c8296642d9
Rectangle getBounds();
346af580-bd08-43be-988b-165224e56124
short getDrawType();
6fb6fc39-5b0f-4f8b-b2c6-f5fb370aa416
boolean isDrawn();
9b53c01b-28b8-451f-8b8d-130e8949462f
public Entity() { pos = new Point(0, 0); }
128a8774-72c3-483f-9776-56989703b211
public void setImage(Image image) { brush = image; pixels = new double[image.getWidth(null)] [image.getHeight(null)] [Util.CHANNELS]; Util.imageToPixels(Util.toBufferedImage(image), pixels); BufferedImage bi = Util.toBufferedImage(brush); data = ((DataBufferByte) bi.getRaster() .getDataBuffer()).getData(); }
10f5e045-a632-4253-8cfd-6a6dc740bd12
public Dimension getSize() { return new Dimension(brush.getWidth(null), brush.getHeight(null)); }
31d09451-f6f6-4f3a-9b1d-b16e067ad926
public Rectangle getBounds() { return new Rectangle(pos.x, pos.y, brush.getWidth(null), brush.getHeight(null)); }
4579812b-ee45-4730-b36b-41f9d7053a84
public void setPos(int x, int y) { pos.x = x; pos.y = y; }
cb9f811e-b8b4-4f93-a9b7-6e9fac5fd348
public void setPos(Point p) { pos = new Point(p); }
fe1e6b89-c00f-472c-bd2b-5ca2323c0dff
public void move(int x, int y) { pos.x += x; pos.y += y; }
773564a8-f499-4d89-aafd-1305fc9c8997
public Point getPos() { return pos; }
f72d092d-b927-4e74-a78e-1ab27741950d
public void draw(Component comp, Graphics page, int offsetX, int offsetY) { //set x, y to the bottom left corner int x = 0; int y = comp.getHeight() - brush.getHeight(null); //move right and up by the x and y values. x += pos.x; y -= pos.y; //this should be a subtraction //offset by the camera x -= offsetX; y += offsetY; // brush.paintIcon(comp, page, x, y); page.drawImage(brush, x, y, null); }
baf66985-c5f8-49be-9dc0-cd6745d13d2b
@Override public double[][][] getPixels() { return pixels; }
dc6c02c6-8931-410d-b27f-243467182f1c
@Override public short getDrawType() { // TODO Auto-generated method stub return 0; }
a7c98913-e6a3-4508-ad06-334c5198cc84
@Override public boolean isDrawn() { return true; }
42fb1809-1d61-42d6-a740-b2ca2a7e142b
@Override public byte[] getData() { return data; }
915a4fec-c5ed-44d6-a65e-d509d7aa62f9
public TexturePack(String basePath) { String path; URL res; Toolkit defToolkit = Toolkit.getDefaultToolkit(); BufferedImage swap; for (int i = 0; i < Texture.values().length; i++) { path = basePath + Texture.values()[i].getName(); /* Don't ask me why this next line is important. Just trust me. * It has something to do with image observers, and without it * a call to image.getWidth(null) fails. */ images[i] = new ImageIcon(getClass().getResource(path)); res = getClass().getResource(path); imgs[i] = defToolkit.getImage(res); swap = Util.toBufferedImage(imgs[i]); pixels[i] = new double[imgs[i].getWidth(null)] [imgs[i].getHeight(null)] [Util.CHANNELS]; Util.imageToPixels(swap, pixels[i]); } }
3a450c50-4782-4b8a-ae93-86d1bc34242c
public Image get(Texture t) { return imgs[t.ordinal()]; }
61897964-0e93-4ad6-864e-fddadab1fb4d
public double[][][] getP(Texture t) { return pixels[t.ordinal()]; }
89c90896-67d2-4de8-baa7-b43766c45d65
boolean isTriggered(Rectangle bounds);
49946283-69e5-4984-a838-f56c26ee2566
void triggerAction();
ba4901f2-d8ab-4d89-b5fd-e73a7885e693
public static void main(String[] args) { JFrame frame = new JFrame("Direction"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new Engine()); frame.pack(); frame.setVisible(true); }
f78ee3ed-f9f9-4af6-a296-acad1f1e4734
public static void imageToPixels(BufferedImage image, double[][][] result) { final double maxByte = 255.0; final byte[] pixels = ((DataBufferByte) image.getRaster() .getDataBuffer()).getData(); final int width = image.getWidth(); final int height = image.getHeight(); final boolean hasAlphaChannel = image.getAlphaRaster() != null; int row = height - 1; int col = 0; if (hasAlphaChannel) { final int pixelLength = 4; for (int pixel = 0; pixel < pixels.length; pixel += pixelLength) { //must convert from signed byte (-128:127) to unsigned byte, //therefore add 256 to a negative value. result[col][row][A] = ((pixels[pixel + A] >= 0) ? pixels[pixel + A] : pixels[pixel + A] + BYTE) / maxByte; // alpha result[col][row][B] = ((pixels[pixel + B] >= 0) ? pixels[pixel + B] : pixels[pixel + B] + BYTE) / maxByte; // blue result[col][row][G] = ((pixels[pixel + G] >= 0) ? pixels[pixel + G] : pixels[pixel + G] + BYTE) / maxByte; // green result[col][row][R] = ((pixels[pixel + R] >= 0) ? pixels[pixel + R] : pixels[pixel + R] + BYTE) / maxByte; // red col++; if (col == width) { col = 0; row--; } } } else { final int pixelLength = 3; for (int pixel = 0; pixel < pixels.length; pixel += pixelLength) { result[col][row][A] = 1.0; // alpha result[col][row][B] = (pixels[pixel] >= 0) ? pixels[pixel] : pixels[pixel] + BYTE; // blue result[col][row][G] = (pixels[pixel + 1] >= 0) ? pixels[pixel + 1] : pixels[pixel + 1] + BYTE; // green result[col][row][R] = (pixels[pixel + 2] >= 0) ? pixels[pixel + 2] : pixels[pixel + 2] + BYTE; // red col++; if (col == width) { col = 0; row--; } } } // System.out.println("at 0,0: A=" + result[0][0][A] // + ", R=" + result[0][0][R] // + ", G=" + result[0][0][G] // + ", B=" + result[0][0][B]); //return result; }
4c8b209f-d62a-4721-af44-441d4371cce5
public static void pixelsToImage(double[][][] pixels, BufferedImage image) { if (image.getWidth() != pixels.length || image.getHeight() != pixels[0].length) { throw new IllegalArgumentException("Image size " + "does not match pixel array."); } //boolean hasAlpha = image.getAlphaRaster() != null; int y = pixels[0].length - 1; int x = 0; int width = pixels.length; int height = pixels[0].length; int[] dbPacked = new int[width * height]; for (int i = 0; i < dbPacked.length; i++) { dbPacked[i] = toIntRGBA(pixels[x][y]); x++; if (x == width) { x = 0; y--; } } WritableRaster raster = Raster.createPackedRaster(DataBuffer.TYPE_INT, pixels.length, pixels[0].length, CHANNELS, BITS_PER_CHANNEL, null); raster.setDataElements(0, 0, pixels.length, pixels[0].length, dbPacked); image.setData(raster); }
41e410d2-da7d-4ddd-a8f1-9d2be30a9879
public static void pixelsToImage(double[][][] pixels, BufferedImage image, Rectangle region) { if (image.getWidth() != pixels.length || image.getHeight() != pixels[0].length) { throw new IllegalArgumentException("Image size " + "does not match pixel array."); } //boolean hasAlpha = image.getAlphaRaster() != null; //Identify the boundary of the box (cropped to the image) int left = Math.max(0, region.x); int bottom = Math.max(0, region.y); int right = Math.min(image.getWidth(), region.x + region.width); int top = Math.min(image.getHeight(), region.y + region.height); //Set width, height and starting position. int width = right - left; int height = top - bottom; int x = left; int y = top - 1; int[] dbPacked = new int[width * height]; for (int i = 0; i < dbPacked.length; i++) { dbPacked[i] = toIntRGBA(pixels[x][y]); x++; if (x == right) { x = left; y--; } } int rTop = image.getHeight() - top; WritableRaster r2 = Raster.createPackedRaster( DataBuffer.TYPE_INT, width, height, CHANNELS, BITS_PER_CHANNEL, new Point(left, rTop)); r2.setDataElements(left, rTop, width, height, dbPacked); image.setData(r2); }
b0516087-d7f6-4381-ad02-6563402cbbdd
public static void copyPixelArray(double[][][] source, double[][][] dest) { for (int x = 0; x < source.length; x++) { for (int y = 0; y < source[0].length; y++) { System.arraycopy( source[x][y], 0, dest[x][y], 0, source[0].length); } } }
9c317cf3-5f14-41dd-b3d7-84566c86b022
public static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } // Create a buffered image with transparency BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR); // Draw the image on to the buffered image Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(img, 0, 0, null); bGr.dispose(); // Return the buffered image return bimage; }
3ce84813-0f8c-4cf8-9829-98c3144c719c
public static int toIntRGBA(double[] argb) { int channel = (int) (argb[Util.A] * B_MAX); channel = Math.max(0, Math.min(channel, B_MAX)); int color = channel << 0; channel = (int) (argb[Util.B] * B_MAX); channel = Math.max(0, Math.min(channel, B_MAX)); color += channel << (B * BITS_PER_CHANNEL); channel = (int) (argb[Util.G] * B_MAX); channel = Math.max(0, Math.min(channel, B_MAX)); color += channel << (G * BITS_PER_CHANNEL); channel = (int) (argb[Util.R] * B_MAX); channel = Math.max(0, Math.min(channel, B_MAX)); color += channel << (R * BITS_PER_CHANNEL); return color; }
7e9ce91b-946a-457d-ae8b-bf514d49bb4a
public Vector2D(double x, double y) { this.x = x; this.y = y; }
860b45f9-0a06-47f6-bfce-35a3219f7df1
public Vector2D(Vector2D v) { x = v.x; y = v.y; }
fc3043e9-2a00-4e33-ab1b-dc3873e8556f
public double getX() { return x; }
07fadcfd-352c-4d9e-aa14-eddb2328ed2b
public double getY() { return y; }
d4c5cd18-cf10-4328-bcc7-33b5edad50ea
public void setX(double newX) { x = newX; }
5010df81-1a7f-4154-97e2-63749a11c0ca
public void setY(double newY) { y = newY; }
5abdfbd5-938f-4467-a67f-1ec53a592088
public void set(double newX, double newY) { this.x = newX; this.y = newY; }
6e178a37-b32f-480a-901d-b56d7369396f
public double length() { return Math.sqrt(x * x + y * y); }
1687130f-f372-41cd-a74e-1d9fd425546e
public Vector2D normalize() { double a = length(); return new Vector2D(x / a, y / a); }
43ee18fc-c445-460d-8eef-42dd8bfdaff2
public Vector2D negate() { return new Vector2D(-x, -y); }
19407999-66aa-49b8-995b-cb7a6d87f8fd
public Vector2D multiply(double factor) { return new Vector2D(x * factor, y * factor); }
26f324fa-0c82-4323-8555-2499b72274e3
public void offset(Vector2D v) { x += v.x; y += v.y; }
15c1a226-3a67-486d-a345-8915811c5eba
public void offset(double offsetX, double offsetY) { x += offsetX; y += offsetY; }
a47d9abd-47a1-4105-badd-2e2145d827c6
public Hero() { super(); vel = new Vector2D(0, 0); force = new Vector2D(0, 0); }
f4144453-d466-4ad7-b7e6-fbf476c64f07
@Override public void applyForces(double seconds) { vel.x += force.x * seconds; vel.y += force.y * seconds; force.set(0, 0); }
01a1b8c4-5be4-44ca-87e2-11922c036ae1
@Override public void addForce(Vector2D f) { force.offset(f); }
a14f26f4-ba1b-4dfb-a0bc-4bd8b5f2b739
@Override public Vector2D getVel() { return vel; }
cb2facbe-8c5e-4bd8-8cb7-6044be547cb7
@Override public void setVel(Vector2D v) { vel = new Vector2D(v); }
3cedf7b8-8c78-40e4-adf0-38ae415d4647
@Override public void applyDrag(double seconds) { double factorX = 1 - DRAG_FACTOR * seconds; double factorY = 1 - (DRAG_FACTOR * DRAG_FACTOR_Y) * seconds; vel = new Vector2D(vel.x * factorX, vel.y * factorY); }
2b64f12f-29bd-4df3-95d0-495dec5a224c
@Override /** * Get the width and height of the entity. * @return A new dimension object holding the width and height. */ public Dimension getSize() { Dimension dim = super.getSize(); Dimension result = new Dimension((int) (dim.height * CBOX_SCALE), (int) (dim.width * CBOX_SCALE)); return result; }
64c7897a-453d-46ae-9579-fa382184f692
public boolean isOnGround() { return onGround; }
6e265b36-a326-4977-9a50-04e6916af032
public void setOnGround(boolean onGround) { this.onGround = onGround; }
075ad312-d7ce-4fc1-bfa0-ec05dee077f2
@Override public Rectangle getBounds() { Rectangle bounds = super.getBounds(); // bounds.x -= (bounds.width * (1 - CBOX_SCALE) / 2); // bounds.y -= (bounds.height * (1 - CBOX_SCALE) / 2); // bounds.width *= CBOX_SCALE; // bounds.height *= CBOX_SCALE; return bounds; }
1dd8494c-343f-4dfd-b34a-a0ac6871855d
@Override public Rectangle getCollisionBox() { Rectangle bounds = super.getBounds(); bounds.x += (bounds.width * (1 - CBOX_SCALE) / 2); bounds.y += (bounds.height * (1 - CBOX_SCALE) / 2); bounds.width *= CBOX_SCALE; bounds.height *= CBOX_SCALE; return bounds; }