⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 absorber.java

📁 基于JAVA的跳球游戏
💻 JAVA
字号:
/*
 * Absorber.java
 *
 * Created on 2008年5月11日, 下午8:32
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package gizmos;
import java.lang.Object;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import org.w3c.dom.*;
import java.util.*;
import gizmoball_demo.*;

 

public class Absorber extends Gizmo {
       
    private int x;  //横坐标
    private int y;  //纵坐标
    private int width;
    private int height;
    
    /** Creates a new instance of Absorber */
    public Absorber(java.lang.String name,int x,int y,int width,int height) 
    {
        this.name=name;
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
    }

    public void setPosition(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
    //Increments the state of a BasicGizmo for secs seconds in a physical environment described by env.
    //public void update(double secs,Environment env)
    //{
    //   env.update(secs); 
    //}
    //Resets all internal state, puts all contained balls in play
    public void reset()
    {
       
    }
    public int getX()
    {
        return x;
    }
    public int getWidth()
    {
        return width;
    }
    public int getHeight()
    {
        return height;
    }
    public int getY()
    {
        return y;
    }
    //return if this Absorber is over the sector at (sectorX, sectorY).
    /*public boolean overSector(int sectorX,int sectorY)
    {
        return ((this.sectors>sectorX)&&(this.sectors<sectorY));//sectors是一个坐标
    }
    public java.lang.Iterable<BoardSector> getSectors(){}
    //
    public boolean overlapsWith(Ball ball)
    {
        //小球的坐标和吸收器的坐标重合
        return ( this.x== ball.getX() );   
    }  
    //Returns:
    //The number of seconds until ball collides with this Gizmo if they will collide,
    //Double.POSITIVE_INFINITY otherwise.
    public double collisionTime(Ball ball)
    {
        
        
    }*/
    
    public boolean collide(BouncingBall ball)
    {
        if(ball==null) throw new java.lang.IllegalArgumentException("Ball "+ball+" does not exist...");   
        int dx = ball.getX() - this.getX();
        int dy = ball.getY() - this.getY();
	int 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
			 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;
    }
    //Draws an Absorber using g.
    public void draw(java.awt.Graphics2D g)
    {
        Rectangle2D ab = new Rectangle2D.Double(x,y,width,height);
        g.fill(ab);
    }
    //Draw the bounding box of a GameItem.
    public void drawBB(java.awt.Graphics2D g,java.awt.Color c)
    {
        //bouding box 的边界如何界定
        Rectangle2D abBox = new Rectangle2D.Double(x,y,width+2,height+2);
        g.setColor(c);
        g.fill(abBox);
    }
   
    // Get the names of a gizmo’s properties. 
    //Subclasses may override this method, but must make use of it for the superclass properties.
    //Return an ordered list of property names.
    public java.util.List<java.lang.String> getPropertyNames()
    {
        java.util.List<String> proNameList = new  java.util.LinkedList<String>();
        proNameList.add("name");
        proNameList.add("x");
        proNameList.add("y");
        proNameList.add("width");
        proNameList.add("height");
        return proNameList;
    }
    public java.util.List<java.lang.String> getPropertyValues()
    {
        java.util.List<String> proValList = new  java.util.LinkedList<String>();
        proValList.add(this.name);
        proValList.add(((Integer)(this.x)).toString());
        proValList.add(((Integer)(this.y)).toString());
        proValList.add(((Integer)(this.width)).toString());
        proValList.add(((Integer)(this.height)).toString());
        return proValList;
    }
   /* public void setPropertyValues(java.util.List<java.lang.String> values)throws GizmoPropertyException
    {
        this.name=values.get(0);
        this.x=Integer.parseInt(values.get(1));
        this.y=Integer.parseInt(values.get(2));
        this.width=Integer.parseInt(values.get(3));
        this.height=Integer.parseInt(values.get(4));
    }*/
    
    //Create an XML representation of a Gizmo. The XML returned is a well-formed node.
    //compatible - Be strictly compatible with 6.170 schema
    /*public org.w3c.dom.Element toXML(boolean compatible)
    { 
       //Element gizmoElement=new ;  //先创建一个空元素
        
        if(compatible==true)
        {
           java.util.List<String>  properties = getPropertyValues();
           java.util.List<String>  attributes = getPropertyNames();
            for(int i=0; i<attributes.size(); i++)
            {
                gizmoElement.setAttribute(attributes.get(i),properties.get(i));
                
            }
            
        }
       return gizmoElement;
            
    }*/
    //public int pollScore(){}
    //public int popScore(){}

 
    
    
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -