Java随机数生成器需要帮助

标签 java

在我的代码中,我需要根据用户输入的内容使用 if 语句来绘制一定数量的棋子。因此,如果用户输入 4 个棋子,我需要其中三个位于随机位置。所以我需要生成在 Canvas 大小之间协调的随机 x 和 y。我知道我需要使用随机数生成器,但我不太确定它是如何工作的。请帮帮我。谢谢 这是我的代码...

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ChessSet extends JPanel
{
    public int numPawns;
    public int numBishops;
    public static void main()
    {
        JFrame ourFrame = new JFrame();
        ourFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
        ourFrame.setSize(1000,1000);

        ChessSet ourChessSet = new ChessSet();
        Scanner key = new Scanner( System.in );
        System.out.println( "How many pawns would you like? (0-4): " );
        ourChessSet.numPawns = key.nextInt();

        System.out.println( "How many bishops would you like? (0-4): " );
        ourChessSet.numBishops = key.nextInt();

        ourFrame.add( ourChessSet );
        ourFrame.setBackground( Color.white );

        ourFrame.setVisible(true);

    }

    public void paint( Graphics canvas )
    {
        ChessSet inst1 = new ChessSet();
        this.drawPawn( canvas, 100, 140, Color.black );
        this.drawKing( canvas, 375, 50, Color.black );
        this.drawRook( canvas, 600, 110, Color.black );
        this.drawQueen( canvas, 100, 535, Color.black );
        this.drawKnight( canvas, 350,480, Color.black );
        this.drawBishop( canvas, 700, 480, Color.black );

        canvas.drawString( "King 1", 10, 10 );
        canvas.drawString( "Rook 1", 10, 30 );
        canvas.drawString( "Queen 1", 10, 20 );  
        canvas.drawString( "Pawn " + numPawns, 100, 10 );
        canvas.drawString( "Bishop " + numBishops, 100, 30 );
        canvas.drawString( "Knight 1", 100, 20 );


        if( numPawns == 1 )
        {
            this.drawPawn( canvas, 100, 140, Color.black );

        }
        else if( numPawns == 2 )
        {
            this.drawPawn( canvas, 100, 140, Color.black );
            this.drawPawn( canvas, 800, 800, Color.black );
        }
        else if( numPawns == 3 )
        {

        }
        else if( numPawns == 4 )
        {

        }
        else
        {

        }

        if( numBishops == 1 )
        {
            this.drawBishop( canvas, 100, 140, Color.cyan );

        }
        else if( numPawns == 2 )
        {
            this.drawBishop( canvas, 100, 140, Color.black );
        }
        else if( numBishops == 3 )
        {

        }
        else if( numBishops == 4 )
        {

        }
        else
        {

        }

    }

    public void drawPawn( Graphics canvas, int x, int y, Color pawnColor )
    {
        canvas.setColor( pawnColor );
        canvas.fillOval( x-10, y-45, 120, 120 );//top oval
        canvas.fillRect( x+0, y+50, 100, 150 );//middle rect
        canvas.fillRoundRect( x-35, y+200, 170, 70, 10, 10 );//base rect
        canvas.setColor( Color.white );
        canvas.fillOval( x-25 , y+75, 50, 125 );//oval out of middle
        canvas.fillOval( x+75, y+75, 50, 125 );//oval out of middle

    }

    public void drawKing( Graphics canvas, int x, int y, Color kingColor )
    {
        canvas.setColor( kingColor );
        canvas.setColor( Color.black );
        canvas.fillRect( x-60, y+180, 210, 120 );//seconds rect from the bottom
        canvas.setColor( Color.white );
        canvas.fillOval( x-90, y+165, 60, 120 );//ovals out of second from bot
        canvas.fillOval( x+120, y+165, 60, 120 );//ovals out of second from bot
        canvas.setColor( Color.black );
        canvas.fillRoundRect( x-90, y+300, 270, 60, 10, 10 );//base rect
        canvas.fillRect( x+30, y-0 , 30, 90 );//cross rect
        canvas.fillRect( x+0, y+30, 90, 30 );//cross rect
        canvas.fillRect( x-15, y+90, 120, 60 );//first rect from top
        canvas.fillRect( x-15, y+150, 120, 30 );//first rect from top
        canvas.setColor( Color.white );
        canvas.fillOval( x-30, y+30, 30, 120 );//oval out of top rect
        canvas.fillOval( x+90, y+30, 30, 120 );//oval out of top rect
        canvas.setColor( Color.black );

    }

    public void drawRook( Graphics canvas, int x, int y, Color rookColor )
    {
        canvas.setColor( rookColor );
        canvas.setColor( Color.black );
        canvas.fillRect( x+15, y+90, 150, 180 );//middle rect
        canvas.setColor( Color.white );
        canvas.fillOval( x-30, y+30, 60, 240);//ovals on side of bottom rect
        canvas.fillOval( x+150, y+30, 60, 240);//ovals on bottom of rect
        canvas.setColor( Color.black );
        canvas.fillRoundRect( x+0, y+0, 180, 90, 20, 20);//top rec
        canvas.fillRoundRect( x+0, y+240, 180, 60, 10, 10);//base rectangle
        canvas.setColor( Color.white );
        canvas.fillRect( x+40, y+0, 15, 30);//next 3 lines are slits in top
        canvas.fillRect( x+80, y+0, 15, 30);
        canvas.fillRect( x+120, y+0, 15, 30);

    }

    public void drawQueen( Graphics canvas, int x, int y, Color queenColor )
    {
        canvas.setColor( queenColor );
        canvas.setColor( Color.black );
        canvas.fillRect( x+0, y+0, 150, 240 ); //main body rectangle
        canvas.fillOval(x-30, y+180, 60, 60);//circles added to base
        canvas.fillOval(x+120, y+180, 60, 60);//circles added to base
        canvas.fillOval(x+55, y-45, 40, 40);//circle on top
        canvas.setColor( Color.white );
        canvas.fillOval( x-45, y-30, 90, 210);//ovals out of middle rect
        canvas.fillOval( x+105, y-30, 90, 210);//ovals out of middle rect
        canvas.setColor( Color.black );
        canvas.fillRoundRect( x+30, y+60, 90, 30, 10, 10 );//rect in middle of body

    }

    public void drawKnight( Graphics canvas, int x, int y, Color knightColor )
    {
        canvas.setColor( Color.black );
        Polygon tri = new Polygon();
        tri.addPoint( x+0, y+75 );
        tri.addPoint( x+210, y+75 );
        tri.addPoint( x+105, y+165 );
        canvas.fillPolygon( tri );
        canvas.fillRoundRect( x-0, y+240, 210, 60, 10, 10 );//base rectangle
        canvas.fillOval( x+30, y+130, 60, 120 );//left oval
        canvas.fillOval( x+120, y+130, 60, 120 );//right oval
        canvas.setColor( Color.white );
        canvas.fillOval( x+100, y+105, 15, 15 );
        canvas.fillOval( x+100, y+135, 15, 15 );

    }

    public void drawBishop( Graphics canvas, int x, int y, Color bishopColor )
    {

        canvas.setColor( Color.black );
        canvas.fillOval( x+0, y+0, 90, 90 );//head
        canvas.fillRoundRect( x-30, y+90, 150, 30, 15, 15 );//shoulders
        canvas.fillRect( x-30, y+120, 150, 120 );//body
        canvas.fillOval( x+30, y-30, 30, 30 );
        canvas.setColor( Color.white );
        canvas.fillOval( x-75, y+120 , 90, 120 );//ovals out of body
        canvas.fillOval( x+75, y+120 , 90, 120 );//ovals out of body
        canvas.fillRect( x+20, y+0, 15, 50 );//slit out of head
        canvas.setColor( Color.black );
        canvas.fillRoundRect( x-60, y+240, 210, 60, 10, 10 );//base rect

    }
}

最佳答案

int range = (maximum - minimum) + 1;     
int out = (int) (Math.random() * range) + minimum;
System.out.println("random " + out);

更多详细信息请参阅此链接 See

关于Java随机数生成器需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42128774/

相关文章:

java - 如何使用数组在 Java 的一行中创建多个字符串和整数值?

java - 在不同的类中使用执行器并同步 CountDownLatch

java - 图像变换导致红色图像?

java - 列出有重复项到无重复项

java - 倒数的 JUnit 测试

java - 如何在Spring Security 4中动态修改登录用户的角色?

java - 想知道 Java object-json 序列化

java - 这些是什么类型的java构造函数?构造函数链接?

java - 无法识别的 SSL 消息,明文连接?异常(exception)

java - 如何设置 eclipse/webapp maven?