Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.KeyEvent;
- import java.awt.image.BufferedImage;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import java.util.Random;
- import javax.swing.AbstractButton;
- import javax.swing.JButton;
- public class randomizer extends JPanel {
- private BufferedImage canvas;
- Random rand = new Random();
- public randomizer(int width, int height) {
- canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- randCanvas();
- }
- public Dimension getPreferredSize() {
- return new Dimension(canvas.getWidth(), canvas.getHeight());
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g;
- g2.drawImage(canvas, null, null);
- }
- public void randCanvas(){
- for (int x = 0; x < canvas.getWidth(); x++) {
- for (int y = 0; y < canvas.getHeight(); y++) {
- int n = rand.nextInt(255) + 1;
- int o = rand.nextInt(255) + 1;
- int p = rand.nextInt(255) + 1;
- Color g = new Color(n,o,p);
- int r = g.getRGB();
- canvas.setRGB(x, y, r);
- }
- }
- repaint();
- }
- public static void main(String[] args) {
- int width = 512;
- int height = 512;
- JFrame frame = new JFrame("Image Randomizer");
- randomizer panel = new randomizer(width, height);
- frame.add(panel);
- frame.pack();
- frame.setVisible(true);
- frame.setResizable(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JButton b1=new JButton("Generate New Photo");
- b1.setVerticalTextPosition(AbstractButton.CENTER);
- b1.setHorizontalTextPosition(AbstractButton.TRAILING);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement