Skip to content

Instantly share code, notes, and snippets.

@rz7d
Created October 1, 2019 01:13
Show Gist options
  • Save rz7d/6831bc331a74bf1d77f00452150f1721 to your computer and use it in GitHub Desktop.
Save rz7d/6831bc331a74bf1d77f00452150f1721 to your computer and use it in GitHub Desktop.
Noise texture generator
/*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
final class NoiseGen {
private NoiseGen() {
}
public static void main(String[] args) {
final int w = 768;
final int h = 768;
final int alpha = 60;
var buffer = new DataBufferInt(
new Random().ints(w * h).parallel()
.map(x -> (x & (alpha << 24)))
.toArray(),
w * h);
var raster = Raster.createPackedRaster(
buffer,
w, h, w,
// ARGB
new int[] { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 },
new Point(0, 0));
var img = new BufferedImage(ColorModel.getRGBdefault(), raster,
false, null);
preview(img);
try {
ImageIO.write(img, "PNG", new File("noise.png"));
} catch (IOException exception) {
exception.printStackTrace();
}
}
private static void preview(Image img) {
int w = img.getWidth(null);
int h = img.getHeight(null);
var contentPane = new JComponent() {
private static final long serialVersionUID = 1L;
public void paint(java.awt.Graphics g) {
g.drawImage(img,
((getWidth() - w) >> 1),
((getHeight() - h) >> 1),
this);
};
};
contentPane.setPreferredSize(new Dimension(w + 10, h + 10));
contentPane.setSize(w + 10, h + 10);
var frame = new JFrame("Preview");
frame.setDefaultCloseOperation(3);
frame.setBackground(Color.WHITE);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment