Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4.  
  5. public class Class {
  6.    
  7.    
  8.  
  9. public static void main(String[] args){
  10.      int x = 500; int y = 500;
  11.    
  12.     JFrame f = new JFrame("Pixels");
  13.     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.    
  15.     GUI p = new GUI();
  16.     p.setBackground(Color.WHITE);
  17.     f.add(p);
  18.     f.setSize(x,y);
  19.         f.setVisible(true);
  20.        
  21.    
  22. }
  23. }
  24.  
  25.  
  26.  
  27.  
  28. import java.awt.*;
  29. import java.awt.event.*;
  30. import javax.swing.*;
  31. import java.util.Random;
  32.  
  33. public class GUI extends JPanel {
  34.     private final static int MAX_X = 1920;
  35.     private final static int MAX_Y = 1080;
  36.  
  37.     public void paintComponent(Graphics g) {
  38.         super.paintComponent(g);
  39.  
  40.         for (int x = 0; x < MAX_X; x++) {
  41.             for (int y = 0; y < MAX_Y; y++) {
  42.      
  43.                 Color randomColor = Color.BLACK; //Black because it wouldn't work uninitialized.
  44.                
  45.                 //Slightly inefficient?
  46.                 int number; Random rColor = new Random();
  47.                 number = rColor.nextInt(3);
  48.                
  49.                  if(number == 0){randomColor = Color.RED;}
  50.                  else if(number ==1){randomColor = Color.GREEN;}
  51.                  else if(number == 2){randomColor = Color.BLUE;}
  52.                
  53.                
  54.                 g.setColor(randomColor); //Red, Blue, or Green. Depends on if number is 0, 1 or 2.
  55.                 g.drawRect(x, y, 1, 1);
  56.             }
  57.                
  58.        
  59.         }
  60.        
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement