import java.applet.*; import java.awt.*; import java.util.*; public class Region implements Runnable { public static final int BOUNDARY = 0; public static final int RECTANGLE = 1; public static final int CIRCLE = 2; public static final int TRANGLE = 3; public static final int ENDREGION = 4; public static final int HALFARC = 5; public static final int SLEEP = 100; public int lx, ly, xw, yw; public int cx, cy, dia; public int tx1, ty1, tx2, ty2, tx3, ty3; Color wheelcolor[] = { Color.cyan, Color.darkGray, Color.orange, Color.pink, Color.green, Color.red }; Color wheelcenter = Color.red, wheelflash = Color.yellow; boolean flashflag = false; double tangle; Rectangle r; Polygon poly; public int type; Game parent; Thread effect; public Region (Game p, int x1, int y1, int x2, int y2, int typearg) { parent = p; lx = x1; ly = y1; xw = x2; yw = y2; r = new Rectangle (lx, ly, xw, yw); type = typearg; } public Region (Game p, int x1, int y1, int diaarg) { parent = p; cx = x1; cy = y1; dia = diaarg; type = CIRCLE; } public Region (Game p, int x1, int y1, int x2, int y2) { parent = p; cx = (x1+x2)/2; cy = y2; dia = y2-y1; type = HALFARC; } public Region (Game p, int x1, int y1, int x2, int y2, int x3, int y3) { double dis; parent = p; tx1 = x1; ty1 = y1; tx2 = x2; ty2 = y2; tx3 = x3; ty3 = y3; dis = Math.sqrt ((tx2-tx1)*(tx2-tx1)+(ty2-ty1)*(ty2-ty1)); tangle = 360*Math.acos((tx1-tx2)/dis)/(2*Math.PI); if (tangle > 90) { tangle = tangle - 180; } poly = new Polygon (); poly.addPoint (tx1, ty1-PBall.ria); if (tx1>tx2) poly.addPoint (tx2-PBall.ria, ty2); else poly.addPoint (tx2+PBall.ria, ty2); poly.addPoint (tx3, ty3); type = TRANGLE; } public void delay () { try { Thread.sleep (SLEEP); } catch (InterruptedException e) {}; } public double reachRegion (PBall b) { double nangle = 0; switch (type) { case BOUNDARY: nangle = insideRegion (b); break; case RECTANGLE: nangle = outsideRegion (b); break; case CIRCLE: nangle = outsideCircle (b); if (nangle != 0) { parent.hitplay (); parent.addgrade (100); start (); } break; case HALFARC: nangle = InsideArc (b); if (nangle != 0) { parent.addgrade (30); } break; case TRANGLE: nangle = outsideTrangle (b); if (nangle != 0) { parent.addgrade (10); } break; case ENDREGION: nangle = outsideEnd (b); break; } return (nangle); } public void CheckSpeed (PBall b) { switch (type) { case BOUNDARY: b.speed -= PBall.speeddec; break; case RECTANGLE: // b.speed -= PBall.speeddec; break; case CIRCLE: // b.speed += PBall.speeddec; break; case HALFARC: // b.speed += PBall.speeddec; break; case TRANGLE: b.speed -= PBall.speeddec; break; case ENDREGION: break; } } public double insideRegion (PBall b) { double nangle = 0; if (b.y+b.ria >= ly+yw) { nangle = 180; } if (b.y-b.ria <= ly) { nangle = 180; } if (b.x+b.ria >= lx+xw) { nangle = 90; } if (b.x-b.ria <= lx) { nangle = 90; } return (nangle); } public double outsideRegion (PBall b) { double nangle = 0; if (r.inside (b.x, b.y+b.ria)) { nangle = 180; } if (r.inside (b.x, b.y-b.ria)) { nangle = 180; } if (r.inside (b.x+b.ria, b.y)) { nangle = 90; } if (r.inside (b.x-b.ria, b.y)) { nangle = 90; } return (nangle); } public double outsideCircle (PBall b) { double nangle = 0, nnangle = 0; double dis, aa, bb; int nx, ny; aa = (double) (b.x-cx); bb = (double) (b.y-cy); dis = Math.sqrt (aa*aa+bb*bb); if (dis < b.ria+dia) { nx = b.ox; ny = b.oy; aa = (double) (nx-cx); bb = (double) (ny-cy); dis = Math.sqrt (aa*aa+bb*bb); nnangle = 360*Math.acos(Math.abs(aa)/dis)/(2*Math.PI); if (aa*bb > 0) nangle = 90 - nnangle; else nangle = -1*(90-nnangle); if (nangle == 0) nangle = 180; } return (nangle); } public double InsideArc (PBall b) { double nangle = 0, nnangle = 0; double dis, aa, bb; int nx, ny; if (b.y < cy) { aa = (double) (b.x-cx); bb = (double) (b.y-cy); dis = Math.sqrt (aa*aa+bb*bb); if (dis > dia-b.ria) { nx = b.ox; ny = b.oy; aa = (double) (nx-cx); bb = (double) (ny-cy); dis = Math.sqrt (aa*aa+bb*bb); nnangle = 360*Math.acos(Math.abs(aa)/dis)/(2*Math.PI); if (aa*bb > 0) nangle = 90 - nnangle; else nangle = -1*(90-nnangle); if (nangle == 0) nangle = 180; } } return (nangle); } public double outsideTrangle (PBall b) { double nangle = 0; if (poly.inside (b.x, b.y+b.ria) || poly.inside (b.x, b.y+b.ria/2)) { nangle = tangle; } if (poly.inside (b.x, b.y-b.ria) || poly.inside (b.x, b.y-b.ria/2)) { nangle = tangle; } if (poly.inside (b.x+b.ria, b.y) || poly.inside (b.x+b.ria/2, b.y)) { nangle = tangle; } if (poly.inside (b.x-b.ria, b.y) || poly.inside (b.x-b.ria/2, b.y)) { nangle = tangle; } return (nangle); } public double outsideEnd (PBall b) { double nangle = 0; if (r.inside (b.x, b.y+b.ria)) { nangle = -1; } if (r.inside (b.x, b.y-b.ria)) { nangle = -1; } if (r.inside (b.x+b.ria, b.y)) { nangle = -1; } if (r.inside (b.x-b.ria, b.y)) { nangle = -1; } return (nangle); } public void draw (Graphics g) { if (type == CIRCLE) { int redcolor; int nx, ny; int randg, randb; randg = (int) (127*Math.random()); randb = (int) (127*Math.random()); for (int i=0; i<360; i++) { nx = (int) (cx + (dia-1)*Math.cos(Direction.getRand(i))); ny = (int) (cy + (dia-1)*Math.sin(Direction.getRand(i))); redcolor = (int) (255 - 155*Math.abs(cy-ny)/dia); g.setColor (new Color(redcolor, randg, randb)); g.drawLine (cx, cy, nx, ny); } g.setColor (Color.black); g.drawOval (cx-dia, cy-dia, 2*dia, 2*dia); g.setColor (wheelcenter); g.fillOval (cx-dia+12, cy-dia+12, 2*dia-24, 2*dia-24); g.setColor (Color.black); g.drawOval (cx-dia+12, cy-dia+12, 2*dia-24, 2*dia-24); if (flashflag) { g.setColor (Color.white); for (int j=5;j<20;j+=5) g.drawOval (cx-dia-j, cy-dia-j, 2*(dia+j), 2*(dia+j)); } } } public void checkflash (Graphics g) { if ((type == CIRCLE) && flashflag) { g.setColor (Color.black); g.drawOval (cx-dia, cy-dia, 2*dia, 2*dia); g.setColor (wheelflash); g.fillOval (cx-dia+12, cy-dia+12, 2*dia-24, 2*dia-24); g.setColor (Color.black); g.drawOval (cx-dia+12, cy-dia+12, 2*dia-24, 2*dia-24); if (flashflag) { g.setColor (Color.white); for (int j=5;j<20;j+=5) g.drawOval (cx-dia-j, cy-dia-j, 2*(dia+j), 2*(dia+j)); } } } public void randomwheel () { Color tmp; int i, j; for (i=0; i