2017-02-09 33 views
0

在我的代碼中,我需要使用if語句,具體取決於用戶輸入什麼來繪製一定數量的棋子。所以如果用戶輸入4個棋子,我需要其中的三個棋子進入隨機點。所以我需要在我的畫布尺寸之間產生隨機的x和y座標。我知道我需要使用隨機數字生成器,但我不確定它是如何工作的。請幫助我。由於 這裏是我的代碼...需要Java隨機數生成器幫助

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 

    } 
} 

回答

1

提示:

Random random = new Random(); 
int limit = 1000; 
int randomValue = random.nextInt(limit); 

將返回0和999

之間的隨機值。如果我想在1和1000之間的值,我我會做

int limit = 1000; 
int randomValue = random.nextInt(limit) + 1; 
+0

真棒。非常感謝你 – user7538286

1

如果您需要生成從最小到最大(包括兩個)的數字,您寫

Random random = new Random(); 
random.nextInt(max - min + 1) + min 

作爲每documentation,該方法調用返回「的僞隨機,介於0(含)均勻分佈的int值和規定值(不包括)」。

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

更多信息請參考以下鏈接See