home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / BRICK / BRICK.EXE / Brick.java < prev    next >
Encoding:
Java Source  |  1996-04-23  |  2.6 KB  |  112 lines

  1.  
  2. import java.awt.Graphics;
  3. import java.awt.Color;
  4. import java.awt.Point;
  5. import java.util.Random;
  6.  
  7. public class Brick {
  8.   public int x1, x2, y1, y2;
  9.   public boolean active;
  10.   int value, rnd;
  11.   Color color;
  12.   static Graphics g;
  13.   private Point tmp;
  14.  
  15.   public Brick (int x1, int y1, int x2, int y2, Color color, int value) {
  16. //    System.out.println("Brick:: New Brick!" + color);
  17.  
  18.     this.x1 = x1;
  19.     this.y1 = y1;
  20.     this.x2 = x2;
  21.     this.y2 = y2;
  22.  
  23.     this.value = value;
  24.     this.color = color;
  25.  
  26.     active=true;
  27.  
  28. //    System.out.println(toString());
  29.   }
  30.  
  31.   public void setGraphics (Graphics g) {
  32.     this.g = g;
  33.   }
  34.  
  35.   public void draw () {
  36.     if (active) {
  37.       rnd = (int) ((y2 - y1) * 0.5f);
  38.       synchronized(g) {
  39.         g.setColor(color);
  40.         g.fillRoundRect(x1, y1, x2 - x1, y2 - y1, rnd, rnd);
  41.       }
  42.     }
  43.   }
  44.  
  45.   public void undraw () {
  46.     synchronized(g) {
  47.       g.setColor(Color.black);
  48.       g.fillRect(x1, y1, x2 - x1, y2 - y1);
  49.     }
  50.   }
  51.  
  52.   public void setActive(boolean state) {
  53.     this.active = state;
  54.   }
  55.  
  56.   public void setColor(Color color) {
  57.     this.color = color;
  58.   }
  59.  
  60.   public int getValue() {
  61.     return value;
  62.   }
  63.  
  64.   public void setValue(int val) {
  65.     this.value = val;
  66.   }
  67.  
  68.   public boolean impact (Ball b) {
  69.     if (!active) return false;
  70.  
  71.     if 
  72.     ((tmp = b.right()).x >= x1 && tmp.x <= x2 && tmp.y >= y1 && tmp.y <= y2) {
  73.       System.out.println(this + "Right Impact.");
  74.       b.play(BricksConstants.EFX_IMPACT_BRICK);
  75.       b.impact(BricksConstants.IMPACT_RIGHT, y1, y2, 0.1f);
  76.     } else if 
  77.  
  78.     ((tmp = b.left()).x >= x1 && tmp.x <= x2 && tmp.y >= y1 && tmp.y <= y2) {
  79.       System.out.println(this + "Left Impact.");
  80.       b.play(BricksConstants.EFX_IMPACT_BRICK);
  81.       b.impact(BricksConstants.IMPACT_LEFT, y1, y2, 0.1f);
  82.     } else if 
  83.  
  84.     ((tmp = b.top()).x >= x1 && tmp.x <= x2 && tmp.y >= y1 && tmp.y <= y2) {
  85.       System.out.println(this + "Top Impact.");
  86.       b.play(BricksConstants.EFX_IMPACT_BRICK);
  87.       b.impact(BricksConstants.IMPACT_TOP, x1, x2, 0.1f);
  88.     } else if 
  89.  
  90.     ((tmp = b.bottom()).x >= x1 && tmp.x <= x2 && tmp.y >= y1 && tmp.y <= y2) {
  91.       System.out.println(this + "Bottom Impact.");
  92.       b.play(BricksConstants.EFX_IMPACT_BRICK);
  93.       b.impact(BricksConstants.IMPACT_BOTTOM, x1, x2, 0.1f);
  94.     } else {
  95.       // no impact here
  96.       return false;
  97.     }
  98.  
  99.     // impact
  100.     active = false;
  101.     undraw();
  102.     b.addScore(value);
  103.  
  104.     return true;
  105.   }
  106.  
  107.   public String toString () {
  108.     return ("Brick:: (" + x1 + "," + y1 + ").");
  109.   }
  110. }
  111.  
  112.