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

📄 fraction.java

📁 CroftSoft Code Library是一个开源的可移植的纯Java游戏库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
             if ( fractionQuestion.getSecond ( )
               && !fractionQuestion.getThird ( ) )
             {
               if ( ( fractionQuestion.getCommonDenominator ( )
                 == fractionDoor.getDenominator ( ) )
                 && ( fractionQuestion.getThirdAnswerNumerator ( )
                 == fractionDoor.getNumerator ( ) ) )
               {
                 fractionQuestion.setThird ( true );

                 fractionDoor.setOpen ( );

                 popAudioClip.play ( );

                 setHighlighted (
                   fractionQuestion.getThirdNumerator ( ),
                   fractionQuestion.getThirdDenominator ( ) );
               }
             }

             if ( fractionQuestion.getThird ( )
               && !fractionQuestion.getFourth ( ) )
             {
               if ( ( fractionQuestion.getThirdDenominator ( )
                 == fractionDoor.getDenominator ( ) )
                 && ( fractionQuestion.getThirdNumerator ( )
                 == fractionDoor.getNumerator ( ) ) )
               {
                 fractionQuestion.setFourth ( true );

                 bongoAudioClip.play ( );

                 scoreTextSprite.setText ( "Score:  " + ++score );

                 answerTextSprite.setText (
                   fractionQuestion.getFirstNumerator ( )
                   + "/"
                   + fractionQuestion.getFirstDenominator ( )
                   + " + "
                   + fractionQuestion.getSecondNumerator ( )
                   + "/"
                   + fractionQuestion.getSecondDenominator ( )
                   + " = " 
                   + ( fractionQuestion.getFourth ( )
                   ? fractionQuestion.getThirdNumerator ( ) + "/"
                   + fractionQuestion.getThirdDenominator ( ) : "?/?" ) );

                 reset ( );
               }
             }

             break;
           }         }

         fractionHeroSprite.update ( component );
       }

       if ( crossedDoor )       {         component.repaint ( );       }       else       {         fractionHeroSprite.getPaintBounds ( paintBounds1 );         paintBounds1.add ( paintBounds0 );         component.repaint ( paintBounds1 );       }       String  firstAnswer
         = fractionQuestion.getFirst ( )
           ? ( fractionQuestion.getFirstAnswerNumerator ( ) + "/"
           + fractionQuestion.getCommonDenominator ( ) )
           : "?/?";

       String  secondAnswer
         = fractionQuestion.getSecond ( )
           ? ( fractionQuestion.getSecondAnswerNumerator ( ) + "/"
           + fractionQuestion.getCommonDenominator ( ) )
           : "?/?";

       String  thirdAnswer
         = fractionQuestion.getSecond ( )
           ? ( fractionQuestion.getThirdAnswerNumerator ( ) + "/"
           + fractionQuestion.getCommonDenominator ( ) )
           : "?/?";

       questionTextSprite.setText ( 
         fractionQuestion.getFirstNumerator ( )
         + "/"
         + fractionQuestion.getFirstDenominator ( )
         + " + "
         + fractionQuestion.getSecondNumerator ( )
         + "/"
         + fractionQuestion.getSecondDenominator ( )
         + " = " 
         + firstAnswer
         + " + "
         + secondAnswer
         + " = "
         + thirdAnswer
         + ( ( fractionQuestion.getThirdNumerator ( )
               == fractionQuestion.getThirdAnswerNumerator ( ) )
           && ( fractionQuestion.getThirdDenominator ( )
                == fractionQuestion.getCommonDenominator ( ) )
           ? ""
           : " = ?/?" ) );
     }

     public void  paint (
       JComponent  component,
       Graphics2D  graphics2D )
     //////////////////////////////////////////////////////////////////////
     {
       graphics2D.setColor ( BACKGROUND_COLOR );

       graphics2D.fillRect ( 0, 0, bounds.width, bounds.height );

       graphics2D.setColor ( MOVEMENT_LINE_COLOR );

       int  verticalMovementX
         = fractionHeroSprite.getVerticalMovementX ( );

       int  horizontalMovementY
         = fractionHeroSprite.getHorizontalMovementY ( );

       graphics2D.drawLine (
         verticalMovementX, 0, verticalMovementX, bounds.height - 1);

       for ( int  i = 0; i < fractionDoors.length; i++ )
       {
         fractionDoors [ i ].paint ( component, graphics2D );
       }

       for ( int  i = 1; i <= FLOORS; i++ )
       {
         int  y = i * floorHeight;

         graphics2D.setColor ( FLOOR_COLOR );

         graphics2D.drawLine ( 0, y, bounds.width, y );
       }

       graphics2D.setColor ( MOVEMENT_LINE_COLOR );

       graphics2D.drawLine (
         0,
         horizontalMovementY,
         bounds.width - 1,
         horizontalMovementY );

       questionTextSprite.paint ( component, graphics2D );

       answerTextSprite.paint ( component, graphics2D );

       scoreTextSprite.paint ( component, graphics2D );

       fractionHeroSprite.paint ( component, graphics2D );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     private void  resetBounds ( )
     //////////////////////////////////////////////////////////////////////
     {
       animatedComponent.getBounds ( bounds );

       score = 0;

       scoreTextSprite.setText ( "Score:  0" );

       answerTextSprite.setText ( INIT_ANSWER_TEXT );
       // ( 600 / 5 ) / 30 = 4 pixels per frame = 120 pixels per second

       spriteVelocity
         = ( int ) Math.ceil ( ( bounds.width / 5 ) / FRAME_RATE );
       floorHeight = bounds.height / ( FLOORS + 1 );

       FontLib.setMaxFont (
         animatedComponent,
         "Score:  " + Integer.toString ( Integer.MAX_VALUE ),
         FONT_NAME_SCORE,
         FONT_STYLE,
         bounds.width / 2,
         floorHeight );

       scoreTextSprite.setFont ( animatedComponent.getFont ( ) );

       answerTextSprite.setFont ( animatedComponent.getFont ( ) );

       scoreTextSprite.setY ( floorHeight * FLOORS + floorHeight );

       questionTextSprite.setY ( 3 * floorHeight / 4 );

       answerTextSprite.setY ( floorHeight * FLOORS + floorHeight );

       FontLib.setMaxFont (
         animatedComponent,
         "?/? + ?/? = ?/? + ?/? = ?/? = ?/?",
         FONT_NAME_SCORE,
         FONT_STYLE,
         3 * bounds.width / 4,
         floorHeight );

       questionTextSprite.setFont ( animatedComponent.getFont ( ) );

       int  doorHeight = ( int ) ( floorHeight * 0.9 );

       int  doorSpacing = bounds.width / ( FLOORS + 1 );

       int  doorWidth
         = ( int ) ( doorHeight / MathConstants.GOLDEN_RATIO );

       fractionHeroSprite.setDiameter ( doorHeight );

       int  doorOffset = ( doorSpacing - doorWidth ) / 2;

       questionTextSprite.setX ( doorWidth + 2 * doorOffset );

       answerTextSprite.setX (
         bounds.width / 2 + doorWidth / 2 + doorOffset );

       scoreTextSprite.setX ( doorWidth + 2 * doorOffset );

       FontLib.setMaxFont (
         animatedComponent,
         Integer.toString ( FLOORS ),
         FONT_NAME,
         FONT_STYLE,
         doorWidth,
         doorHeight / 2 );

       java.util.List  fractionDoorList = new ArrayList ( );

       Graphics  graphics = animatedComponent.getGraphics ( );

       FontMetrics  fontMetrics = graphics.getFontMetrics ( );

       for ( int  i = 1; i <= FLOORS; i++ )
       {
         int  y = ( i - 1 ) * floorHeight;

         Rectangle2D  denominatorTextBounds = fontMetrics.getStringBounds (
           Integer.toString ( i ), graphics );

         int  denominatorY
           = y + floorHeight - doorHeight / 2
           - ( int ) denominatorTextBounds.getY ( );

         for ( int  j = 0; j <= i; j++ )
         {
           int  x = ( j * doorSpacing * FLOORS ) / i + doorOffset;

           Rectangle  rectangle = new Rectangle (
             x,
             y + floorHeight - doorHeight,
             doorWidth,
             doorHeight );

           Rectangle2D  numeratorTextBounds
             = fontMetrics.getStringBounds (
             Integer.toString ( j ), graphics );

           int  numeratorX = ( int )
             ( x + ( doorWidth - numeratorTextBounds.getWidth ( ) ) / 2 );

           int  numeratorY
             = y + floorHeight - doorHeight / 2
             - ( int ) ( numeratorTextBounds.getHeight ( )
             + numeratorTextBounds.getY ( ) );

           int  denominatorX = x + ( int )
             ( ( doorWidth - denominatorTextBounds.getWidth ( ) ) / 2 );

           fractionDoorList.add (
             new FractionDoor (
               rectangle,
               j,
               i,
               numeratorX,
               numeratorY,
               denominatorX,
               denominatorY,
               x + 4,
               y + floorHeight - doorHeight / 2,
               x + doorWidth - 4,
               y + floorHeight - doorHeight / 2,
               y + floorHeight,
               x + doorWidth / 2 ) );
         }
       }

       fractionDoors = ( FractionDoor [ ] )
         fractionDoorList.toArray ( new FractionDoor [ 0 ] );

       int  verticalMovementX
         = fractionDoors [ 0 ].getVerticalMovementX   ( );

       int  horizontalMovementY
         = fractionDoors [ 0 ].getHorizontalMovementY ( );

       fractionHeroSprite.setX ( verticalMovementX );

       fractionHeroSprite.setY ( horizontalMovementY );

       fractionHeroSprite.setVerticalMovementX ( verticalMovementX );

       fractionHeroSprite.setHorizontalMovementY ( horizontalMovementY );

       reset ( );

       boundsInitialized = true;       animatedComponent.repaint ( );
     }

     private void  reset ( )
     //////////////////////////////////////////////////////////////////////
     {
       fractionQuestion.reset ( );

       for ( int  i = 0; i < fractionDoors.length; i++ )
       {
         fractionDoors [ i ].reset ( );
       }
    
       setHighlighted (
         fractionQuestion.getFirstNumerator   ( ),
         fractionQuestion.getFirstDenominator ( ) );       animatedComponent.repaint ( );
     }

     private void  setHighlighted (
       int  numerator,
       int  denominator )
     //////////////////////////////////////////////////////////////////////
     {
       for ( int  i = 0; i < fractionDoors.length; i++ )
       {
         FractionDoor  fractionDoor = fractionDoors [ i ];

         if ( ( denominator == fractionDoor.getDenominator ( ) )
           && ( numerator   == fractionDoor.getNumerator   ( ) ) )
         {
           fractionDoor.setHighlighted ( );

           break;
         }
       }       animatedComponent.repaint ( );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }

⌨️ 快捷键说明

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