📄 outerwalls.java
字号:
/*
* OuterWalls.java
*
* Created on 2008年5月11日, 下午8:36
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package gizmos;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import gizmoball_demo.*;
/**
*
* @author ICE
*/
/**
* The outer wall Gizmo.
* <br>
* Impermeable barriers surrounding the playfield.
* <br>
* Trigger: generated whenever the ball hits it
* <br>
* Action: none required
* <br>
* Coefficient of reflection: 1.0
* <br>
* <br>
* <br>
* A Gizmoball game supports exactly one set of outer walls. The user cannot
* move, delete, or rotate the outer walls. The outer walls lie just outside the
* playing area:
* <br>
* <ul>
* <li> There is one horizontal wall just above the y=0L coordinate. </li>
* <li> There is one horizontal wall just below the y=20L coordinate. </li>
* <li> There is one vertical wall just to the left of the x=0L coordinate. </li>
* <li> There is one vertical wall just to the right of the x=20L coordinate. </li>
* </ul>
*
* @specfield height : real // Height of the gizmo (in world coordinates)
* @specfield width : real // Width of the gizmo (in world coordinates)
* */
public class OuterWalls extends Gizmo{
private static final double COEFF_REFLECTION = 1.0;
private int x;
private int y;
private int width;
private int height;
/** Creates a new instance of OuterWalls */
public OuterWalls()
{
this.name="OuterWalls";
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setPosition(int x,int y)
{
this.x=x;
this.y=y;
}
//public boolean overSector(int sectorX,int sectorY){}
//public boolean overlapsWith(Ball ball){}
public void draw(java.awt.Graphics2D g)throws java.io.IOException
{
//java.net.URL imgURL = TriangleBumper.class.getResource("/gizmoball/src/image/wall.png");
Image tbImage=ImageIO.read(new File("/gizmoball/src/image/wall.png"));
g.drawImage(tbImage,x,y,null);
}
public void drawBB(java.awt.Graphics2D g,java.awt.Color c){}
//public java.lang.Iterable<BoardSector> getSectors(){}
//public double collisionTime(Ball ball){}
public boolean collide(BouncingBall ball)
{
if (ball == null)
throw new RuntimeException("Ball "+ball+" does not exist...");
final double dx = ball.getX() - this.getX();
final double dy = ball.getY() - this.getY();
final double radius = ball.getRadius();
if (dx < 0) { // left of gizmo
if (dy < 0) { // top left of gizmo
return dx*dx+dy*dy <= radius*radius;
} else if (dy > height) { // bottom left of gizmo
final double tmp = dy - height;
return dx*dx+tmp*tmp <= radius*radius;
} else { // left of gizmo
return -dx <= radius;
}
} else if (dx > width) { // right of gizmo
final double tmp = dx - width;
if (dy < 0) { // top right of gizmo
return tmp*tmp+dy*dy <= radius*radius;
} else if (dy > height) { // bottom right of gizmo
final double tmp2 = dy - height;
return tmp*tmp+tmp2*tmp2 <= radius*radius;
} else { // right of gizmo
return tmp <= radius;
}
}
return false;
}
//}
//public org.w3c.dom.Element toXML(boolean compatible)
//{
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -