Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication4;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- import java.awt.Color;
- import java.awt.Graphics;
- public class blackwhite extends JFrame
- {
- public blackwhite()
- {
- JFrame f = new JFrame("black&white");
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setSize(400, 400);
- JPanel damier = new Checkerboard();
- f.add(damier);
- f.setVisible(true);
- }
- public static void main(String argv[])
- {
- Runnable showMyGUI = new Runnable() {
- public void run() {
- new blackwhite();
- }
- };
- SwingUtilities.invokeLater(showMyGUI);
- }
- }
- class Checkerboard extends JPanel
- {
- static final int CHECKER_SIZE = 100;
- public void paintComponent(Graphics g)
- {
- Color[] colors = {Color.MAGENTA, Color.GRAY, Color.GRAY, Color.YELLOW, Color.CYAN};
- g.setColor(Color.white);
- g.fillRect(0, 0, getWidth(), getHeight());
- int num = (int)(Math.random()*5);
- g.setColor(colors[num]);
- for (int stripeX = 0; stripeX < getWidth(); stripeX += CHECKER_SIZE) {
- for (int y = 0, row = 0; y < getHeight(); y += CHECKER_SIZE/2, ++row) {
- int x = (row % 2 == 0) ? stripeX : (stripeX + CHECKER_SIZE/2);
- g.fillRect(x, y, CHECKER_SIZE/2, CHECKER_SIZE/2);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement