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

📄 cubesystem.java

📁 wince 5.0上基于personal java写的的虚拟魔方
💻 JAVA
字号:

/*
 * Java Virtual Cube
 * -----------------
 *
 * Copyright 1996, Song Li  
 * URL: http://www.cs.umbc.edu/~sli2
 *
 * 
 * You can use the code for any nonprofittable use. But remember to mention
 * the authors' names in your revised program. You are also encouraged to
 * improve the program or give your comments and bug reports. If there are
 * further questions, please contact me and I'll be glad to help. My E-Mail
 * address is: sli2@gl.umbc.edu.
 *
 */




import java.awt.*;
import java.util.*;




class CubeSystem {

    final static String PlaneName [] = {"Plane0", "Plane1", "Plane2" } ;
        
    final static int XPlane = 0 ;
    final static int YPlane = 1 ;
    final static int ZPlane = 2 ;

    CubeMap   Map  = new CubeMap () ;
    Object3D  Cube = WholeCube () ;
    CubePlane PlaneTable [] [] ;


    Model3D   ModelPlane [] ;
    CubeModel ModelCube ;



    CubeSystem (Vertex viewpoint) {
        ModelCube  = new CubeModel (viewpoint) ;
        ModelPlane = new Model3D [3] ;
        for (int axis=XPlane; axis<=ZPlane; axis++)
            ModelPlane [axis] = new Model3D (ModelCube.GetView ()) ;
        
        PlaneTable = new CubePlane [3] [3] ;
        for (int axis=XPlane; axis<=ZPlane; axis++)
            for (int plane=0; plane<3; plane++) {
                PlaneTable [axis] [plane] = new CubePlane (axis, plane) ;
                ModelPlane[axis].AddObject (PlaneName[plane], PlaneTable[axis][plane]) ;
                }

        for (int i=0; i<6; i++)
            ((CubeFace) Cube.Faces[i]).SetMap (Map.ColorMap [i]) ;
        ModelCube.AddObject ("Cube", Cube) ;
    }



    void DefineBound (int width, int height) {
        ModelCube.DefineBound (width, height) ;
    }


    void XRotateView (double theta) {ModelCube.XRotateView (theta) ;}
    void YRotateView (double theta) {ModelCube.YRotateView (theta) ;}
    void ZRotateView (double theta) {ModelCube.ZRotateView (theta) ;}



    private boolean InSpin = false ;
    private int     CurAxis ;
    private int     CurPlane ;
    private boolean CurCounterclock ;




    public synchronized void Paint (Graphics graph, int width, int height) {
        if (InSpin)
            ModelPlane [CurAxis].Paint (graph, width, height) ;
        else
            ModelCube.Paint (graph, width, height) ;
        return ;
    }
    
    

    public void SpinMap (int axis, int plane, boolean dir) {
        Map.Spin (axis, plane, dir) ;
    }
    

    public void SetCubeMap () {
        for (int i=0; i<6; i++)
            ((CubeFace) Cube.Faces[i]).SetMap (Map.ColorMap [i]) ;
    }
    


    public synchronized void SpinBegin (int axis, int plane, boolean counterclock) {
        if (InSpin) return;
        InSpin = true ;

        for (int i=0; i<3; i++)
            PlaneTable [axis] [i].SetMap (Map.ColorMap) ;

        CurAxis         = axis ;
        CurPlane        = plane ;
        CurCounterclock = counterclock ;
    }
    
    
    

    public synchronized void Spin (int step) {
        if (! InSpin)
            return ;

        double theta = (CurCounterclock ? 15 : -15) * step * Math.PI / 180 ;
        switch (CurAxis) {
            case XPlane: ModelPlane[XPlane].XRotate (PlaneName[CurPlane], theta) ;  break;
            case YPlane: ModelPlane[YPlane].YRotate (PlaneName[CurPlane], theta) ;  break;
            case ZPlane: ModelPlane[ZPlane].ZRotate (PlaneName[CurPlane], theta) ;  break;
            }
        ModelPlane[CurAxis].Transform (PlaneName [CurPlane]) ;
    }



    
    public synchronized void SpinEnd () {
        if (! InSpin)
            return ;

        ModelPlane[CurAxis].Unify (PlaneName [CurPlane]) ;
        ModelPlane[CurAxis].Transform (PlaneName [CurPlane]) ;
        
        SpinMap (CurAxis, CurPlane, CurCounterclock) ;
        SetCubeMap () ;
        InSpin = false ;
        notify () ;
    }        


    
    public Vertex Unproject (float selx, float sely) {
        return ModelCube.Unproject (selx, sely) ;
    }



    public static int SelectFace (Vertex hotpoint) {
        if (hotpoint == null)
            return -1 ;
        if (hotpoint.x == -3)  return 0 ;
        if (hotpoint.x ==  3)  return 1 ;
        if (hotpoint.y == -3)  return 2 ;
        if (hotpoint.y ==  3)  return 3 ;
        if (hotpoint.z == -3)  return 4 ;
        if (hotpoint.z ==  3)  return 5 ;
        return -1 ;
    }


    public static int SelectHor (Vertex hotpoint) {
        if (hotpoint == null)
            return -1 ;

        int pos = -1 ;
        
        if (hotpoint.x==-3 || hotpoint.x==3)
            pos = (int) ((hotpoint.y + 3) / 2) ;
        if (hotpoint.y==-3 || hotpoint.y==3)
            pos = (int) ((hotpoint.z + 3) / 2) ;
        if (hotpoint.z==-3 || hotpoint.z==3)
            pos = (int) ((hotpoint.x + 3) / 2) ;

        return pos<3 ? pos : 2 ;
    }



    public static int SelectVer (Vertex hotpoint) {
        if (hotpoint == null)
            return -1 ;

        int pos = -1 ;
        
        if (hotpoint.x==-3 || hotpoint.x==3)
            pos = (int) ((hotpoint.z + 3) / 2) ;
        if (hotpoint.y==-3 || hotpoint.y==3)
            pos = (int) ((hotpoint.x + 3) / 2) ;
        if (hotpoint.z==-3 || hotpoint.z==3)
            pos = (int) ((hotpoint.y + 3) / 2) ;

        return pos<3 ? pos : 2 ;
    }



    private final static int SpinTable []  [] = {
        {500, 202, 201, 200, 400, 410, 420, 300, 301, 302, 520, 510, 500} ,
        {501, 212, 211, 210, 401, 411, 421, 310, 311, 312, 521, 511, 501} ,
        {502, 222, 221, 220, 402, 412, 422, 320, 321, 322, 522, 512, 502} ,
        {502, 120, 110, 100, 402, 401, 400,   0,  10,  20, 500, 501, 502} ,
        {512, 121, 111, 101, 412, 411, 410,   1,  11,  21, 510, 511, 512} ,
        {522, 122, 112, 102, 422, 421, 420,   2,  12,  22, 520, 521, 522} ,
        {100, 101, 102, 320, 310, 300,   2,   1,   0, 200, 210, 220, 100} ,
        {110, 111, 112, 321, 311, 301,  12,  11,  10, 201, 211, 221, 110} ,
        {120, 121, 122, 322, 312, 302,  22,  21,  20, 202, 212, 222, 120} } ;



    public static int ComputeSpin (Vertex start, Vertex end) {
        
        int startface  = SelectFace (start) ;
        int startver   = SelectVer  (start) ;
        int starthor   = SelectHor  (start) ;
        int startmagic = startface*100 + startver*10 + starthor ;
        if (startface==-1 || startver==-1 || starthor==-1)
            return 9999 ;

        int endface  = SelectFace (end) ;
        int endver   = SelectVer  (end) ;
        int endhor   = SelectHor  (end) ;
        int endmagic = endface*100 + endver*10 + endhor ;
        if (endface==-1 || endver==-1 || endhor==-1)
            return 9999 ;

        for (int circle=0; circle<9; circle++)
            for (int num=0; num<=11; num++) {
                if (SpinTable [circle] [num]   == startmagic &&
                    SpinTable [circle] [num+1] == endmagic)
                    return circle + 1;
                if (SpinTable [circle] [num]   == endmagic &&
                    SpinTable [circle] [num+1] == startmagic)
                    return - (circle + 1);
                }
        
        return 9999 ;
    }







    private Object3D WholeCube () {
        Vertex   vertex [] = new Vertex   [8] ;
        CubeFace face   [] = new CubeFace [6] ;

        vertex[0] = new Vertex (-3, -3, -3) ;
        vertex[1] = new Vertex ( 3, -3, -3) ;
        vertex[2] = new Vertex ( 3,  3, -3) ;
        vertex[3] = new Vertex (-3,  3, -3) ;
        vertex[4] = new Vertex (-3, -3,  3) ;
        vertex[5] = new Vertex ( 3, -3,  3) ;
        vertex[6] = new Vertex ( 3,  3,  3) ;
        vertex[7] = new Vertex (-3,  3,  3) ;
        
        for (int i=0; i<6; i++)
            face[i] = new CubeFace (3, 3) ;
        
        face[0].Vertices[0] = vertex[0] ;
        face[0].Vertices[1] = vertex[4] ;
        face[0].Vertices[2] = vertex[7] ;
        face[0].Vertices[3] = vertex[3] ;
        
        face[1].Vertices[0] = vertex[1] ;
        face[1].Vertices[1] = vertex[2] ;
        face[1].Vertices[2] = vertex[6] ;
        face[1].Vertices[3] = vertex[5] ;
        
        face[2].Vertices[0] = vertex[0] ;
        face[2].Vertices[1] = vertex[1] ;
        face[2].Vertices[2] = vertex[5] ;
        face[2].Vertices[3] = vertex[4] ;
        
        face[3].Vertices[0] = vertex[3] ;
        face[3].Vertices[1] = vertex[7] ;
        face[3].Vertices[2] = vertex[6] ;
        face[3].Vertices[3] = vertex[2] ;
        
        face[4].Vertices[0] = vertex[0] ;
        face[4].Vertices[1] = vertex[3] ;
        face[4].Vertices[2] = vertex[2] ;
        face[4].Vertices[3] = vertex[1] ;
        
        face[5].Vertices[0] = vertex[4] ;
        face[5].Vertices[1] = vertex[5] ;
        face[5].Vertices[2] = vertex[6] ;
        face[5].Vertices[3] = vertex[7] ;
      
        Object3D cube = new Object3D () ;
        cube.Define (vertex, 8, face, 6) ;
        return cube ;
    }



    public void NewGame () {
        Map.Reset () ;
        SetCubeMap () ;
    }
    

}

⌨️ 快捷键说明

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