Skip to content

Instantly share code, notes, and snippets.

@raoofha
Created April 21, 2018 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raoofha/7ee1042ab72ae4ca774acf89734dedb5 to your computer and use it in GitHub Desktop.
Save raoofha/7ee1042ab72ae4ca774acf89734dedb5 to your computer and use it in GitHub Desktop.
(import '(java.awt.image BufferedImage)
'(java.awt Color Canvas Frame))
(def width 640)
(def height 480)
(def cs [16711680 167123])
(defonce image (BufferedImage. width height BufferedImage/TYPE_INT_RGB))
(defonce pixels (-> image .getRaster .getDataBuffer .getData))
(defonce canvas (doto (proxy [Frame] []
(update [g] (. this paint g))
(paint [g]
(. g drawImage image 0 0 nil)))
(.setSize width height)
(.setBackground Color/black)
(.setFocusableWindowState false)
(.setVisible true)))
(defmacro for-loop [[sym init check change :as params] & steps]
`(loop [~sym ~init value# nil]
(if ~check
(let [new-value# (do ~@steps)]
(recur ~change new-value#))
value#)))
(time
(for-loop
[k 0 (< k 2) (inc k)]
(for-loop
[c 0 (< c 2) (inc c)]
(for-loop
[i 0 (< i width) (inc i)]
(for-loop
[j 0 (< j height) (inc j)]
(aset pixels (+ i (* j width)) (get cs c))))
(.repaint canvas))))
import java.awt.Frame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
public class Paint extends Frame {
public int width = 640;
public int height = 480;
public BufferedImage image;
public int[] pixels;
public int[] cs = {16711680, 167123};
public static void main(String[] args) {
new Paint();
}
public Paint() {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
setSize(width, height);
setBackground(Color.black);
setFocusableWindowState(false);
setVisible(true);
long t = System.currentTimeMillis();
for (int k = 0 ; k < 2; k++) {
for (int c = 0; c < 2; c++) {
for (int i = 0 ; i < width; i++) {
for (int j = 0; j < height; j++) {
pixels[i + j * width] = cs[c];
}
}
repaint();
}
}
System.out.println(System.currentTimeMillis() - t);
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment