This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 7th, 2012  |  syntax: Java  |  size: 1.80 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import javax.swing.*;
  4.  
  5. public class Difficult extends Canvas{
  6.   protected static int[] buffer;
  7.   protected static final int width = 70;
  8.   protected static final int height = 70;
  9.  
  10.   public Difficult() {
  11.     setSize(700,725);
  12.     setBackground(Color.white);
  13.     setForeground(Color.black);
  14.     do_spiral();
  15.   }
  16.    
  17.   private void draw_line(int x, int y, int xx, int yy) {
  18.     for(int sy = y; sy <= yy; sy++) {
  19.       for(int sx = x; sx <= xx; sx++) {
  20.         buffer[sy * height + sx] = 1;
  21.       }
  22.     }
  23.   }
  24.  
  25.   private void do_spiral() {
  26.     int speed = 1;
  27.     int x = width / 2;
  28.     int y = height / 2;
  29.     buffer = new int[width*height];
  30.     for(int i = 0; i < height-4; i++) {
  31.       int dir = i % 4;
  32.       switch(dir) {
  33.         // up
  34.         case 0:
  35.           draw_line(x, y-speed, x, y);
  36.           y -= speed;
  37.           break;
  38.         // left
  39.         case 1:
  40.           speed += 2;
  41.           draw_line(x-speed, y, x, y);
  42.           x -= speed;
  43.           break;
  44.         // down
  45.         case 2:
  46.           draw_line(x, y, x, y+speed);
  47.           y += speed;
  48.           break;
  49.         // right
  50.         case 3:
  51.           speed += 2;
  52.           draw_line(x, y, x+speed, y);
  53.           x += speed;
  54.           break;
  55.       }
  56.     }
  57.   }
  58.  
  59.   public void paint(Graphics g) {
  60.     for(int sy = 0; sy < height; sy++) {
  61.       for(int sx = 0; sx < width; sx++) {
  62.         if (buffer[sy*height+sx] == 1)
  63.           g.fillRect(sx*10, sy*10, 10, 10);
  64.       }
  65.     }
  66.   }
  67.  
  68.   public static void main(String [] args) {
  69.     Difficult d = new Difficult();
  70.     Difficult dtwo = new Difficult();
  71.     JFrame f = new JFrame("Challenge #18");
  72.     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73.     f.setSize(700,725);
  74.     f.add(d);
  75.     f.setVisible(true);
  76.   }
  77. }
clone this paste RAW Paste Data