Last active
August 29, 2015 14:08
-
-
Save enshiromashiro/f155e0c530ffed51e30e to your computer and use it in GitHub Desktop.
Drawing stripes with Java (Swing/WritableRaster).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Canvas; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.WritableRaster; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
public class Stripe { | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("stripe"); | |
BufferedImage bi = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB); | |
long start = System.currentTimeMillis(); | |
draw(bi.getRaster(), 50, 0, new int[]{255, 255, 255, 255}, new int[]{0, 0, 0, 255}); | |
System.out.println("eplaced: " + (System.currentTimeMillis() - start) / 1000.0f); | |
Canvas canvas = new Canvas() { | |
public void paint(Graphics g) { | |
g.setColor(new Color(0, 0, 0, 255)); | |
g.fillRect(0, 0, this.getWidth(), this.getHeight()); | |
g.drawImage(bi, 0, 0, null); | |
} | |
}; | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
canvas.setPreferredSize(new Dimension(640, 480)); | |
frame.add(canvas); | |
frame.pack(); | |
frame.show(); | |
} | |
public static void draw(WritableRaster ras, int i, int o, int[] c1, int[] c2) { | |
int w = ras.getWidth(); | |
int h = ras.getHeight(); | |
for (int y=0; y<h; y++) { | |
for (int x=0; x<w; x++) { | |
if (((x + o) / i) % 2 == 0) { | |
ras.setPixel(x, y, c1); | |
} else { | |
ras.setPixel(x, y, c2); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment